android Launcher4 给第三方应用图标添加底图

最近改系统Launcher 遇到不少头疼的问题,Launcher中,图标大小不统一非常影响体验,

也不美观,市场上的launcher多数做法都是给第三方应用添加底图,源码中自带了用来调试用的

给图标添加一个方框的代码,是在是看不下去...(个人审美观的问题...不喜勿喷...)

话不多说,上源码...

package/apps/Launcher4/src/com/android/launcher4/Utilities.java

//在源码Utilities.java的createIconBitmap(Drawable icon, Context context, boolean isCustomIcon)方法中添加

public static Bitmap createIconBitmap(Drawable icon, Context context, boolean isCustomIcon) {
        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) {
                // Scale the icon proportionally to the icon dimensions
                final float ratio = (float) sourceWidth / sourceHeight;
                if (sourceWidth > sourceHeight) {
                    height = (int) (width / ratio);
                } else if (sourceHeight > sourceWidth) {
                    width = (int) (height * ratio);
                }
            }


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


            textureWidth += isCustomIcon ? 0 : 42;
            textureHeight += isCustomIcon ? 0 : 42;


            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);
            }


            // 第三方icon添加底图begin
            if (!isCustomIcon){//isCustomIcon标示系统应用
                int resourceId = R.drawable.ic_background_for_every_icon; //要添加的底图资源文件
                Bitmap backBitmap = BitmapFactory.decodeResource(context.getResources(), resourceId);
                int backWidth = backBitmap.getWidth();
                int backHeight = backBitmap.getHeight();
                if(backWidth != sIconWidth || backHeight != sIconHeight){
                    Matrix matric = new Matrix();
                    matric.postScale((float)0.9 , (float)0.9);//缩放底图
                    canvas.drawBitmap(Bitmap.createBitmap(backBitmap ,left, top, left+width+left, 
                                        top+height+top ,matric ,true) ,8 ,9 ,null);
                }else{
                    canvas.drawBitmap(backBitmap ,0.0f ,0.0f ,null);
                }
            }
            // end


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


            return bitmap;
        }
    }

完结

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值