如何将Transition Drawable转换为Drawable或Bitmap

时间:2015-02-05 14:22:33

标签: android bitmap android-drawable

我正在寻找一种方法,可以将TransitionDrawable转换为Drawable或To位图,以便能够保存图像。

我一直在寻找一段时间,我尝试了很多方法而且没有任何线索。

这是我尝试的代码:

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

2 个答案:

答案 0 :(得分:0)

我无法做到这一点,但我找到了另一条线索,我可以在将其转换为TransitionDrawable之前直接获取位图。

答案 1 :(得分:0)

transitionDrawable是可绘制(Defnition)的数组。

所以;

1。根据所需索引获取所需的绘图对象

2。将其隐藏到位图(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;
}