Anass Boukalane
Anass Boukalane

Reputation: 549

Generate scaled version of ImageView drawable

I want to generate a scaled version of a drawable coming from an imageView and show it as small icon for a dialog. I'm using this code but the picture keeps its initial size

Drawable drw = image.getDrawable().mutate().getConstantState().newDrawable();
                    Drawable drwScal = new ScaleDrawable(drw, 0, 0.5f, 0.5f).getDrawable();
                    drwScal.setLevel(5000);
                    new MaterialDialog.Builder(context)
                            .title(String.format(getResources().getString(R.string.summary)))
                            .icon(drwScal)
                            .content(sommaire)
                            .forceStacking(true)
                            .show();

what is wrong ?

Upvotes: 1

Views: 68

Answers (1)

Nicola Gallazzi
Nicola Gallazzi

Reputation: 8723

You can take the content of the ImageView as a Bitmap and scale the bitmap itself like this:

BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
Bitmap bitmap = drawable.getBitmap();

private static Bitmap scaleBitmap(Bitmap bitmap, float scaleFactor) {
    final int sizeX = Math.round(bitmap.getWidth() * scaleFactor);
    final int sizeY = Math.round(bitmap.getHeight() * scaleFactor);
    return Bitmap.createScaledBitmap(bitmap, sizeX, sizeY, false);
}

Upvotes: 1

Related Questions