Launcher3图标添加背景和统一边框

最后的效果如下:

第三方图标大小不一致,排列在桌面上样式混乱,需要对其做统一处理。

 

1,给图标添加背景。

全局搜索

createIconBitmap方法,进入到方法体内,这个方法就是图标的处理方法。

添加图标背景的代码如下,这段代码写在

@SuppressWarnings("all") // suppress dead code warning
final boolean debug = false;
if (debug) {……

的上面。代码:

//增加图标背景图片 OWL
 Bitmap backBitmap = BitmapFactory.decodeResource(context.getResources(),
         R.drawable.ic_launcher_icon_bg2);

 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);
     backBitmap = Bitmap.createBitmap(backBitmap, 0, 0, backWidth, backHeight, matrix, true);
     backBitmap = ImageUtil.getRoundCornerBitmap(backBitmap, PUtil.dip2px(context,12));//且圆角,可不要
     canvas.drawBitmap(backBitmap,
             0.0f, 0.0f, null);
 }else
 {
     backBitmap = ImageUtil.getRoundCornerBitmap(backBitmap, PUtil.dip2px(context,12));//且圆角,可不要
     canvas.drawBitmap(backBitmap, 0.0f, 0.0f, null);
 }

2,添加边框

createIconBitmap()方法的最后会返回一个bitmap对象,在返回前对其添加边框处理:

代码:

原本:return bitmap;改成:return addBorderToImage(bitmap, sIconTextureWidth);

其中addBorderToImage()方法和它用到的方法代码如下:

private static Bitmap addBorderToImage(Bitmap src, int iconSize) {

        int width = iconSize;//getBorderWidth();
        int height = iconSize;//getBorderHeight();

        Bitmap output = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        RectF outerRect = new RectF(0,0,width,height);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);

        LinearGradient mLinearGradientClamp = new LinearGradient(0, 0, 0, height, new int[] {getCenterTopColor(src),getCenterBottomColor(src)}, null, Shader.TileMode.CLAMP);
        paint.setShader(mLinearGradientClamp);
        canvas.drawRoundRect(outerRect, width/mRoundedRate, height/mRoundedRate, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
        int sw = src.getWidth();
        int sh = src.getHeight();

        Rect srcRect = new Rect(0,0,sw,sh);
        Rect dstRect = getDstRect(sw,sh,width,height);

        canvas.drawBitmap(src, srcRect, dstRect, paint);
        return output;
    }

    private static Rect getDstRect( int sw,int sh,int w,int h){
        return new Rect(0,0,w,h);
    }

    /**
     * 得到原始图片中,底部中间的第一个完全不透明的颜色
     * @return
     */
    private static int getCenterTopColor(Bitmap src){
        int w = src.getWidth();
        int h = src.getHeight();
        int x = (int) (w*0.5);
        int c = Color.WHITE;
        for(int y =h-1;y>=0;--y){
            int pixel = src.getPixel(x, y);
            if(Color.alpha(pixel)==0xFF){
                c = pixel;
            }
        }
        return c;
//        return softColor(c);
    }
    /**
     * 得到原始图片中,顶部中间的第一个完全不透明的颜色
     * @return
     */
    private static int getCenterBottomColor(Bitmap src){
        int w = src.getWidth();
        int h = src.getHeight();
        int x = (int) (w*0.5);
        int c = Color.WHITE;
        for(int y =0;y<h;++y){
            int pixel = src.getPixel(x, y);
            if(Color.alpha(pixel)==0xFF){
                c = pixel;
            }
        }
        return c;
//        return softColor(c);
    }

    /**
     * 将颜色变浅,变淡 一旦使用此方法,边框颜色会对应不上,故上面两个方法没有使用
     * @param color
     * @return
     */
    private static int softColor(int color){
        int r = (int) (Color.red(color)*1.3);
        int b = (int) (Color.blue(color)*1.3);
        int g = (int) (Color.green(color)*1.3);
        int af = 0xFF;
        if(b >= 0xFF){
            b = 0xFF;
        }
        if(r>=0xFF){
            r = 0xFF;
        }
        if(g>=0xFF){
            g = 0xFF;
        }
        return af<<24|r<<16|g<<8|b;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值