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

后端 未结 10 1811
长情又很酷
长情又很酷 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.

    0 讨论(0)
  • 2020-11-22 17:23

    I could get image width and height by its drawable;

    int width = imgView.getDrawable().getIntrinsicWidth();
    int height = imgView.getDrawable().getIntrinsicHeight();
    
    0 讨论(0)
  • 2020-11-22 17:25

    I just set this property and now Android OS is taking care of every thing.

    android:adjustViewBounds="true"

    Use this in your layout.xml where you have planted your ImageView :D

    0 讨论(0)
  • 2020-11-22 17:25

    The reason the ImageView's dimentions are 0 is because when you are querying them, the view still haven't performed the layout and measure steps. You only told the view how it would "behave" in the layout, but it still didn't calculated where to put each view.

    How do you decide the size to give to the image view? Can't you simply use one of the scaling options natively implemented?

    0 讨论(0)
  • 2020-11-22 17:33

    The simplest way is to get the width and height of an ImageView in onWindowFocusChanged method of the activity

     @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
    
        height = mImageView.getHeight();
        width = mImageView.getWidth();
    
    }
    
    0 讨论(0)
  • 2020-11-22 17:38

    If you have created multiple images dynamically than try this one:

    // initialize your images array

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

    // create programatically and add 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);
                }
    

    // That's it. Happy Coding.

    0 讨论(0)
提交回复
热议问题