create Icon Bitmap - Android Graphics

Android examples for Graphics:Icon

Description

create Icon Bitmap

Demo Code


import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import org.wolink.app.appcategory.Category.Items;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PaintDrawable;
import android.net.Uri;
import android.os.Environment;

public class Main{
    private static final Canvas sCanvas = new Canvas();
    private static final Rect sOldBounds = new Rect();
    private static int sIconWidth = -1;
    private static int sIconHeight = -1;
    private static final String[] sLauncher = new String[] {
            "com.htc.launcher.settings", "com.android.launcher2.settings",
            "com.android.launcher.settings" };
    private static Uri sLauncherUri;
    private static boolean sInitStatics = false;
    private static final String LAUNCHER_TABLE_FAVORITES = "favorites";
    public static Bitmap createIconBitmap(Drawable icon, Context context) {
        synchronized (sCanvas) { // we share the statics :-(
            if (sIconWidth == -1) {
                initStatics(context);/*from w ww  .j  a  v  a2  s  .c o m*/
            }

            int width = sIconWidth;
            int height = sIconHeight;
            Bitmap originBitmap = null;

            if (icon instanceof PaintDrawable) {
                PaintDrawable painter = (PaintDrawable) icon;
                painter.setIntrinsicWidth(width);
                painter.setIntrinsicHeight(height);
            } else if (icon instanceof BitmapDrawable) {
                // Ensure the bitmap has a density.
                BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
                originBitmap = bitmapDrawable.getBitmap();
                if (originBitmap.getDensity() == Bitmap.DENSITY_NONE) {
                    bitmapDrawable.setTargetDensity(context.getResources()
                            .getDisplayMetrics());
                }
            }

            int sourceWidth = icon.getIntrinsicWidth();
            int sourceHeight = icon.getIntrinsicHeight();

            if (sourceWidth > 0 && sourceWidth > 0) {
                // There are intrinsic sizes.
                if (width != sourceWidth && height != sourceHeight) {
                    // It's too big, scale it down.
                    final float ratio = (float) sourceWidth / sourceHeight;
                    if (sourceWidth > sourceHeight) {
                        height = (int) (width / ratio);
                    } else if (sourceHeight > sourceWidth) {
                        width = (int) (height * ratio);
                    }
                    Bitmap bm = Bitmap.createScaledBitmap(originBitmap,
                            width, height, false);
                    return bm;
                } else {
                    return originBitmap;
                }
            }

            final Bitmap bitmap = Bitmap.createBitmap(width, height,
                    Bitmap.Config.ARGB_8888);
            final Canvas canvas = sCanvas;
            canvas.setBitmap(bitmap);

            sOldBounds.set(icon.getBounds());
            icon.setBounds(0, 0, width, height);
            icon.draw(canvas);
            icon.setBounds(sOldBounds);

            return bitmap;
        }
    }
    private static void initStatics(Context context) {
        final Resources resources = context.getResources();

        sIconWidth = sIconHeight = (int) resources
                .getDimension(android.R.dimen.app_icon_size);

        final ContentResolver cr = context.getContentResolver();

        for (String launcher : sLauncher) {
            Uri uri = Uri.parse("content://" + launcher + "/"
                    + LAUNCHER_TABLE_FAVORITES);
            try {
                ContentValues values = new ContentValues();
                values.put("test_test_test", 0);
                cr.update(uri, values, "_id=?",
                        new String[] { String.valueOf(-1) });
                sLauncherUri = uri;
                break;
            } catch (IllegalArgumentException e) {
                //e.printStackTrace();
                continue;
            } catch (Exception e) {
                sLauncherUri = uri;
                break;
            }
        }

        sInitStatics = true;
    }
}

Related Tutorials