Tejas
Tejas

Reputation: 358

Android two Canvas within single Custom View (within onDraw())

I have custom View.

within this I have onDraw() function.

  @Override
public void onDraw(Canvas canvas) 
{

     canvas.drawLine(0,0,600,200,black);

    } 

within this onDraw() I want to create one more canvas . Suppose canvas1.

  @Override
public void onDraw(Canvas canvas) 
{

     canvas.drawLine(0,0,600,200,black);

     canvas1.drawLine(0,0,500,100,Red);        
    } 

And on The button Click I want to make the canvas1 plot visible and Invisible. but I don't want to redraw again.
First thing: Is this possible to create one more canvas within onDraw()? *And make it visible and InVisible.*

If you have any other way to do this please suggest me, but apart from solution to redraw() again. I can't redraw() there is some problem in my case.

Hope you have understood my problem? please suggest me .

Upvotes: 1

Views: 3169

Answers (2)

Tejas
Tejas

Reputation: 358

Best is to design 2 custom view and put on top of each other and make background of bottom one transparent.

Upvotes: 1

strike
strike

Reputation: 1039

for using another Canvas u need draw a bitmap on second canvas and then draw that on canvas

Bitmap bitmap ;
Canvas c2 = new Canvas(bitmap);

@Override
public void onDraw(Canvas canvas) 
{

     canvas.drawLine(0,0,600,200,black);

     // Draw on Second canvas
     c2.drawLine(0,0,500,100,Red); 
     // Draw second canvas (c2) to first one
     canvas.drawBitmap(bitmap, 0, 0, null);

} 

Upvotes: 4

Related Questions