Home > Android, Development > Android scale drawable

Android scale drawable

...
Drawable scaledDrawable = getScaledDrawable(scaleLevel, scaleLevel, R.drawable.icon);

private Drawable getScaledDrawable(int newWidth, int newHeight, int id) {

Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), id);

Matrix matrix = new Matrix();
float scaleWidth = ((float) newWidth) / bitmap.getWidth();
float scaleHeight = ((float) newHeight) / bitmap.getHeight();
matrix.postScale(scaleWidth, scaleHeight);
matrix.postRotate(0);
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

return new BitmapDrawable(this.getResources(), scaledBitmap);
}
...
Advertisements
Categories: Android, Development
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: