Android 如何在Imageview中获取图像的高度?

Android 如何在Imageview中获取图像的高度?,android,android-layout,android-imageview,Android,Android Layout,Android Imageview,我在imageview中设置了一个图像。它显示在屏幕中央,因为我已经使其父布局(相对布局)匹配父布局。Imageview的高度和宽度也设置为与父级匹配 我想在imageview中获得图像的高度和宽度 我使用了: int imagWidth = imageView.getWidth(); int imagHeight = imageView.getHeight(); 但通过这个,我们得到了Imageview的高度和宽度 以下是我的xml: <RelativeLayout xmlns:and

我在imageview中设置了一个图像。它显示在屏幕中央,因为我已经使其父布局(相对布局)匹配父布局。Imageview的高度和宽度也设置为与父级匹配

我想在imageview中获得图像的高度和宽度

我使用了:

int imagWidth = imageView.getWidth();
int imagHeight = imageView.getHeight();
但通过这个,我们得到了Imageview的高度和宽度

以下是我的xml:

<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/imag"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:background="@android:color/black"
        android:src="@drawable/image2" />
</RelativeLayout>

  • 使用
    ImageView.getDrawable().getInstrinsicWidth()
    getIntrinsicHeight()
    都将返回原始维度

  • 通过
    ImageView.getDrawable()
    获取
    Drawable
    并将其强制转换为
    BitmapDrawable
    ,然后使用
    BitmapDrawable.getBitmap().getWidth()
    getHeight()
    还返回原始图像及其尺寸

  • 请尝试以下代码:

    ImageView.getDrawable().getIntrinsicHeight()//original height of underlying image
    ImageView.getDrawable().getIntrinsicWidth()//original width of underlying image
    imageView.getMeasuredHeight();//height of imageView
    imageView.getMeasuredWidth();//width of imageView
    
    参考资料:

    试试这个:-

    Bitmap b = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
    int w = b.getWidth(); 
    int h = b.getHeight(); // height of your image
    
    int height = imageView.getDrawable().getIntrinsicHeight();
    int height_in_dp = Math.round(height / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT)); 
    
    p.S:-
    h
    是图像的高度(原始图像高度

    更新

    如果要获得imageView中图像的确切高度,请尝试以下操作:-

    Bitmap b = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
    int w = b.getWidth(); 
    int h = b.getHeight(); // height of your image
    
    int height = imageView.getDrawable().getIntrinsicHeight();
    int height_in_dp = Math.round(height / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT)); 
    

    height\u in_dp
    dp中图像的高度

    试试这个:-你能分享你的xml吗?@YahyaMukhtar:我已经更新了我的问题..请检查我想通过编程获得我的图像的高度。@AshutoshTripathi:我已经试过了。但它不起作用。请看我在应用程序崩溃后发布的答案:ColorDrawable无法转换为android.graphics.drawable。BitmapDrawable@PragyaMendiratta请检查更新的答案。。!!我用图片编辑了我的问题。请在@yahya mukhtar答案中查看我的评论。。。。。。。图像高度大于您提到的代码的imageview…@PragyaMendiratta更新了答案,请检查@如果您不明白,请随时提问!!获取以下错误:ColorDrawable无法强制转换为android.graphics.drawable。BitmapDrawable@Pragya现在检查它。根据此位图高度,我使用imageview.getHeight()=672获得h=882和imageview(imagHeight)高度。。图像高度大于imageview。。这不对,解决方案的结果是什么-px或dp?获取原始尺寸。一张照片的照片你不明白…你能解释一下吗?请点击此链接了解更多信息请参见我的更新答案@PragyaMendiratta
    Bitmap b =  ((BitmapDrawable) imag.getDrawable()).getBitmap();
    int w = b.getWidth();
    int h = b.getHeight();