Android4.0为all app图标加背景图

来源:互联网 发布:淘宝氛围图是什么样的 编辑:程序博客网 时间:2024/06/05 08:51

修改位置:Launcher2/src/com/android/launcher2/Utilities.java

    /**     * Returns a bitmap suitable for the all apps view.     */    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);            }                        //增加背景图片int model = context.getSharedPreferences("models",Context.MODE_PRIVATE).getInt("MODEL", 0);if (model > 0) {TypedArray iconbacks = context.getResources().obtainTypedArray(R.array.iconbacks);Bitmap backBitmap = BitmapFactory.decodeResource(context.getResources(),iconbacks.getResourceId(model, R.drawable.app_back_1));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);}}            sOldBounds.set(icon.getBounds());            icon.setBounds(left, top, left+width, top+height);            icon.draw(canvas);            icon.setBounds(sOldBounds);            canvas.setBitmap(null);            return bitmap;        }    }


原创粉丝点击