我尝试使用以下代码旋转图像,但我发现生成的图像越来越大:
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代码在这里:
https://github.com/freewind/Android-RotateTest/blob/master/src/com/example/MyActivity.java
答案 0 :(得分:4)
我尝试了你的代码,经过一些轮换后,它崩溃了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设置为矩阵
<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获取旋转的位图,请执行以下操作:
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;
}
我希望这会有所帮助。
答案 1 :(得分:1)
您必须旋转第一张图像。将第一个图像保存到变量中,然后创建一个副本。旋转时,请始终从原件复制并旋转该副本。
修改强>
将您的代码更改为:
@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);
}
答案 2 :(得分:1)
我似乎无法看到您的图像,但我遇到了同样的问题,每次我试图旋转图像时,它似乎都会调整自己的大小。
我设法编写了一些可以成功转换图像的代码。 我给你的第一个提示是,当你试图创建一个动画旋转图片时,你不应该每次都创建一个新的Bitmap,因为这会让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。
我希望这会有所帮助。