我尝试使用以下代码旋转图像,但我发现生成的图像越来越大:
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
解决方法:
我尝试了你的代码,经过一些轮换后,它崩溃了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;
}
我希望这有帮助.
标签:android,rotation,bitmap 来源: https://codeday.me/bug/20190826/1727047.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。