Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 641
Anderiel Avatar asked Apr 12 '13 18:04

Anderiel


1 Answers

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.

like image 191
Gabe Sechan Avatar answered Nov 10 '22 04:11

Gabe Sechan