Reputation: 171
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?
Upvotes: 7
Views: 9836
Reputation: 2508
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);
Upvotes: 0
Reputation: 93561
Use the matrix version of draw bitmap. The important thing is to do it in the right order. Start with an identity matrix. Then scale it by whatever factor you want using postScale. That will make it grow but without changing the origin. Then translate it by using postTranslate, which will move it left/right/up/down. Then draw the bitmap onto the canvas, passing it the matrix. It will scale and move the bitmap by the matrix before copying it.
Upvotes: 10