如何从ImageView获取实际图像的宽度/高度?

3
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/myImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:background="@android:color/black"
        android:src="@drawable/image2" />

    <View
        android:id="@+id/view_clickable_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#aa000000" />


</RelativeLayout>

我希望在获取坐标后,能够使ImageView(myImage)中的图像小部分可点击。这对我来说很有效。现在,我想要在该图像上获得双指缩放的功能。当我放大时,可点击区域将随着图像一起缩放。

我的问题是,我想要获取ImageView中图像的高度(不包括顶部和底部的黑色空白部分。由于ImageView被设置为与父容器匹配,因此在横屏模式下,图像显示在屏幕中央)。每次我都只能获取到ImageView的高度。

以下是用于获取图像和ImageView高度的代码:

 int imagWidth = imageView.getWidth();
        int h = imageView.getHeight();


        int w = imageView.getDrawable().getIntrinsicWidth();
        int imagHeight = imageView.getDrawable().getIntrinsicHeight();

        Log.e("height", "Image height" + imagHeight + "\n" + "Imageview height" + h);

根据计算: 图片高度 = 882 ImageView 高度 = 672


1
请查看ImageView#getDrawable()方法。 - pskink
1
是的,它正在工作 - 它返回显示的“Drawable”的大小。 - pskink
@pskink:请检查我的更新帖子 我已经更新了我得到的值 - Pragya Mendiratta
我不知道你真正想要实现什么:如果你不相信getIntrinsic*方法,那就尝试将你的Drawable转换为BitmapDrawable并获取Bitmap的大小 - 通过调用BitmapDrawable#getBitmap()方法来获得。 - pskink
嗨@pskink,如果图像大小为100x100,并且我们将其设置到高度和宽度为50X50的imageView中,那么使用您刚才建议的方法,图像的尺寸将是多少? - Nirmal Prajapat
显示剩余6条评论
3个回答

3

为我工作

    imgView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // Ensure you call it only once :
                imgView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                // Here you can get the size :)
                width = imgView.getWidth();
                height = imgView.getHeight();
            }
        });

1
要获取图像视图中图像的高度,您可以简单地执行以下操作:
int width = imgView.getDrawable().getIntrinsicWidth();
int height = imgView.getDrawable().getIntrinsicHeight();

这将返回高度和宽度的“固有”值。 (这些值是您加载到图像视图中而不进行任何缩放的图像的值。您可以调整这些值以匹配您可能应用的任何缩放)。要为缩放调整值(假设您知道其处于横向模式),应该使用以下代码:
int width = imgView.getDrawable().getIntrinsicWidth();
int height = imgView.getDrawable().getIntrinsicHeight();  
double scale = ((double)imgeView.getWidth())/width
int final_height = Math.toIntExact(Math.round(scale * height));

现在的final_height是显示图像的高度。

我已经尝试过了...不起作用...获取ImageView的高度和宽度.. - Pragya Mendiratta
@PragyaMendiratta...能否让我们看看你的代码?因为我之前也使用过相同的函数来获取我在应用程序中使用的图像的高度和宽度。 - Karan Shishoo
@PragyaMendiratta,你似乎已经得到了图像的实际高度。由于图像高度与图像视图高度不同,你是在询问显示图像的高度吗?只需通过当前图像缩放比例来缩放图像高度即可。我会编辑我的答案以匹配这一点。 - Karan Shishoo

0

使用以下代码与 ViewTreeObserver 一起使用

int yourImageViewWidth , yourImageViewHeight;
ImageView y = (ImageView)findViewById(R.id.scaled_image);
ViewTreeObserver vto = yourImageView.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    public boolean onPreDraw() {
        yourImageView.getViewTreeObserver().removeOnPreDrawListener(this);
        yourImageViewHeight = yourImageView.getMeasuredHeight();
        yourImageViewWidth = yourImageView.getMeasuredWidth();
        Log.d("Height: " + yourImageViewHeight + " Width: " + yourImageViewWidth);
        return true;
    }
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接