给launcher上的应用图标ICON加上背景框

launcher上的应用图标ICON,是在Utilities.java里的static Bitmap createIconBitmap(Drawable icon, Context context)这个函数里实现的,如果要给icon加上对应的图标,可以如下做:


    //add by xuhui for app icon bg
    static final int[] APP_ICON_BG = {
        R.drawable.icon_bg_01,
        R.drawable.icon_bg_02,
        R.drawable.icon_bg_03,
        R.drawable.icon_bg_04,
        R.drawable.icon_bg_05
    };//add end 

   static Bitmap createIconBitmap(Drawable icon, Context context) {
        synchronized (sCanvas) { // we share the statics :-(
            if (sIconWidth == -1) {
                initStatics(context);
            }

            int width = sIconWidth;
            int height = sIconHeight;

            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;
                Bitmap bitmap = bitmapDrawable.getBitmap();
                if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
                    bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
                }
            }
            int sourceWidth = icon.getIntrinsicWidth();
            int sourceHeight = icon.getIntrinsicHeight();
            if (sourceWidth > 0 && sourceHeight > 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);
                    }
                } else if (sourceWidth < width && sourceHeight < height) {
                    // Don't scale up the icon
                    width = sourceWidth;
                    height = sourceHeight;
                }
            }

            // no intrinsic size --> use default size
            int textureWidth = sIconTextureWidth;
            int textureHeight = sIconTextureHeight;

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

            final int left = (textureWidth-width) / 2;
            final int top = (textureHeight-height) / 2;

            @SuppressWarnings("all") // suppress dead code warning
            final boolean debug = false;
            if (debug) {
                // draw a big box for the icon for debugging
                canvas.drawColor(sColors[sColorIndex]);
                if (++sColorIndex >= sColors.length) sColorIndex = 0;
                Paint debugPaint = new Paint();
                debugPaint.setColor(0xffcccc00);
                canvas.drawRect(left, top, left+width, top+height, debugPaint);
            }
            //增加图标背景图片,add by xuhui
            if (true) 
            {
                int index = new Random(System.currentTimeMillis()).nextInt(5);
                Bitmap backBitmap = BitmapFactory.decodeResource(context.getResources(),APP_ICON_BG[index]);
                int backWidth = backBitmap.getWidth();
                int backHeight = backBitmap.getHeight();
                if(backWidth != sIconWidth || backHeight != sIconHeight)
                {
                    Matrix matrix = new Matrix();
                    matrix.postScale((float)sIconWidth/backWidth, (float)sIconHeight/backHeight);
                    canvas.drawBitmap(Bitmap.createBitmap(backBitmap, 0, 0, backWidth, backHeight, matrix, true),0.0f, 0.0f, null);
                }else
                {
                    canvas.drawBitmap(backBitmap, 0.0f, 0.0f, null);
                }
            }     //add end           

            sOldBounds.set(icon.getBounds());
            icon.setBounds(left, top, left+width, top+height);
            icon.draw(canvas);
            icon.setBounds(sOldBounds);
            canvas.setBitmap(null);

            return bitmap;
        }
    }


猜你喜欢

转载自blog.csdn.net/xuhui_7810/article/details/36901815