Resize Drawable in Android
Solution 1
The following worked for me:
private Drawable resize(Drawable image) {
Bitmap b = ((BitmapDrawable)image).getBitmap();
Bitmap bitmapResized = Bitmap.createScaledBitmap(b, 50, 50, false);
return new BitmapDrawable(getResources(), bitmapResized);
}
Solution 2
Here's where I ended up, thanks in part to Saad's answer:
public Drawable scaleImage (Drawable image, float scaleFactor) {
if ((image == null) || !(image instanceof BitmapDrawable)) {
return image;
}
Bitmap b = ((BitmapDrawable)image).getBitmap();
int sizeX = Math.round(image.getIntrinsicWidth() * scaleFactor);
int sizeY = Math.round(image.getIntrinsicHeight() * scaleFactor);
Bitmap bitmapResized = Bitmap.createScaledBitmap(b, sizeX, sizeY, false);
image = new BitmapDrawable(getResources(), bitmapResized);
return image;
}
Solution 3
For the resizing, this is nice and short (the code above wasn't working for me), found here:
ImageView iv = (ImageView) findViewById(R.id.imageView);
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.picture);
Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap, newWidth, newHeight, true);
iv.setImageBitmap(bMapScaled);
Solution 4
Here is a combination of the above answers as a Kotlin extension
fun Context.scaledDrawableResources(@DrawableRes id: Int, @DimenRes width: Int, @DimenRes height: Int): Drawable {
val w = resources.getDimension(width).toInt()
val h = resources.getDimension(height).toInt()
return scaledDrawable(id, w, h)
}
fun Context.scaledDrawable(@DrawableRes id: Int, width: Int, height: Int): Drawable {
val bmp = BitmapFactory.decodeResource(resources, id)
val bmpScaled = Bitmap.createScaledBitmap(bmp, width, height, false)
return BitmapDrawable(resources, bmpScaled)
}
Usage:
val scaled = context.scaledDrawableResources(R.drawable.ic_whatever, R.dimen.width, R.dimen.height)
imageView.setImageDrawable(scaled)
or
val scaled = context.scaledDrawable(R.drawable.ic_whatever, 100, 50)
imageView.setImageDrawable(scaled)
Solution 5
Maybe my solution covers the question not completely, but I needed something like a "CustomDrawable".
In other words, I want to set a logo in front of a circle shape. So I created a FrameLayout with a background (just a colored circle) and in front of this round shape I show the logo.
To resize the logo I shrink the logo by scaling - here is some code:
iv = new ImageView(mContext);
iv.setScaleX(0.75f); // <- resized by scaling
iv.setScaleY(0.75f);
// loading the drawable from a getter (replace this with any drawable)
Drawable drawable = ML.loadIcon(mContext, Integer.parseInt(icon));
iv.setImageDrawable(drawable);
// icon get's shown inside a ListView
viewHolder.mIvIcon.addView(iv);
Here is the FrameLayout which shows the icon inside ListView's row:
<FrameLayout
android:id="@+id/iv_card_icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/circle"
android:layout_marginStart="16dp"
/>
See this solution as an option / idea.
Admin
Updated on July 09, 2022Comments
-
Admin almost 2 years
I am setting a drawable for a progress dialog (
pbarDialog
) but my issue is I want to resize the drawable each time but can't figure out how.Here is some code:
Handler progressHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { // some more code case UPDATE_PBAR: pbarDialog.setIcon(mAppIcon); pbarDialog.setMessage(mPbarMsg); pbarDialog.incrementProgressBy(mIncrement+1); break; } } }; pbarDialog.show(); Thread myThread = new Thread(new Runnable() { public void run() { // some code for (int i = 0; i < mApps.size(); i++) { mAppIcon = mAdapter.getIcons().get(mApps.get(i).getPackageName()); // need to resize drawable here progressHandler.sendEmptyMessage(UPDATE_PBAR); } handler.sendEmptyMessage(DISMISS_PBAR); } }); myThread.start();