2

I used canvas.drawbitmap(bitmap,matrix,paint) to draw the bitmap on canvas. yes, it worked most of the time! But sometimes it did not work. There was nothing on canvas after I called the method canvas.drawbitmap(); So, what`s wrong?

1: I chose a picture from the album and got the Bitmap which named bgBitmap

2: Created a CanvasView which extends View. Then Create a empty bitmap on canvas that to paint something on it.

mBitmap = Bitmap.createBitmap(screenWidth, screenHeight,Bitmap.Config.RGB_565);
mCanvas = new Canvas(mBitmap);  
mCanvas.drawColor(Color.WHITE);

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(getResources().getColor(R.color.color_white));
    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);   
}

3: draw the picture on the canvas.

mCanvas.drawBitmap(bgBitmap,matrix, bitmapPaint);
this.invalidate();

It worked on most of the time, but sometimes it did not work.

Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
kim
  • 41
  • 3

2 Answers2

0

1: I chose a picture from the album and got the Bitmap which named bgBitmap;

2: Created a CanvasView which extends View.Then,Create a empty bitmap on canvas that to paint someting on it.

 mBitmap = Bitmap.createBitmap(screenWidth, screenHeight,Bitmap.Config.RGB_565);
 mCanvas = new Canvas(mBitmap);  
 mCanvas.drawColor(Color.WHITE);

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(getResources().getColor(R.color.color_white));
    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);   
}

3: draw the picture on the canvas.

 mCanvas.drawBitmap(bgBitmap,matrix, bitmapPaint);
 this.invalidate();

it worked on most of the time,but sometimes it did not work.

kim
  • 41
  • 3
0

I had a similar problem....at first "canvas.drawBitmap()" worked, but then it stopped working, depending on what bitmap I was trying to display. I was working with photos. I downscaled a 24Mb panorama photo to about 400K, but even that was too large. When I reduced the jpeg size even further, (downscaling the resolution - not reducing colour quality), it started working reliably.

Tim Cooper
  • 10,023
  • 5
  • 61
  • 77