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

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


        
相关标签:
10条回答
  • 2020-11-22 17:40

    your xml file :

     <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"/>
    

    your java file:

    ImageView imageView = (ImageView)findViewById(R.id.imageView);
         int width = imageView.getDrawable().getIntrinsicWidth();
         int   height = imageView.getDrawable().getIntrinsicHeight();
    
    0 讨论(0)
  • 2020-11-22 17:40

    my friend by this u are not getting height of image stored in db.but you are getting view height.for getting height of image u have to create bitmap from db,s image.and than u can fetch height and width of imageview

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

    Post to the UI thread works for me.

    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();
    
                }
    });
    
    0 讨论(0)
  • 2020-11-22 17:49

    I think you can let the Android OS take care of this for you. Set the scale type on the ImageView to fitXY and the image it displays will be sized to fit the current size of the view.

    <ImageView 
        android:layout_width="90px" 
        android:layout_height="60px"
        android:scaleType="fitXY" />
    
    0 讨论(0)
提交回复
热议问题