旋转图像多次,但保持一致的角落 [英] Rotate an image multiple times but keep the corners aligned

查看:219
本文介绍了旋转图像多次,但保持一致的角落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着用以下code旋转图像,但我发现生成的图像变得越来越大:

I tried to rotate an image with following code, but I found the generated image gets bigger and bigger:

Matrix matrix = new Matrix();
matrix.setRotate(10);
Bitmap newImage = Bitmap.createBitmap(image, 0, 0, 
        image.getWidth(), image.getHeight(), matrix, true);

您可以看到图片:

原始

旋转10度

旋转10度再次

旋转10度再次

蓝色矩形的完整图像。

可以看到的图像变得越来越大(虽然沙发的大小不改变),和原始图像的4个角不在的新的图像的边界以后。

You can see the images getting bigger and bigger(although the size of sofa is not changed), and the 4 corners of original image are not on the borders of new images later.

如何改变code,保持弯道上边框(就像第二图象)?

How to change the code to keep the corners on the border(just like the 2nd image)?

我忘了说我创建GitHub上的演示项目。您可以克隆它,主要的Java code是在这里:

I forgot to say I've created a demo project on github. You can clone it, the main java code is here:

https://github.com/freewind/Android-RotateTest/blob/master/src/com/example/MyActivity.java

推荐答案

我想你的code和一些旋转后它与内存不足的异常崩溃导致每个新位图被创建时,这是非常耗费资源。你应该永远永远!在迭代使用createBitMap()。我做了一些修改,图像旋转code和现在它正在按预期运行。结果
这里是code:

I tried your code and after some rotation it crashed with the OutOfMemory exception cause each time a new bitmap is created which is very resource intensive. You should never never! use createBitMap() in iteration. I made some modification to your image rotation code and now it's running as expected.
Here is the code:

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的的规模型到矩阵

<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" />

如果我们想从ImageView的旋转的位图,这样做:

And if we want to get the rotated bitmap from ImageView, do this:

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

我希望这有助于。

I hope this helps.

这篇关于旋转图像多次,但保持一致的角落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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