convert Icon To Bitmap - Android Graphics

Android examples for Graphics:Drawable

Description

convert Icon To Bitmap

Demo Code


//package com.java2s;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

public class Main {
    private static int sIconSize = -1;

    public static Bitmap convertIconToBitmap(Context context,
            Drawable drawable) {/*  ww  w  .j  a v a2  s.c  om*/

        if (sIconSize == -1) {
            sIconSize = context.getResources().getDimensionPixelSize(
                    android.R.dimen.app_icon_size);
        }

        return toBitmap(drawable, sIconSize, sIconSize);
    }

    private static Bitmap toBitmap(Drawable drawable, int width, int height) {

        Bitmap bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        Canvas c = new Canvas(bmp);
        drawable.setBounds(new Rect(0, 0, width, height));
        drawable.draw(c);

        return bmp;
    }
}

Related Tutorials