简体   繁体   English

代码使用矩阵旋转位图导致强制关闭

[英]Code to rotate a bitmap using Matrix causing force close

I m working on a simple game where a ball tumbles on a slope and has to avoid the obstacles by jumping. 我正在做一个简单的游戏,球在斜坡上滚动,必须通过跳跃来避开障碍物。 The problem comes when I am trying to rotate the ball object using the Matrix . 当我尝试使用Matrix旋转球形物体时,问题就来了。 I am using the following code:- 我正在使用以下代码:-

private void rotateMe() {
    if(jump==0){
        Matrix mtx=new Matrix();
        mtx.postRotate(deg);
        Bitmap rotatedBMP=Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), mtx, false);
        deg=deg+1; //deg=0 in beginning
        bm=rotatedBMP;
    }   
}

This method is written in the class of ball, which is internally called by another method move(). 此方法写在ball类中,该类由另一个方法move()内部调用。 The bitmap bm is not rotating as I wanted. bm的位图没有按我的意愿旋转。 It's just falling out of the screen, which seems like the size of bitmap has increased. 它只是从屏幕上掉出来,似乎位图的大小已增加。 And I've to force close it after few seconds. 我必须在几秒钟后强行关闭它。
I've seen other rotate bitmap questions, but they are not working for me too. 我见过其他旋转位图问题,但它们对我也不起作用。 Thanks in advance. 提前致谢。

You are rotating the Matrix with the same value ( 10 ) all the time. 您一直在旋转具有相同值( 10 )的Matrix That is why the ball is not rotating. 这就是为什么球不旋转的原因。 Do the following instead: mtx.postRotate(deg); 而是执行以下操作: mtx.postRotate(deg);

Also, a general remark: I guess you are drawing the resulting rotated ball bm ? 另外,一个一般性的评论:我猜您正在绘制生成的旋转球bm If you are drawing the ball in a different thread then the first thing you should look at is to make sure your threads have exclusive access to bm so that you won't be rendering the ball while it is being affected a new value at bm=rotatedBMP; 如果您要在其他线程上绘制球,那么您应该首先查看的是确保您的线程对bm具有独占访问权,以便在bm=rotatedBMP;新值影响球时不会渲染球。 bm=rotatedBMP;

Anyways found the answer by myself. 无论如何我自己找到了答案。 The code I am using now is as follows (for those who are having same problem) 我现在使用的代码如下(对于那些有相同问题的人)

Matrix mtx=new Matrix();
mtx.postRotate(deg,25,25);
mtx.postTranslate(x, y);
c.drawBitmap(bm, mtx, paint);
deg=deg-10;

The code is running fine now. 代码现在运行良好。 Thanks anyways. 不管怎么说,多谢拉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM