Navaneeth Sen
Navaneeth Sen

Reputation: 6466

How to draw a View on a canvas by giving the x , y postion?

How can i draw a view on a canvas by giving the x,y position in the canvas.

For example,
I have custom view myView, which was created by inflating one of my layout.xml file. Now i want to draw this myView on the canvas at position (x, y).

How can i do that?

Please help with your ideas.

Thanks,
Sen

Updated:

To be more specific,
I have

myView extend view { 
... 
protected void onDraw(Canvas canvas) {  
super.onDraw(canvas);  
    { Inside which i will draw a curve }  

Then I will obtain 8 set of co-ordinates along the path and will store it in a list.

Then i have created a new view by using Absolute layout and its name is myinfo.xml(which contains two text views) present inside the res/layouts folder.

Now i want to inflate this view inside the onDraw method and want to place them in the above collected co-ordinates.

But i tried to inflate the view in this class(myView) but it is not working.

Please tell where am i wrong??

If you have some other suggestion, do update me.

Thanks & Regards,
Sen

Upvotes: 2

Views: 3979

Answers (1)

mreichelt
mreichelt

Reputation: 12455

Call save() on your Canvas to save the properties. Then you can do any operations you like, e.g. call clipRect(float left, float top, float right, float bottom) to clip the region of your view and then draw it on the canvas.

After you are done, call restoreToCount(int saveCount) using the integer returned by the save() command before to restore the Canvas' properties. :-)

Upvotes: 6

Related Questions