我有一个 Activity
,它有一个在 XML 中定义的自定义 ImageView
,用于图像上的捏合缩放功能。现在,我的问题是我想从捏合缩放功能中获取缩放后的 Bitmap
。例如,如果用户对图像进行缩放,那么我想将图像的确切大小和位置存储为位图。
这是我在 XML 中声明的自定义 ImageView。
<com.cam.view.PinchZoom_ImageView
android:id="@+id/gallery_selected_image_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/icon"
android:scaleType="center"
/>
更新:
我正在尝试使用下面的代码来获取缩放后的图像,但它返回的是空白位图。
pinchZoom.setDrawingCacheEnabled(true);
pinchZoom.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
pinchZoom.layout(0, 0, pinchZoom.getMeasuredWidth(), pinchZoom.getMeasuredHeight());
pinchZoom.buildDrawingCache(true);
Bitmap_gallery_Item = Bitmap.createBitmap(pinchZoom.getDrawingCache());
pinchZoom.setDrawingCacheEnabled(false);
如有任何帮助,我们将不胜感激。谢谢
最佳答案
好吧,最后我不敢相信这只是一行代码。我终于成功使用了 matrix
.解决方案非常简单,我存储了 translation and scaling
矩阵中的图像。所以,我宣布最终 matrix
作为public static
并使用该矩阵在 Canvas 上创建绘制位图。
Canvas comboImage = new Canvas(cs);
background = Bitmap.createScaledBitmap(background, width, height, true);
comboImage.drawBitmap(background, 0, 0, null);
comboImage.drawBitmap(foreground, PinchZoom_ImageView.matrix, null);
如您所见PinchZoom_ImageView.matrix
.这是一个matrix
包含我声明为 public static
的缩放和平移图像的最终位置.
关于android - 如何从 ImageView 获取缩放后的 Bitmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7648155/