Adarsh Adarsh - 2 months ago 14
Android Question

set width and height of a drawable object in runtime process in android

Hi I have a

drawable
object in which content is loaded at
runtime
process.I want to get its actual width and height so I used this code :

int x=drawable.getIntrinsicWidth();
int y=drawable.getIntrinsicHeight();


Ans the result is x as 72 and y as 72.Now I want to set my
drawable
object width and height as 72.How can I do that.Is there any solution.

Answer
Drawable drawable = getResources().getDrawable(R.drawable.yourAwesomeDrawable);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
// Scale it to 100 x 100
Drawable newDrawable = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, 100, 100, true));

You could also try setBounds(), but I am not quite sure..

Comments