1

I'm looking for a way on how can I convert a TransitionDrawable to Drawable or To bitmap to be able to save the Image.

I have been looking for a while and I tried a lot methods and no clue.

here a code what I have tried :

TransitionDrawable drawable = (TransitionDrawable) profil.getDrawable();
                        Drawable drawalb =  drawable.mutate();
                        final Bitmap bitmap = ((BitmapDrawable) drawalb).getBitmap();
                        ByteArrayOutputStream stream = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                        byte[] image = stream.toByteArray();
Nadador Base
  • 99
  • 10

2 Answers2

0

I wantn't able to do that, but I have found another clue, I was able to get the bitmap directly before converting it to TransitionDrawable.

Nadador Base
  • 99
  • 10
0

A transitionDrawable is an array of drawable (Defnition).

so;

1.Get the drawable you want, depending on its index

2.covert it to a bitmap (As cited here by Andre)

 TransitionDrawable Tdrawable =(TransitionDrawable) imageView.getDrawable();
 Drawable mDrawable =  Tdrawable.getDrawable;
 bitmap = drawableToBitmap(mDrawable);

public static Bitmap drawableToBitmap (Drawable drawable) {
    Bitmap bitmap = null;

    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        if(bitmapDrawable.getBitmap() != null) {
            return bitmapDrawable.getBitmap();
        }
    }

    if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
    } else {
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Don Louis
  • 61
  • 1
  • 7