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

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

问题描述

<$p$p><$c$c>╔══════════════════════════════════════════════╗ ^ ║ImageView的╔══════════════╗║| ║║║║| ║║实际图像║║| ║║║║| ImageView的的60PX高度 ║║║║| ║║║║| ║╚══════════════╝║| ╚══════════════════════════════════════════════╝ &LT; ------------------------------------------------ &GT;                    ImageView的的90px宽度

我有一些默认的高度和宽度的图像显示,图像存储在数据库,我想根据ImageView的高度宽度缩放图像。由于我不想牛逼给默认值,因为当过我改变它的高度和宽度,我也有在code更改。

我试图获得的ImageView的高度和宽度,但0返回到我在这两种情况下。 请帮我

  INT身高=((ImageView的)v.findViewById(R.id.img_ItemView))的getHeight()。
 

这将返回我0甚至有默认的高度和宽度

解决方案

我在<一个答案href="http://stackoverflow.com/questions/4649438/how-to-get-the-dimensions-of-a-drawable-in-an-imageview/4649842">this问题可以帮助你:

  INT finalHeight,finalWidth;
最后ImageView的IV =(ImageView的)findViewById(R.id.scaled_image);
最后的TextView电视=(TextView中)findViewById(R.id.size_label);
ViewTreeObserver VTO = iv.getViewTreeObserver();
vto.addOn preDrawListener(新ViewTreeObserver.On preDrawListener(){
    在preDraw公共布尔(){
        iv.getViewTreeObserver()removeOn preDrawListener(本)。
        finalHeight = iv.getMeasuredHeight();
        finalWidth = iv.getMeasuredWidth();
        tv.setText(高:+ finalHeight +宽:+ finalWidth);
        返回true;
    }
});
 

您可以再从内上preDraw添加您的图像缩放work()方法。

╔══════════════════════════════════════════════╗   ^
║ ImageView    ╔══════════════╗                ║   |
║              ║              ║                ║   |
║              ║ Actual image ║                ║   |
║              ║              ║                ║   |60px height of ImageView
║              ║              ║                ║   |
║              ║              ║                ║   |
║              ╚══════════════╝                ║   |
╚══════════════════════════════════════════════╝   
<------------------------------------------------>
                   90px width of 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 t give default values because when ever I change its height and width I also have to change it in code.

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

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

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

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

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

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