如何在android中获取图像视图的宽度和高度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10411975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:37:44  来源:igfitidea点击:

How to get the width and height of an Image View in android?

androidimageview

提问by Ankit

In my code I have an Image Viewin my XML layout and I keep changing the source image for this in my code. Now I want to know the width and height of the generated image each time.

在我的代码中Image View,我的 XML 布局中有一个,并且我一直在我的代码中为此更改源图像。现在我想知道每次生成图像的宽度和高度。

I tried using getWidth(), getHeight(),getMeasuredWidth(),and getMeasuredHeight(), but they all return 0.

我尝试使用getWidth(),getHeight(),getMeasuredWidth(),getMeasuredHeight(),但它们都返回 0。

How can I get this?

我怎样才能得到这个?

回答by Veer

Where you calling getWidth()and getHeight()on ImageView? If you calling from onCreate()in activity, it won't work. You need to wait for activity window to attached and then call getWidth() and getHeight()on ImageView. You can try calling getWidth() and getHeight()from onWindowFocusChanged()method of your activity.

你在哪里打电话getWidth()getHeight()在 ImageView 上?如果您从onCreate()活动中调用,它将不起作用。您需要等待活动窗口附加,然后调用getWidth() and getHeight()ImageView。您可以尝试getWidth() and getHeight()onWindowFocusChanged()活动的方法中调用。

@Override
public void onWindowFocusChanged(boolean hasFocus){
    int width=imageView.getWidth();
    int height=imageView.getHeight();
}

回答by Khan

try this

尝试这个

 ImageView iv=(ImageView)findViewById(R.id.image);
 ViewTreeObserver vto = iv.getViewTreeObserver();
    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        public boolean onPreDraw() {
           finalHeight = iv.getMeasuredHeight();
           finalWidth = iv.getMeasuredWidth();
           Log.e("hilength","Height: " + finalHeight + " Width: " + finalWidth);
            return true;
        }
    });

回答by Ravi Makvana

try this code

试试这个代码

int finalHeight, finalWidth;
final ImageView iv = (ImageView)findViewById(R.id.scaled_image);
final TextView tv = (TextView)findViewById(R.id.size_label);
ViewTreeObserver vto = iv.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    public boolean onPreDraw() {
        iv.getViewTreeObserver().removeOnPreDrawListener(this);
        finalHeight = iv.getMeasuredHeight();
        finalWidth = iv.getMeasuredWidth();
        tv.setText("Height: " + finalHeight + " Width: " + finalWidth);
        return true;
    }
});

it's really working...

它真的有效...

回答by canaanhhh

if you set ImageView a drawable, and the ImageView's height and width type is WRAP_CONTENT, you can get the ImageView's height and width by this even you calling from onCreate()

如果您将 ImageView 设置为可绘制对象,并且 ImageView 的高度和宽度类型为 WRAP_CONTENT,则即使您从 onCreate()

int height = imageView.getDrawable().getIntrinsicHeight();
int width = imageView.getDrawable().getIntrinsicWidth();

回答by Yogesh Rathi

Try this solution:

试试这个解决方案:

Bitmap bitmap = ((BitmapDrawable)imageView.getBackground()).getBitmap();
int width = bitmap.getWidth();
int height = bitmap.getHeight();

回答by Sreedev R

First you have to convert the image to mutablebitmap and the try to get the width and height of the image and mostly this will solve your issue..

首先,您必须将图像转换为 mutablebitmap 并尝试获取图像的宽度和高度,这通常会解决您的问题。

    Bitmap Rbitmap = Bitmap.createBitmap(bitmap).copy(
                    Config.ARGB_4444, true);

Rbitmap.getWidth();
Rbitmap.getheight();

回答by Jay Mayu

The way you make reference to the image objectis wrong. That's why you are given zero all the time. first create a bitmap object from imageview and get the width and height from it.

引用图像对象的方式是错误的。这就是为什么你总是被赋予零的原因。首先从 imageview 创建一个位图对象并从中获取宽度和高度。

When taken a image from cam I do the following and it works like a charm.

当从 cam 拍摄图像时,我执行以下操作,它就像一个魅力。

Bitmap image2 = (Bitmap) data1.getExtras().get("data");
double width = Double.valueOf(image2.getWidth());
Log.v("WIDTH", String.valueOf(width));
double height = Double.valueOf(image2.getHeight());
Log.v("height", String.valueOf(height));

Hope it helps for you.

希望对你有帮助。