I'm having a big trouble here. I need to draw a doll in a bed (as in a medical bed), and I can't seem to do it. I tried by creating a custom view, then I started to draw. I used canvas.drawCircle
and canvas.drawRect
. I managed to draw this doll, but I based it size on the height and width of the custom view, so in the end I couldn't set the angles.
Then, I tried to set my custom xml attribute (size), and then wrap its content. But wrap_content didn't work, it was always setting as full screen. So I couldn't use this too, because I need the right position of the head to set up the rest of the body.
What I exactly need is, draw some geometric forms, and then manipulate them individually. Like, the user choose to move the leg, then when he press the up button, the leg will set angle +1. I don't know if this is possible with a custom View and only onDraw. I'm really really really lost now, please, someone at least point me out what I need to use to achieve this.
Picture
When drawing into the canvas, you are able to perform all kinds of transformations to the shapes you want to draw.
So if you first want to rotate the whole doll, you should perform a transformation before the doll is drawn. Then you can transform each body part within that trasformation.
For example, a doll with one leg.
//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()
There are all kinds of transformations, like rotation and skewing and so on. Just use your auto-completion to find out how they are written.
(I hope the indentation makes it clear, how it works)
Best way is to use RenderScript or OpenGL
It give you best performance but it`s hard to implement. Here is good tutorial, there are 5 chapters in detail.