简体   繁体   English

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

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

In my code I have an Image View in my XML layout and I keep changing the source image for this in my code. 在我的代码中,我的XML布局中有一个Image View ,并且我在代码中不断更改源图像。 Now I want to know the width and height of the generated image each time. 现在我想知道每次生成的图像的宽度和高度。

I tried using getWidth() , getHeight(), getMeasuredWidth(), and getMeasuredHeight() , but they all return 0. 我尝试使用getWidth()getHeight(), getMeasuredWidth(),getMeasuredHeight() ,但它们都返回0。

How can I get this? 我怎么能得到这个?

Where you calling getWidth() and getHeight() on ImageView? 你在ImageView上调用getWidth()getHeight() If you calling from onCreate() in activity, it won't work. 如果从活动中的onCreate()调用,它将无法工作。 You need to wait for activity window to attached and then call getWidth() and getHeight() on ImageView. 您需要等待附加活动窗口,然后在ImageView上调用getWidth() and getHeight() You can try calling getWidth() and getHeight() from onWindowFocusChanged() method of your activity. 您可以尝试从活动的onWindowFocusChanged()方法调用getWidth() and getHeight()

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

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;
        }
    });

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... 它确实在工作......

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设置为drawable,并且ImageView的高度和宽度类型是WRAP_CONTENT,那么即使你从onCreate()调用,也可以获得ImageView的高度和宽度

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

The way you make reference to the image object is 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. 从凸轮拍摄图像时,我会执行以下操作,它就像一个魅力。

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. 希望它对你有所帮助。

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();

Try this solution: 尝试此解决方案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM