图片变化的大小,因为它的旋转。我该如何停止? [英] Image changes size as it's rotated. How do I stop this?

查看:236
本文介绍了图片变化的大小,因为它的旋转。我该如何停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个游戏为Android,我需要旋转图像。当我明明旋转它的尺寸变化。例如,当它旋转45度(这是正方形的,但我想这适用于任何矩形,所以这是一个更通用的解决方案),它的宽度和高度成为对角线的长度,这是比原来长。一些代数后,你可以推断出该比例因子的sqrt(2)。但我知道的唯一途径旋转位图是一个矩阵。例如:

I'm making a game for Android and I need to rotate an image. When I rotate it obviously it's dimensions change. For example when it's rotated 45 degrees (it's square but I'd like this to work for any rectangle so it's a more general solution) it's width and height become the length of the diagonal, which is longer than the original. After some algebra you can work out that the scale factor is sqrt(2). But the only way I know of to rotate a bitmap is with a matrix. Ex:

matrix.postRotate(degrees);
rotated = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);

使用此方法的位图的大小保持恒定,以便以适合旋转后的图像中的图像必须收缩的内容。这导致了我的问题。

Using this method the size of the bitmap remains constant so to fit the rotated image in the content of the image must shrink. Which causes my problem.

我现在应该工作,但在运行时没有。可能是因为它过于复杂,从来没有少,那就是:

What I have now should work but when run doesn't. Probably because it's overly complex, never the less, here it is:

     float totalRotated = 0;

public void rotate(float degrees){
    if(mBitmap != null){
        float increment = (float)((mBitmap.getWidth()/45.0)*(Math.sqrt(2)-1));
        totalRotated += degrees;
        totalRotated -= (float)((int)totalRotated/360)*360;
        matrix.reset();
        matrix.setRotate(totalRotated);
        rotated = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
        rotated = Bitmap.createScaledBitmap(rotated, (int)(mBitmap.getWidth()+(((Math.abs(Math.abs(((int)totalRotated%90)-45)-45)))*increment)), (int)(mBitmap.getHeight()+(((Math.abs(Math.abs(((int)totalRotated%90)-45)-45)))*increment)), true);
    }
}

使用 Log.d 功能,我能够确定,在最后一条语句设置的尺寸是什么,我希望他们可以,但图像不改变大小。由于这甚至不工作,我需要一个更好的方式来做到这一点还是有办法来解决我的方法。另外我的方法只适用于正方形。那么,我该怎么办呢?

Using the Log.d function I was able to determine that the dimensions set in the last statement are what I expect them to be but the image doesn't change size. Since this doesn't even work, I need a better way to do this or a way to fix my method. Also my method only works for squares. So, how can I do this?

编辑:  我的方法做的工作,我只是不叫的setBounds()这不可能是,虽然做到这一点的唯一途径,这是如此低效的。

My method does work, I just didn't call setBounds() This can't be the only way to do it though, this is so inefficient.

推荐答案

目前还不清楚,你要寻找的,所以这里的根据你的函数,试图计算出新的位图的正确宽度和高度,做通过创建只是一个单一的位图的旋转。

It's not clear what you're looking for, so here's a function based on yours that attempts to compute the proper width and height of the new bitmap and do the rotation by creating just a single bitmap.

float totalRotated = 0;

public void rotate(float degrees){
    if(mBitmap != null){
        // compute the absolute rotation
        totalRotated = (totalRotated + degrees) % 360;
        // precompute some trig functions
        double radians = Math.toRadians(totalRotated);
        double sin = Math.abs(Math.sin(radians));
        double cos = Math.abs(Math.cos(radians));
        // figure out total width and height of new bitmap
        int newWidth = mBitmap.getWidth() * cos + mBitmap.getHeight() * sin;
        int newHeight = mBitmap.getWidth() * sin + mBitmap.getHeight() * cos;
        // set up matrix
        matrix.reset();
        matrix.setRotate(totalRotated);
        // create new bitmap by rotating mBitmap
        rotated = Bitmap.createBitmap(mBitmap, 0, 0,
                                      newWidth, newHeight, matrix, true);
    }
}

这篇关于图片变化的大小,因为它的旋转。我该如何停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆