'Android: Using matrix to draw bitmaps

im trying to learn drawing bitmap on canvas with the usage of

drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint);

Because i need one of my pngs to increase its size nearly every frame, i managed to do that with

Matrix matrix=new Matrix();
matrix.setScale(0.001f,0.001f);

and this works fine for me. But the problem i have is when it comes to placing the image in the right coordinates. I thought either of those would do it

matrix.setTranslate(x,y); matrix.postTranslate(x,y);

But that is where im mistaken, the image is always drawn on 0,0 coordinates. Before i needed scaling images i was fine with using

canvas.drawBitmap(bmp,x,y,paint);

where i can specify the point for it to be drawn. Can i do so with the matrix version of drawBitmap too? or how else should i resize my image so often?



Solution 1:[1]

Matrix m = new Matrix();
final float wantedWidth = ...;
final float wantedHeight = ...;
m.postScale(wantedWidth / bitmap.getWidth(), wantedHeight / bitmap.getHeight());
m.postTranslate(x, y);
canvas.drawBitmap(bitmap, m, paint);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 beginner