## matrix equivalent to canvas.draw(bmp, rect, rect, paint)

Problems with Canvas, OpenGL, etc...

### matrix equivalent to canvas.draw(bmp, rect, rect, paint)

Hey there,

i used to draw a _part_ of my bitmap with this function:
canvas.drawBitmap(graphic.getBitmap(), graphic.getRectangle(), dest, null);

According to the javadoc it says:
"Draw the specified bitmap, scaling/translating automatically to fill the destination rectangle"

Now i d like to apply a rotation on my bitmap. So i think i need to use a matrix. The problem is I can t find the matrix method(s) which do(es) what my drawBitmap(...) function was doing (scaling/translating automatically).

Thank you for ur help !
oxyde
Junior Developer

Posts: 10
Joined: Wed Aug 11, 2010 1:28 pm
Location: paris, france

### Re: matrix equivalent to canvas.draw(bmp, rect, rect, paint)

I don't normally work with canvas', but could you use the rotate() method?

for example:
canvas.rotate(degrees);
jonbonazza
Master Developer

Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

### Re: matrix equivalent to canvas.draw(bmp, rect, rect, paint)

Hey,

I found the solution. i do
Matrix matrix = new Matrix();
matrix.postScale(0.25f, 0.25f);
this._bitmap = Bitmap.createBitmap(fullBitmap, left, top, WIDTH, HEIGHT, matrix, false);
to create my new bitmap which is a part of the fullBitmap.

and then i can apply a new rotation matrix to display my bitmap:

Matrix m = new Matrix();
m.postRotate(90, dest.left, dest.top + CardGraphic.DISPLAYED_HEIGHT );
canvas.drawBitmap(bitmap, m, null);

i hope it will help someone !
oxyde
Junior Developer

Posts: 10
Joined: Wed Aug 11, 2010 1:28 pm
Location: paris, france

### Re: matrix equivalent to canvas.draw(bmp, rect, rect, paint)

hi everyone,

i'm new to this forum. i read this thread hoping for a solution to a problem i'm having.

i have 8 cylinders concentrically placed on the screen. i want to be able to rotate each cylinder independently.
this means i can't rotate the canvas because that would rotate all of them at the same time.

when i draw the cylinders the first time, they look great and are positioned exactly where i want them.

when i rotate them, they are distorted. like they are not doing a simple rotation around the z-axis but a more complex 3d rotation. bottomline: the rotations ruin the view.

how do i make a bitmap of an image rotate 6 degrees around the z-axis and retain it's shape geometry?

thank you (whoever helps me!!!)

here's the code i'm using:
--------------------------------------------------------------------
// declare the global array of clock cylinder bitmaps
private Bitmap cylinders[] = new Bitmap[8];

// i load a cylinder into a bitmap
cylinders[0] = BitmapFactory.decodeResource(getResources(), R.drawable.wheel01 );
:
cylinders[7] = BitmapFactory.decodeResource(getResources(), R.drawable.wheel07 );

--------------------------
the next few lines are in a function scheduled to repeatedly execute on time-based periods:
--------------------------

final SurfaceHolder holder = getSurfaceHolder();

Canvas c = null;
try {
c = holder.lockCanvas();
if (c != null) {
setTouchEventsEnabled(true);
drawCylinders(c);
drawTouchPoint(c);
} catch(Exception e) {
// do some stuff
} finally {
if (c != null) holder.unlockCanvasAndPost(c);
}
}

----------------------------------------
the drawCylinders function
---------------------------------------
void drawCylinders(Canvas c) {
// we always save the canvas
c.save();

if ( first_time == true ) {
// do some stuff

} else {
// display the cylinders contained within the coordinates of a matching rectangle
for ( int ix=0; ix<=7; ix++ ) {
c.drawBitmap( cylinders[ix], null, (RectF)cylOrigins[ix], new Paint());
};

// rotate the inner cylinder

float degrees = (float) 6f * arbitrary_number;

int W = cylinders[0].getWidth();
int H = cylinders[0].getHeight();
int nW = cD[0]; // width of original geometric shape
int nH = cD[0]; // height
float scaleWidth = ((float) nW) / W;
float scaleHeight = ((float) nH) / H;

Matrix mSH = new Matrix();
mSH.postScale( scaleWidth, scaleHeight );
mSH.setRotate( (float)degrees, (float)240, (float)420 );

cylinders[0] = Bitmap.createBitmap(cylinders[0],0,0,W,H,mSH,true);

};

c.restore();
}
ichthus
Freshman

Posts: 3
Joined: Wed Nov 02, 2011 9:07 am

### Re: matrix equivalent to canvas.draw(bmp, rect, rect, paint)

BUMP...anyone help me?
ichthus
Freshman

Posts: 3
Joined: Wed Nov 02, 2011 9:07 am