简体   繁体   中英

How to get width and height of imageview ?

I want to get width and height of ImageView.

myimgbg = (ImageView) findViewById(R.id.myimggb);
myimgbg.setBackgroundResource(R.drawable.dudu_1);

The other answers demonstrate how to get the dimensions, but you are trying to do it too early, before the layout has been measured. This is how you do this but I am pretty sure you should never need to do this - there is almost always a better way that does not require you to know the pixel dimensions of anything.

myimgbg.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        myimgbg.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        int width = myimgbg.getWidth();
    }
});

Just to emphasize again, if you are doing something that requires knowing the exact pixel size of a view, you should probably take a step back and reconsider how to do what you want.

Check this :

image_view.getLayoutParams().height 
image_view.getLayoutParams().width 

Hope this helps.

尝试myimgbg.getWidth()和myimgbg.getHeight()

This is simple way to get width and height from imageview

ImageView img = (ImageView) findViewById(R.id.img);
Log.d(TAG, "width : " + img.getWidth());

Just try, after your view is drawn

   yourView.getHeight()
    yourView.getWidth()

or

LayoutParams params=yourView.getLayoutParams();
params.height //is your views height

Try this:

int width=image.getWidth();
int Height=image.getHeight();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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