开发者

Rotate Bitmap (bigger than Canvas) but centered with Canvas

开发者 https://www.devze.com 2023-01-17 07:43 出处:网络
I\'m trying to rotate a bitmap (which is bigger than Canvas) keeping it centered with Canvas and repeatedly in time, in order to make it appears as spinning. I wanted to post an image explaining the s

I'm trying to rotate a bitmap (which is bigger than Canvas) keeping it centered with Canvas and repeatedly in time, in order to make it appears as spinning. I wanted to post an image explaining the situation, but I can't (not enough reputations; it's a pitty because I've spent a lot of time doing it :( ). I hope you understand what's开发者_运维知识库 the problem, anyway.

Since I don't want the rest of drawables drawn to the Canvas to be rotated as well, I can't use Canvas.rotate(). So I've tried all the different combinations using matrix, and drawing the bitmap to the Canvas using an offset, for example:

Matrix matrix = new Matrix();
matrix.postRotate(degree); //degree is increasing with time
mrotatedbackground = CreateBitmap(mbackground, (mbackground.getWidth()-screenwidth)/2, (mbackground.screenHeight()-screenheight)/2, screenwidth, screenheight, matrix, true); 
canvas.drawBitmap(mrotatedbackground, -screenwidth/2, -screenheight/2, paint); 


Yes you can use Canvas.rotate():

onDraw(Canvas c) {
    c.save();
    c.rotate(some_angle);
    c.drawBitmap(...);
    c.restore();
}

Canvas.save() saves a copy of the current transorm and restores it when you call Canvas.restore().

Note that you can have multiple nested Canvas.save() calls, each of them will need you to call Canvas.restore().

0

精彩评论

暂无评论...
验证码 换一张
取 消