Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 图像大小随时间变化';它是旋转的。我该怎么阻止这一切?_Android_Bitmap_Transformation - Fatal编程技术网

Android 图像大小随时间变化';它是旋转的。我该怎么阻止这一切?

Android 图像大小随时间变化';它是旋转的。我该怎么阻止这一切?,android,bitmap,transformation,Android,Bitmap,Transformation,我正在为Android制作一个游戏,我需要旋转一个图像。当我旋转它时,很明显它的尺寸改变了。例如,当它旋转45度时(它是正方形,但我希望它适用于任何矩形,所以这是一个更一般的解决方案),它的宽度和高度变成对角线的长度,比原始长度长。经过一些代数运算,你可以算出比例因子是sqrt(2)。但我知道旋转位图的唯一方法是使用矩阵。例: matrix.postRotate(degrees); rotated = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getW

我正在为Android制作一个游戏,我需要旋转一个图像。当我旋转它时,很明显它的尺寸改变了。例如,当它旋转45度时(它是正方形,但我希望它适用于任何矩形,所以这是一个更一般的解决方案),它的宽度和高度变成对角线的长度,比原始长度长。经过一些代数运算,你可以算出比例因子是sqrt(2)。但我知道旋转位图的唯一方法是使用矩阵。例:

matrix.postRotate(degrees);
rotated = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
使用此方法,位图的大小保持不变,以便在图像内容中适合旋转的图像必须收缩。这导致了我的问题

我现在所拥有的应该可以使用,但当run无法使用时。可能是因为它过于复杂,但始终如此,这里是:

     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
函数,我能够确定上一条语句中设置的尺寸是我期望的尺寸,但图像大小不变。因为这根本不起作用,我需要一个更好的方法来做这件事,或者一个方法来修复我的方法。我的方法也只适用于正方形。那么,我该怎么做呢

编辑:
我的方法确实有效,我只是没有调用
setBounds()
但这不是唯一的方法,效率太低了

不清楚您在寻找什么,因此这里有一个基于您的函数,它试图计算新位图的正确宽度和高度,并通过创建单个位图来进行旋转

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);
    }
}

现在还不清楚您在寻找什么,所以这里有一个基于您的函数,它试图计算新位图的正确宽度和高度,并通过创建单个位图来进行旋转

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);
    }
}

我尝试了盖布的解决方案,得到了和拉梅什和雷吉斯一样的错误。这对我很有用:

        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
        final int width = mBitmap.getWidth();
        final int height = mBitmap.getHeight();
        final int newWidth = (int) (width * cos + height * sin);
        final int newHeight = (int) (width * sin + height * cos);
        // set up matrix
        final Matrix tf = new Matrix();
        tf.postRotate((float) Math.toDegrees(radians), width / 2, height / 2);
        tf.postTranslate(
            (newWidth - width) / 2,
            (newHeight - height) / 2);
        // create new bitmap by rotating mBitmap with canvas
        final Bitmap rotatedBmp = Bitmap.createBitmap(
            newWidth, newHeight, Bitmap.Config.ARGB_8888);
        final Canvas canvas = new Canvas(rotatedBmp);
        canvas.drawBitmap(mBitmap, tf, null);

我尝试了盖布的解决方案,得到了和拉梅什和雷吉斯一样的错误。这对我很有用:

        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
        final int width = mBitmap.getWidth();
        final int height = mBitmap.getHeight();
        final int newWidth = (int) (width * cos + height * sin);
        final int newHeight = (int) (width * sin + height * cos);
        // set up matrix
        final Matrix tf = new Matrix();
        tf.postRotate((float) Math.toDegrees(radians), width / 2, height / 2);
        tf.postTranslate(
            (newWidth - width) / 2,
            (newHeight - height) / 2);
        // create new bitmap by rotating mBitmap with canvas
        final Bitmap rotatedBmp = Bitmap.createBitmap(
            newWidth, newHeight, Bitmap.Config.ARGB_8888);
        final Canvas canvas = new Canvas(rotatedBmp);
        canvas.drawBitmap(mBitmap, tf, null);

请澄清你的问题是什么。我不太清楚。请澄清你的问题是什么。我不太清楚。虽然你的计算简单得多,而且可能更准确。因为如果第一个整数加上第三个整数(x+宽度)不小于或等于作为参数传递的位图的宽度,createBitmap方法会引发非法参数异常。如果这样做,将抛出异常。我现在知道我必须纠正的是边界矩形。所以我只是加了一行,用你的计算。现在我所要做的就是校正缩放所产生的偏移量。这应该不会太难。@Fsmv:您可能只需要一个
matrix.postTranslate((newWidth-mBitmap.getWidth())/2,(newHeight-mBitmap.getHeight())/2)我尝试了你的代码,我得到了异常:非法参数异常(x+宽度)不小于。。。。请帮助meAlso获得IllegalArgumentException:x+宽度必须为,而您的计算要简单得多,可能更正确。因为如果第一个整数加上第三个整数(x+宽度)不小于或等于作为参数传递的位图的宽度,createBitmap方法会引发非法参数异常。如果这样做,将抛出异常。我现在知道我必须纠正的是边界矩形。所以我只是加了一行,用你的计算。现在我所要做的就是校正缩放所产生的偏移量。这应该不会太难。@Fsmv:您可能只需要一个
matrix.postTranslate((newWidth-mBitmap.getWidth())/2,(newHeight-mBitmap.getHeight())/2)我尝试了你的代码,我得到了异常:非法参数异常(x+宽度)不小于。。。。请帮助我获取IllegalArgumentException:x+宽度必须为