如何获取 android.widget.ImageView 的宽度和高度? [英] How to get the width and height of an android.widget.ImageView?

查看:55
本文介绍了如何获取 android.widget.ImageView 的宽度和高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

╔══════════════════════════════════════════════╗   ^
║ ImageView    ╔══════════════╗                ║   |
║              ║              ║                ║   |
║              ║ Actual image ║                ║   |
║              ║              ║                ║   |60px height of ImageView
║              ║              ║                ║   |
║              ║              ║                ║   |
║              ╚══════════════╝                ║   |
╚══════════════════════════════════════════════╝   
<------------------------------------------------>
                   90px width of ImageView

我有一个默认高度和宽度的图像视图,图像存储在 db 中,我想根据 Imageview 的高度宽度缩放图像.因为我不希望它给出默认值,因为每当我改变它的高度和宽度时,我也必须在代码中改变它.

I have an image view with some default height and width, images are stored in db and I want to scale Image according to Imageview height width. As I don't want it give default values because when ever I change it's height and width I also have to change it in code.

我正在尝试获取 ImageView 的高度和宽度,但在这两种情况下都返回 0.

I am trying to get the height and width of ImageView but 0 is returned to me in both cases.

int height = ((ImageView) v.findViewById(R.id.img_ItemView)).getHeight();

即使它具有默认的高度和宽度,这也会返回 0

this returns me 0 even it has default height and width

推荐答案

我对 这个问题可能对你有帮助:

My answer on this question might help you:

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

然后您可以从 onPreDraw() 方法中添加您的图像缩放工作.

You can then add your image scaling work from within the onPreDraw() method.

这篇关于如何获取 android.widget.ImageView 的宽度和高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆