convert ColorDrawable To Bitmap - Android Graphics

Android examples for Graphics:Drawable

Description

convert ColorDrawable To Bitmap

Demo Code


//package com.java2s;

import android.graphics.Bitmap;
import android.graphics.Canvas;

import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;

public class Main {
    public static Bitmap convertToBitmap(ColorDrawable drawable,
            int widthPixels, int heightPixels) {
        Bitmap mutableBitmap = Bitmap.createBitmap(widthPixels,
                heightPixels, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(mutableBitmap);
        drawable.setBounds(0, 0, widthPixels, heightPixels);
        drawable.draw(canvas);//from w  w w  .ja v a 2  s. c o m
        return mutableBitmap;
    }

    public static Bitmap convertToBitmap(Drawable drawable,
            int widthPixels, int heightPixels) {
        Bitmap mutableBitmap = Bitmap.createBitmap(widthPixels,
                heightPixels, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(mutableBitmap);
        drawable.setBounds(0, 0, widthPixels, heightPixels);
        drawable.draw(canvas);
        return mutableBitmap;
    }
}

Related Tutorials