Android Scaling Canvas Bitmap

僤鯓⒐⒋嵵緔 提交于 2019-12-04 08:38:41
canvas.drawBitmap(out, null, thumbnailRectF, thumbCanvasPaint); 

should change to

canvas.drawBitmap(out, new Rect(0,0,mBitmap.getWidht, mBitmap.getheight), thumbnailRectF, thumbCanvasPaint);

There is no need for

Bitmap out = Bitmap.createScaledBitmap(mBitmap, (int) thumbWidth, (int)....

Also check that mScaled is true all the time when zoom is greater than 1

Scale bitmap by Bitmap.createScaledBitmap then draw will not work

The solution for scale the canvas bitmap is use this function (from the docs)

void drawBitmap (Bitmap bitmap,  Rect src, Rect dst, Paint paint)
// dst : Rect: The rectangle that the bitmap will be scaled/translated to fit into

so by changing the size of dst, your bitmap size will change

Here is example if I want to draw a bitmap at top-left and scale it to 100px x 120px

Bitmap bitmap = BitmapFactory.decodeResource(...);//bitmap to be drawn

float left = 0;
float top = 0;
RectF dst = new RectF(left, top, left + 100, top + 120); // width=100, height=120

canvas.drawBitmap(bitmap, null, dst, null);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!