Resize Drawable in Android Resize Drawable in Android android android

Resize Drawable in Android


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);}


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;}


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);