首页 文章

如何获得android.widget.ImageView的宽度和高度?

提问于
浏览
265
╔══════════════════════════════════════════════╗   ^
║ ImageView    ╔══════════════╗                ║   |
║              ║              ║                ║   |
║              ║ Actual image ║                ║   |
║              ║              ║                ║   |60px height of ImageView
║              ║              ║                ║   |
║              ║              ║                ║   |
║              ╚══════════════╝                ║   |
╚══════════════════════════════════════════════╝   
<------------------------------------------------>
                   90px width of ImageView

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

我试图获得ImageView的高度和宽度,但在两种情况下都返回0 .

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

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

10 回答

  • 4

    ImageView的维度为0的原因是因为当您查询它们时,视图仍然没有执行布局和测量步骤 . 您只告诉视图它在布局中如何“行为”,但它仍然没有计算放置每个视图的位置 .

    你如何决定给图像视图的大小?难道你不能简单地使用本机实现的缩放选项之一吗?

  • 208

    最简单的方法是在活动的onWindowFocusChanged方法中获取ImageView的宽度和高度

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
    
        height = mImageView.getHeight();
        width = mImageView.getWidth();
    
    }
    
  • 25

    你的xml文件:

    <ImageView android:id="@+id/imageView"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:src="@drawable/image"
                   android:scaleType="fitXY"
                   android:adjustViewBounds="true"/>
    

    你的java文件:

    ImageView imageView = (ImageView)findViewById(R.id.imageView);
         int width = imageView.getDrawable().getIntrinsicWidth();
         int   height = imageView.getDrawable().getIntrinsicHeight();
    
  • 4

    如果你创建了 multiple images dynamically 而不是尝试这个:

    // initialize 你的图像数组

    private ImageView myImages[] = new ImageView[your_array_length];
    

    // create programatically 并添加 to parent view

    for (int i = 0; i < your_array_length; i++) {
                    myImages[i] = new ImageView(this);
                    myImages[i].setId(i + 1);
                    myImages[i].setBackgroundResource(your_array[i]);
                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                            frontWidth[i], frontHeight[i]);
                    ((MarginLayoutParams) params).setMargins(frontX_axis[i],
                            frontY_axis[i], 0, 0);
                    myImages[i].setAdjustViewBounds(true);
                    myImages[i].setLayoutParams(params);
    
                    if (getIntent() != null && i != your_array,length) {
                        final int j = i;
                        myImages[j].getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                            public boolean onPreDraw() {
                                myImages[j].getViewTreeObserver().removeOnPreDrawListener(this);
                        finalHeight = myImages[j].getMeasuredHeight();
                            finalWidth = myImages[j].getMeasuredWidth();
                        your_textview.setText("Height: " + finalHeight + " Width: " + finalWidth);
                                return true;
                            }
                        });
                    }
                    your_parent_layout.addView(myImages[i], params);
                }
    

    // 而已 . 快乐的编码 .

  • 2

    我这样的朋友你没有得到存储在db中的图像高度 . 但是你得到的视图高度 . 为了获得图像的高度你必须从db,s image创建位图 . 而且你可以获取imageview的高度和宽度

  • 13

    发布到UI线程适合我 .

    final ImageView iv = (ImageView)findViewById(R.id.scaled_image);
    
    iv.post(new Runnable() {
                @Override
                public void run() {
                    int width = iv.getMeasuredWidth();
                    int height = iv.getMeasuredHeight();
    
                }
    });
    
  • 1

    我想你可以让Android操作系统为你解决这个问题 . 将ImageView上的比例类型设置为 fitXY ,其显示的图像将调整大小以适合视图的当前大小 .

    <ImageView 
        android:layout_width="90px" 
        android:layout_height="60px"
        android:scaleType="fitXY" />
    
  • 18

    我在this question的回答可能会对你有所帮助:

    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()方法中添加图像缩放工作 .

  • 10

    我可以通过它的drawable获得图像宽度和高度;

    int width = imgView.getDrawable().getIntrinsicWidth();
    int height = imgView.getDrawable().getIntrinsicHeight();
    
  • -2

    我只是设置了这个属性,现在Android操作系统正在处理所有事情 .

    android:adjustViewBounds =“true”

    在您种植ImageView的layout.xml中使用它:D

相关问题