我在这里遇到了大麻烦。我需要在床上画一个洋娃娃(如在病床上),但我似乎做不到。我试着创建一个自定义视图,然后开始绘制。我用了canvas.drawCircle
和canvas.drawRect
。我画了这个洋娃娃,但是我根据自定义视图的高度和宽度来确定它的大小,所以最后我无法设置角度。
然后,我尝试设置自定义XML属性(大小),然后包装其内容。但是wrap_内容不起作用,它总是设置为全屏。所以我也不能用这个,因为我需要头部的正确位置来建立身体的其余部分。
我真正需要的是,画一些几何形式,然后单独操作它们。例如,用户选择移动腿,然后当他按下向上按钮时,腿将设置角度+1。我不知道这是否有可能,只有一个自定义视图和ondraw。我现在真的很迷茫,拜托,至少有人指出我需要用什么来实现这个目标。
Picture
最佳答案
在画布中绘图时,可以对要绘制的形状执行各种转换。
因此,如果首先要旋转整个玩偶,应在绘制玩偶之前执行变换。然后你可以在变换中变换身体的每一部分。
例如,有一条腿的洋娃娃。
//save the canvas transformation state
canvas.save();
//first move the whole doll
canvas.translate(dollPosition.x,dollposition.y);
//we are now inside the coordinate system of the doll
//draw head (Position is now relative to the center of the doll position)
canvas.drawCircle(dollHeadPosition.x, dollHeadPosition.y, headRadius, paint);
//save the canvas to transform leg independently
canvas.save();
canvas.translate(legPosition.x,legPosition.y);
//draw leg
canvas.drawRect(...);
//restore the canvas to get back to your doll coordinate system
canvas.restore()
//restore canvas to get to global coordinate system
canvas.restore()
有各种各样的变换,比如旋转和扭曲等等。只需使用你的自动完成来了解它们是如何写的。
(我希望凹痕清楚地说明了它的工作原理)