How to Get The Dimensions of a Drawable in an Imageview

How to get the Dimensions of a Drawable in an ImageView?

Just tried this out and it works for me:

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() {
// Remove after the first run so it doesn't fire forever
iv.getViewTreeObserver().removeOnPreDrawListener(this);
finalHeight = iv.getMeasuredHeight();
finalWidth = iv.getMeasuredWidth();
tv.setText("Height: " + finalHeight + " Width: " + finalWidth);
return true;
}
});

The ViewTreeObserver will let you monitor the layout just prior to drawing it (i.e. everything has been measured already) and from here you can get the scaled measurements from the ImageView.

How can I get the real size of drawable in android?

First you have to convert drawable into bitmap then you can get image height and width.

try below code:-

BitmapDrawable bd=(BitmapDrawable) this.getResources().getDrawable(imageID); 
double imageHeight = bd.getBitmap().getHeight();
double imageWidth = bd.getBitmap().getWidth();

Get the displayed size of an image inside an ImageView

the following will work:

ih=imageView.getMeasuredHeight();//height of imageView
iw=imageView.getMeasuredWidth();//width of imageView
iH=imageView.getDrawable().getIntrinsicHeight();//original height of underlying image
iW=imageView.getDrawable().getIntrinsicWidth();//original width of underlying image

if (ih/iH<=iw/iW) iw=iW*ih/iH;//rescaled width of image within ImageView
else ih= iH*iw/iW;//rescaled height of image within ImageView

(iw x ih) now represents the actual rescaled (width x height) for the image within the view (in other words the displayed size of the image)


EDIT: I think a nicer way to write the above answer (and one that works with ints) :

final int actualHeight, actualWidth;
final int imageViewHeight = imageView.getHeight(), imageViewWidth = imageView.getWidth();
final int bitmapHeight = ..., bitmapWidth = ...;
if (imageViewHeight * bitmapWidth <= imageViewWidth * bitmapHeight) {
actualWidth = bitmapWidth * imageViewHeight / bitmapHeight;
actualHeight = imageViewHeight;
} else {
actualHeight = bitmapHeight * imageViewWidth / bitmapWidth;
actualWidth = imageViewWidth;
}

return new Point(actualWidth,actualHeight);

How to get the width and height of an Image View in android?

Where you calling getWidth() and getHeight() on ImageView? If you calling from onCreate() in activity, it won't work. You need to wait for activity window to attached and then call getWidth() and getHeight() on ImageView. You can try calling getWidth() and getHeight() from onWindowFocusChanged() method of your activity.

@Override
public void onWindowFocusChanged(boolean hasFocus){
int width=imageView.getWidth();
int height=imageView.getHeight();
}

Getting image dimensions in pixels in ImageView

This is the solution found in a web site.

I needed to change the Option value when I decode the resource not to scale the original image. I had to use the three parameters for the decodeResource function, not two.

Of course, the third parameter was Options specifying the original bitmap not to be scaled. So now I can get 800 when I call bitmap's getWidth() function.

                Resources res = getResources();
int id = R.drawable.map10;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
// options.inSampleSize = 3;
Bitmap bb = BitmapFactory.decodeResource(res, id, options);

float fwidth = bb.getWidth();

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

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.



Related Topics



Leave a reply



Submit