Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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_Rotation - Fatal编程技术网

Android 多次旋转图像,但保持角对齐

Android 多次旋转图像,但保持角对齐,android,bitmap,rotation,Android,Bitmap,Rotation,我尝试使用以下代码旋转图像,但发现生成的图像越来越大: Matrix matrix = new Matrix(); matrix.setRotate(10); Bitmap newImage = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), matrix, true); 您可以看到以下图片: 原创 旋转10度 再次旋转10度 再次旋转10度 蓝色矩形是完整的图像 您可以看到图

我尝试使用以下代码旋转图像,但发现生成的图像越来越大:

Matrix matrix = new Matrix();
matrix.setRotate(10);
Bitmap newImage = Bitmap.createBitmap(image, 0, 0, 
        image.getWidth(), image.getHeight(), matrix, true);
您可以看到以下图片:

原创

旋转10度

再次旋转10度

再次旋转10度

蓝色矩形是完整的图像

您可以看到图像越来越大(尽管沙发的大小没有改变),并且原始图像的4个角不再位于新图像的边界上

如何更改代码以保持边框上的角(就像第二幅图像)

我忘了说我已经在github上创建了一个演示项目。您可以克隆它,主要的java代码如下:


必须旋转第一个图像。将第一个图像保存到变量中,然后创建副本。旋转时,始终从原始复制一份新副本,然后旋转该副本

编辑

将代码更改为:

 @Override
        public void onClick(View view) {
            Matrix matrix = new Matrix();
            matrix.setRotate(10);
            Bitmap copy = image;
            Bitmap newImage = Bitmap.createBitmap(copy, 0, 0, image.getWidth(), image.getHeight(), matrix, true);
            setNewImage(newImage);
        }

我似乎看不到你的图像,但我有同样的问题,每次我试图旋转一个图像,它似乎调整自己的大小

我设法写了一些代码,可以成功地改变我的形象。 我给你的第一个提示是,当你试图创建一个动画旋转图片时,你不应该每次都创建一个新的位图,因为这会使GC疯狂并降低性能速度

下面是我的代码,可以旋转图像而不调整其大小:

public static Matrix rotateMatrix(Bitmap bitmap, Shape shape, int rotation) {

        float scaleWidth = ((float) shape.getWidth()) / bitmap.getWidth();
        float scaleHeight = ((float) shape.getHeight()) / bitmap.getHeight();

        Matrix rotateMatrix = new Matrix();
        rotateMatrix.postScale(scaleWidth, scaleHeight);
        rotateMatrix.postRotate(rotation, shape.getWidth()/2, shape.getHeight()/2);
        rotateMatrix.postTranslate(shape.getX(), shape.getY());


        return rotateMatrix;

    }
注意:形状对象仅包含您试图旋转的对象的真实尺寸,例如100x100


我希望这能有所帮助。

我尝试了你的代码,经过一段时间的循环后,它崩溃了,出现OutOfMemory异常,因为每次创建一个新的位图时,都会占用大量资源。你永远不应该!在迭代中使用createBitMap()。我对您的图像旋转代码进行了一些修改,现在它按预期运行。
代码如下:

private void addListeners() {
    this.button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            Matrix matrix = new Matrix();

            //copying the image matrix(source) to this matrix 
            matrix.set(imageView.getImageMatrix());

            matrix.postRotate(10, imageView.getWidth()/2, imageView.getHeight()/2);
            imageView.setImageMatrix(matrix);

            //checking the size of the image
            Drawable d = imageView.getDrawable();
            Bitmap bmp = ((BitmapDrawable)d).getBitmap();
            imageInfo(bmp);
        }
    });
}
还将imageView的比例类型设置为“矩阵”


我希望这有帮助。

嘿,非常抱歉,这就是问题所在。如果你从一开始就把它旋转20度,就可以了。让我看看你的代码,以改进我的答案。有时不可能总是使用第一张图片。e、 g.旋转它,然后缩放它,然后旋转它。我发现karn提供的“带矩阵的ImageView”解决方案非常有效。这是因为他过滤了图像。如果你旋转,你必须制作一个新的位图。关于你的代码,我们是否要将返回矩阵设置为ImageView?什么是
imageInfo(bmp)
<ImageView
    android:id="@+id/Image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#336699"
    android:scaleType="matrix"
    android:padding="2px"
    android:src="@drawable/m" />
private Bitmap getBitmapFromView() {
    // this is the important code :)
    // Without it the view will have a dimension of 0,0 and the bitmap will be null
    imageView.setDrawingCacheEnabled(true);
    imageView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    imageView.layout(0, 0, imageView.getMeasuredWidth(), imageView.getMeasuredHeight());
    imageView.buildDrawingCache(true);
    Bitmap b = Bitmap.createBitmap(imageView.getDrawingCache());
    imageView.setDrawingCacheEnabled(false); // clear drawing cache
    return b;
}