【问题标题】:How to get the size of bitmap after displaying it in ImageView在 ImageView 中显示后如何获取位图的大小
【发布时间】:2013-02-14 05:04:08
【问题描述】:

我有一个图像视图

<ImageView
        android:id="@+id/imgCaptured"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:src="@drawable/captured_image" />

我从相机捕捉图像,将该图像转换为位图。

Bitmap thumbnail;
thumbnail = MediaStore.Images.Media.getBitmap(getActivity()
                    .getContentResolver(), imageUri);

当我在上面的图像视图中显示该位图之前获得该位图的分辨率时,例如

Log.i("ImageWidth = " + thumbnail.getWidth(), "ImageHeight = "
                + thumbnail.getHeight());

它返回我ImageWidth = 2592 ImageHeight = 1936

在此之后,我在上面的图像视图中将此位图显示为imgCaptured.setImageBitmap(thumbnail);,然后我将图像视图的大小设为

Log.i("ImageView Width = " + imgCaptured.getWidth(),
                "ImageView Height = " + imgCaptured.getHeight());

这回了我ImageView Width = 480 ImageView Height = 720

现在我的问题是

  • 我如何在我的图像视图中显示该位图之后的大小。 我知道这可以通过使用这个来完成

    image.buildDrawingCache();
    Bitmap bmap = image.getDrawingCache();
    

    但这会创建一个大小与 imageview 相同的新位图。

  • 我也想知道,图像在图像视图中显示后是否会自动调整大小。如果是,那么有什么方法可以在不调整图像大小的情况下在 imageview 中显示图像。

编辑

实际上我已经拍摄了 2592x1936 的图像。我在我的 imageView 中显示了这张图片,对这张图片做了一些其他的操作。现在我想以相同的 2592x1936 分辨率保存此图像。有可能吗?

提前致谢。

【问题讨论】:

  • 澄清一下-您的意思是图像视图中位图的大小?如果没有padding,两者是等价的,ImageView的宽/高就是位图的宽/高。如果您有填充,只需将其减去。
  • 是的,我的意思是位图在图像视图中显示后的大小。
  • @Gabe Sechan 实际上我已经捕获了 2592x1936 的图像。我在我的 imageView 中显示了这张图片,对这张图片做了一些其他的操作。现在我想以相同的 2592x1936 分辨率保存此图像。有可能吗?
  • 是的,但可能质量不高。如果缩小图像(不保留完整大小的版本),则在缩小图像时会丢失数据,Android 会在扩展​​图像时猜测数据是什么。如果您操作的是完整大小的版本,则相当简单——只需使用带有 FileOutputStream 的位图类上的 compress() 函数。
  • 感谢回复您能否粘贴代码,用于从 2592x1936 大小的图像视图中检索全尺寸图像。

标签: android bitmap imageview android-imageview bitmapimage


【解决方案1】:

ImageView 中显示Bitmap 后,ImageView 将创建一个 BitmapDrawable 对象以在 ImageView 的 Canvas 中绘制它。所以可以调用ImageView.getDrawable()方法获取BitmapDrawable的引用,调用Drawable.getBounds(Rect rect)方法获取Bounds。通过边界,可以计算出ImageView中绘制的Bitmap的宽高

Drawable drawable = ImageView.getDrawable();
//you should call after the bitmap drawn
Rect bounds = drawable.getBounds();
int width = bounds.width();
int height = bounds.height();
int bitmapWidth = drawable.getIntrinsicWidth(); //this is the bitmap's width
int bitmapHeight = drawable.getIntrinsicHeight(); //this is the bitmap's height

【讨论】:

  • 此错误即将出现。 Drawable 类型中的 getBounds() 方法不适用于参数 (Rect)
  • 我正在为我的图像视图制作 scaleType="fitXY"。我删除了这个,现在它工作正常。非常感谢
  • 请告诉我如何保存这个原始分辨率为 2592x1936 的图像?
  • 就像Quadir所说的,你应该小心“scaleType”属性。因为它改变了 imageview 的高度。
猜你喜欢
  • 2012-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-16
  • 2017-02-09
  • 2015-01-22
  • 1970-01-01
相关资源
最近更新 更多