How to get the size of bitmap after displaying it in ImageView

前端 未结 1 1272
攒了一身酷
攒了一身酷 2020-12-19 05:57

I have a imageview



        
相关标签:
1条回答
  • 2020-12-19 06:31

    After you display a Bitmap in a ImageView, The ImageView will create a BitmapDrawable object to draw it in ImageView's Canvas. So you can invoke ImageView.getDrawable() method to get the reference of the BitmapDrawable, and get the Bounds by invoke Drawable.getBounds(Rect rect) method. through the bounds, you can compute the width and height of the Bitmap drawn in ImageView

    Drawable drawable = ImageView.getDrawable();
    //you should call after the bitmap drawn
    Rect bounds = drawable.getBounds();
    int width = bounds.width();
    int height = bounds.height();
    int bitmapWidth = drawable.getIntrinsicWidth(); //this is the bitmap's width
    int bitmapHeight = drawable.getIntrinsicHeight(); //this is the bitmap's height
    
    0 讨论(0)
提交回复
热议问题