我尝试了您的代码,经过一些轮换后,它会因 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;
}
我希望这有帮助。