How to get the width and height of an android.widget.ImageView?

后端 未结 10 1901
长情又很酷
长情又很酷 2020-11-22 16:53
╔══════════════════════════════════════════════╗   ^
║ ImageView    ╔══════════════╗                ║   |
║              ║              ║                ║   |
║              


        
10条回答
  •  日久生厌
    2020-11-22 17:23

    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.

提交回复
热议问题