android - 如何从 ImageView 获取缩放后的 Bitmap

标签 android

我有一个 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/

相关文章:

java - 如何从字符串中获取 Resource(int) - Android

android - 如何下载Beta版APK进行测试?

java - http 连接错误 javax.net.ssl.SSLPeerUnverifiedException : No peer certificate below jelly bean version

Android SearchView 通过 android 启动器图标?

java - android中语法异常的json如何解析?

android - 如何在浏览器 Android 中点击 url 启动应用程序

android - 错误 :(232) Ignore: Unknown issue id "InlineApi"

Android 线程和 Throwable

android - 仅将 RecyclerView 中的 View 动态添加到当前项目

java - 在 Android 中使用 List<String> 创建自定义 ListView