Reputation: 1293
hi there I have an ImageView
that contains a Drawable
, I want to know the real size of this Drawable
( width and height ) . how can I get the real size please?
I tried that but its not working:
final ImageView img = (ImageView) findViewById(R.id.imagePlan);
img.setImageResource(R.drawable.gratuit);
System.out.println("w=="+img.getDrawable().getIntrinsicWidth()+" h=="+img.getDrawable().getIntrinsicHeight());
thanks
Upvotes: 1
Views: 7777
Reputation: 3876
In case of VectorDrawable
you can use intrinsicHeight
and intrinsicWidth
So the accepted answer will be
val vd = this.resources.getDrawable(cardInput.card!!.brand.icon) as VectorDrawable
val imageHeight = vd.intrinsicHeight
val imageWidth = vd.intrinsicWidth
Upvotes: 0
Reputation: 5176
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
Bitmap bmp = BitmapFactory.decodeResource(activity.getResources(), R.drawable.sample_image, options);
int w = bmp.getWidth();
int h = bmp.getHeight();
Upvotes: 1
Reputation: 157487
You are probably getting zero for both width and height, because, the drawable has not been you measured,
img.post(new Runnable() {
@Override
public void run() {
System.out.println("w=="+img.getDrawable().getIntrinsicWidth()+" h=="+img.getDrawable().getIntrinsicHeight());
}
})
Upvotes: 1
Reputation: 38439
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();
Upvotes: 7
Reputation: 14590
By decoding resource into bitmap you can get the real width and height..
final ImageView img = (ImageView) findViewById(R.id.gratuit);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
img.setImageBitmap(bitmap);
Upvotes: 0