Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to get the display size of an image in an ImageView

I'm trying to get the real size of an image displayed in an image view. Actually my image is larger than the screen and the imageview is resizing the image to diplay it. I'm looking for this new size.

I've tried to override the onDraw method of the ImageView in a custom view but I'm not getting the correct height and width...

public class LandImageView extends ImageView {     public LandImageView( Context context )     {         super( context );     }      public LandImageView(Context context, AttributeSet attrs)     {         super(context, attrs);     }      public LandImageView(Context context, AttributeSet attrs, int defStyle)     {         super(context, attrs, defStyle);     }      @Override     protected void onDraw( Canvas canvas )     {         super.onDraw( canvas );          int test = this.getWidth();         int test2 = this.getHeight();     }      @Override     protected void onSizeChanged(int w, int h, int oldw, int oldh)     {         super.onSizeChanged(w, h, oldw, oldh);     } } 

Do you have any clues ?

like image 419
Guillaume Ménant Avatar asked Oct 04 '10 12:10

Guillaume Ménant


People also ask

How do I make an image fit in ImageView?

try adding android:scaleType="fitXY" to your ImageView . This will modify the aspect ratio if the original image is not squared. fitXY will almost always change the aspect ratio of the image.

How do I resize an image in ImageView to keep the aspect ratio?

However, make sure you're setting the image to the ImageView using android:src="..." rather than android:background="..." . src= makes it scale the image maintaining aspect ratio, but background= makes it scale and distort the image to make it fit exactly to the size of the ImageView.


2 Answers

None of the answers here actually answer the question:

From a Bitmap of any size displayed by an ImageView, find the actual dimensions of the displayed image as opposed to the dimensions of the supplied Bitmap.

Namely:

  • Using ImageView.getDrawable().getInstrinsicWidth() and getIntrinsicHeight() will both return the original dimensions.
  • Getting the Drawable through ImageView.getDrawable() and casting it to a BitmapDrawable, then using BitmapDrawable.getBitmap().getWidth() and getHeight() also returns the original image and its dimensions.

The only way to get the actual dimensions of the displayed image is by extracting and using the transformation Matrix used to display the image as it is shown. This must be done after the measuring stage and the example here shows it called in an Override of onMeasure() for a custom ImageView:

public class SizeAwareImageView extends ImageView {      public SizeAwareImageView(Context context) {         super(context);     }      @Override     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {         super.onMeasure(widthMeasureSpec, heightMeasureSpec);          // Get image matrix values and place them in an array         float[] f = new float[9];         getImageMatrix().getValues(f);          // Extract the scale values using the constants (if aspect ratio maintained, scaleX == scaleY)         final float scaleX = f[Matrix.MSCALE_X];         final float scaleY = f[Matrix.MSCALE_Y];          // Get the drawable (could also get the bitmap behind the drawable and getWidth/getHeight)         final Drawable d = getDrawable();         final int origW = d.getIntrinsicWidth();         final int origH = d.getIntrinsicHeight();          // Calculate the actual dimensions         final int actW = Math.round(origW * scaleX);         final int actH = Math.round(origH * scaleY);          Log.e("DBG", "["+origW+","+origH+"] -> ["+actW+","+actH+"] & scales: x="+scaleX+" y="+scaleY);     }  }   

Note: To get the image transformation Matrix from code in general (like in an Activity), the function is ImageView.getImageMatrix() - e.g. myImageView.getImageMatrix()

like image 200
btalb Avatar answered Sep 24 '22 18:09

btalb


I extended B T's answer to produce a static method from it, and to include image left and top positions into the ImageView :

/**  * Returns the bitmap position inside an imageView.  * @param imageView source ImageView  * @return 0: left, 1: top, 2: width, 3: height  */ public static int[] getBitmapPositionInsideImageView(ImageView imageView) {     int[] ret = new int[4];      if (imageView == null || imageView.getDrawable() == null)         return ret;      // Get image dimensions     // Get image matrix values and place them in an array     float[] f = new float[9];     imageView.getImageMatrix().getValues(f);      // Extract the scale values using the constants (if aspect ratio maintained, scaleX == scaleY)     final float scaleX = f[Matrix.MSCALE_X];     final float scaleY = f[Matrix.MSCALE_Y];      // Get the drawable (could also get the bitmap behind the drawable and getWidth/getHeight)     final Drawable d = imageView.getDrawable();     final int origW = d.getIntrinsicWidth();     final int origH = d.getIntrinsicHeight();      // Calculate the actual dimensions     final int actW = Math.round(origW * scaleX);     final int actH = Math.round(origH * scaleY);      ret[2] = actW;     ret[3] = actH;      // Get image position     // We assume that the image is centered into ImageView     int imgViewW = imageView.getWidth();     int imgViewH = imageView.getHeight();      int top = (int) (imgViewH - actH)/2;     int left = (int) (imgViewW - actW)/2;      ret[0] = left;     ret[1] = top;      return ret; } 
like image 35
Quentin S. Avatar answered Sep 25 '22 18:09

Quentin S.