Trying to get image size in ImageView

I am trying to get the actual size of the image displayed in the image view. In fact, my image is larger than the screen, and the image is viewing the image to change it. I am looking for a new size.

I tried to override the onDraw ImageView method in the user view, but I am 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 hints?

+44
android imageview ondraw
04 Oct '10 at
source share
14 answers

None of the answers here actually answer the question:

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

Namely:

  • Using ImageView.getDrawable().getInstrinsicWidth() and getIntrinsicHeight() will return the original measurements.
  • Getting Drawable through ImageView.getDrawable() and dropping it to BitmapDrawable , then using BitmapDrawable.getBitmap().getWidth() and getHeight() also return the original image and its size.

The only way to get the dimensions of the actual displayed image is to extract and use the Matrix transform used to display the image as shown. This must be done after the measurement step, and here the example shows that it is called in Override 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 convert a Matrix image from code in general (for example, to Activity ), the ImageView.getImageMatrix() function - for example. myImageView.getImageMatrix()

+98
Mar 21 '13 at 3:16
source share

I extended BT's answer to create a static method from it, and include the image left and top in 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; } 
+26
Nov 14 '14 at 13:19
source share

I found that WarrenFaith suggested using setAdjustViewBounds, but I had to change ImageView_ layout / layout_height Image_width to 'wrap_content' (using 'match_parent', setAdjustViewBounds did nothing). To get the height / width / gravity behavior that I wanted, I had to wrap the ImageView in a FrameLayout:

 <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:scaleType="fitCenter" android:adjustViewBounds="true" /> </FrameLayout> 

After that, the ImageView sizes (returned by getWidth () and getHeight ()) correspond to the size of the image.

+9
Mar 07 '13 at 3:13
source share

Try

 ImageView.getDrawable().getIntrinsicHeight() ImageView.getDrawable().getIntrinsicWidth() 
+6
Oct 23 '10 at 19:18
source share

I pass by, but I hope that this still helps. I proceed from the assumption that you are talking about raster images in your image.

what you want to understand is the difference between

bmpBack.getWidth() -> this gives you the size of your bitmap as well as bmpBack.getScaledWidth(canvas) ; -> this will give you the size of the bitmap displayed on the screen.

I never used ImageView because the relative display was driving me crazy, so in the end I just overridden onDraw and made my canvas look very much like opengl.

I think this is your problem.

greetings

Jason

+1
Oct 26 '10 at 6:43
source share

You can use imageview viewtreeobserver and addonDrawListener.

 ViewTreeObserver vto = imageView.getViewTreeObserver(); vto.addOnDrawListener(new ViewTreeObserver.OnDrawListener() { @Override public void onDraw() { 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); } }); 
+1
Jun 21 '18 at 5:05
source share

try overriding onMeasure(int widthMeasureSpec, int heightMeasureSpec) instead of onSizeChanged.

0
Oct 04 2018-10-10
source share

If you understand correctly, you will need your ImageView dimension to scale the image accordingly. I did this with a special class, where I override the onMeasure() call to get the width and height.

 class LandImageView extends ImageView{ public LandImageView (Context context, AttributeSet attrs) { super (context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); final int width = MeasureSpec.getSize(widthMeasureSpec); final int height = MeasureSpec.getSize(heightMeasureSpec); Log.v("", String.format("w %dh %d", width, height)); // width and height are the dimensions for your image // you should remember the values to scale your image later on this.setMeasuredDimension(width, height ); }} 

In onMeasure, you get the width and height of the image to fit your presentation.

You can use LandImageView in your layout as follows:

 <my.package.LandImageView ... > 
0
Oct 24 '10 at 10:50
source share

Try loading your resource using BitmapFactory.decodeResource (res, int id, BitmapFactory.Options opts resources) with the class BitmapFactory.Options (), BitmapFactory.Options() has a flag called "inJustDecodeBounds" and gives only the sizes of the resources.

Hope this helped.

0
24 Oct '10 at 12:10
source share
 public class images extends Activity { ImageView i1; LinearLayout l1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); i1=(ImageView)findViewById(R.id.iv); l1 = new LinearLayout(this); ImageView i = new ImageView(this); ImageView i1 = new ImageView(this); i.setImageResource(R.drawable. imagename.........); l1.addView(i); l1.addView(i1); setContentView(l1); } } 

first add the images to the ur ....... resource folder and in the xml file create an image ...

0
Nov 10 '10 at 9:02
source share

I was looking for a solution to set the image view size to a scaled image to prevent blank space from above / below or left / right (since the view size does not change to fit the scaled image).

What I found to do the trick is to use the mImageView.setAdjustViewBounds(true); method mImageView.setAdjustViewBounds(true); which results in the correct layout size. I don't have a scaled image size, but I got the result I was looking for ... just if someone needs it ...

0
Mar 09 2018-11-11T00:
source share

I think you need the image size visible on the screen, for this you just need to do this:

 @Override public void onWindowFocusChanged(boolean hasWindowFocus) { super.onWindowFocusChanged(hasWindowFocus); initialWidth = this.getMeasuredWidth(); initialHeight = this.getMeasuredHeight(); } 

And take a look at the documentation for such methods as: getWidht() , getHeight() , getMeasuredWidth() , getMeasuredHeight() , etc. You will find out what he is doing to give you this size.

EDIT: If you want the actual width and height image to load in the imageView . You can change method calls from getMeasuredWidth() to getIntrinsicWidth() and getMeasuredHeight() to getIntrinsicHeight() as follows:

  Drawable drawable = getDrawable(); actualWidth = drawable.getIntrinsicWidth(); actualHeight = drawable.getIntrinsicHeight(); 
0
Dec 25 '17 at 6:26
source share

You can simply use this code (in action):

 @Override public void onWindowFocusChanged(boolean hasWindowFocus) { super.onWindowFocusChanged(hasWindowFocus); final ImageView imageView = findViewById(R.id.imageView); int width = imageView.getWidth(), height = imageView.getHeight(); } 

In case the image reaches the ends of the imageView.

0
Jul 25 '19 at 1:01
source share

What about ImageView.getBackground () or ImageView.getDrawable () and then Drawable.getBounds ()?

-one
Oct 21 '10 at 20:25
source share



All Articles