The canvas passed in onDraw is used for drawing to the screen, so you
shouldn't store it. You can create your own canvas from your own
Bitmap outside of onDraw easily.

private Bitmap mBitmap;
private Canvas mCanvas;
private Paint mPaint = new Paint();

private void createSurface() {
  Bitmap bmp = Bitmap.createBitmap(64, 64, Bitmap.Config.ARGB8888);
  Canvas c = new Canvas(bmp);
  mBitmap = bmp;
  mCanvas = c;
  mPaint.setColor(0xffff0000);
}

private void drawLine(float x1, float y1, float x2, float y2, Paint p)
{
  mCanvas.drawLine(x1,y1,x2,y2,p);
}

Please note that the Canvas can only draw to bitmaps that are Mutable.

http://code.google.com/android/reference/android/graphics/Bitmap.html
http://code.google.com/android/reference/android/graphics/Canvas.html

battlecry wrote:
> Hi
>
>    I want use canvas outside of the onDraw() method, I code this way:
>
> Canvas mCanvas;
>
> protected void onDraw(Canvas canvas) {
>     mCanvas = canvas;
> }
>
> private void drawSth(){
>     mCanvas.drawRect(0,0,100,100,mPaint);
> }
>
>
> but it seems useless, please tell me how to draw sth outside of
> onDraw() method?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new M5 SDK!
http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to