Launcher 读取第三方图标颜色添加相应的背景

Launcher 读取第三方图标颜色添加相应的背景

因为 android的第三方图标不规则,公司这边会要求统一加一个标准背板。但是如果所有的图标都加一个会显得单调,所以我采用读取第三方图标的颜色,然后添加相应的背景。


修改图标背景的文件是在Launcher3文件夹下的Utilities.java下的 createIconBitmap 函数里,原始函数为:

  1. public static Bitmap createIconBitmap(Drawable icon, Context context) {  
  2.     synchronized (sCanvas) {  
  3.         final int iconBitmapSize = getIconBitmapSize();  
  4.   
  5.         int width = iconBitmapSize;  
  6.         int height = iconBitmapSize;  
  7.   
  8.         if (icon instanceof PaintDrawable) {  
  9.             PaintDrawable painter = (PaintDrawable) icon;  
  10.             painter.setIntrinsicWidth(width);  
  11.             painter.setIntrinsicHeight(height);  
  12.         } else if (icon instanceof BitmapDrawable) {  
  13.             // Ensure the bitmap has a density.  
  14.             BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;  
  15.             Bitmap bitmap = bitmapDrawable.getBitmap();  
  16.             if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {  
  17.                 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());  
  18.             }  
  19.         }  
  20.         int sourceWidth = icon.getIntrinsicWidth();  
  21.         int sourceHeight = icon.getIntrinsicHeight();  
  22.         if (sourceWidth > 0 && sourceHeight > 0) {  
  23.             // Scale the icon proportionally to the icon dimensions  
  24.             final float ratio = (float) sourceWidth / sourceHeight;  
  25.             if (sourceWidth > sourceHeight) {  
  26.                 height = (int) (width / ratio);  
  27.             } else if (sourceHeight > sourceWidth) {  
  28.                 width = (int) (height * ratio);  
  29.             }  
  30.         }  
  31.   
  32.         // no intrinsic size --> use default size  
  33.         int textureWidth = iconBitmapSize;  
  34.         int textureHeight = iconBitmapSize;  
  35.   
  36.         final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,  
  37.                 Bitmap.Config.ARGB_8888);  
  38.         final Canvas canvas = sCanvas;  
  39.         canvas.setBitmap(bitmap);  
  40.   
  41.         final int left = (textureWidth-width) / 2;  
  42.         final int top = (textureHeight-height) / 2;  
  43.   
  44.         @SuppressWarnings("all"// suppress dead code warning  
  45.         final boolean debug = false;  
  46.         if (debug) {  
  47.             // draw a big box for the icon for debugging  
  48.             canvas.drawColor(sColors[sColorIndex]);  
  49.             if (++sColorIndex >= sColors.length) sColorIndex = 0;  
  50.             Paint debugPaint = new Paint();  
  51.             debugPaint.setColor(0xffcccc00);  
  52.             canvas.drawRect(left, top, left+width, top+height, debugPaint);  
  53.         }  
  54.   
  55.         sOldBounds.set(icon.getBounds());  
  56.         icon.setBounds(left, top, left+width, top+height);  
  57.         icon.draw(canvas);  
  58.         icon.setBounds(sOldBounds);  
  59.         canvas.setBitmap(null);  
  60.   
  61.         return  bitmap;  
  62.     }  
  63. }  
修改方法是,给他返回的bitmap添加相应的边框 ,把最末尾的:  

  1. return  bitmap;  
return  bitmap;

改为 :

  1. return addBorderToImage(bitmap, iconBitmapSize);  
return addBorderToImage(bitmap, iconBitmapSize);

关键地方就是这个  addBorderToImage 的实现:

  1. private static float mRoundedRate = 7f;  //圆角的角度  
  2.       
  3.     private static Bitmap addBorderToImage(Bitmap src, int iconSize) {  
  4.   
  5.         int width = iconSize;//getBorderWidth();  
  6.         int height = iconSize;//getBorderHeight();  
  7.           
  8.         Bitmap output = Bitmap.createBitmap(width, height,  
  9.                 Bitmap.Config.ARGB_8888);  
  10.           
  11.         Canvas canvas = new Canvas(output);  
  12.           
  13.           
  14.           
  15.         RectF outerRect = new RectF(0,0,width,height);  
  16.           
  17.         Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);  
  18.           
  19.         LinearGradient mLinearGradientClamp = new LinearGradient(0, 0, 0, height, new int[] {getCenterTopColor(src),getCenterBottomColor(src)}, null, TileMode.CLAMP);  
  20.           
  21.           
  22.         paint.setShader(mLinearGradientClamp);  
  23.           
  24.         canvas.drawRoundRect(outerRect, width/mRoundedRate, height/mRoundedRate, paint);  
  25.           
  26.         paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));  
  27.         int sw = src.getWidth();  
  28.         int sh = src.getHeight();  
  29.           
  30.         Rect srcRect = new Rect(0,0,sw,sh);  
  31.         Rect dstRect = getDstRect(sw,sh,width,height);  
  32.           
  33.         canvas.drawBitmap(src, srcRect, dstRect, paint);  
  34.           
  35.           
  36.         return output;  
  37.     }  
  38.       
  39.     private static Rect getDstRect( int sw,int sh,int w,int h){  
  40.         return new Rect(0,0,w,h);  
  41.     }  
  42.       
  43.     /**  
  44.      * 得到原始图片中,底部中间的第一个完全不透明的颜色  
  45.      * @return  
  46.      */  
  47.     private static int getCenterTopColor(Bitmap src){  
  48.         int w = src.getWidth();  
  49.         int h = src.getHeight();  
  50.           
  51.         int x = (int) (w*0.5);  
  52.           
  53.         int c = Color.WHITE;  
  54.         for(int y =h-1;y>=0;--y){  
  55.             int pixel = src.getPixel(x, y);  
  56.             if(Color.alpha(pixel)==0xFF){  
  57.                 c = pixel;  
  58.             }  
  59.         }  
  60.         return softColor(c);  
  61.     }  
  62. /**  
  63. * 得到原始图片中,顶部中间的第一个完全不透明的颜色  
  64.  * @return  
  65.  */  
  66.     private static int getCenterBottomColor(Bitmap src){  
  67.         int w = src.getWidth();  
  68.         int h = src.getHeight();  
  69.           
  70.         int x = (int) (w*0.5);  
  71.           
  72.         int c = Color.WHITE;  
  73.         for(int y =0;y<h;++y){  
  74.             int pixel = src.getPixel(x, y);  
  75.             if(Color.alpha(pixel)==0xFF){  
  76.                 c = pixel;  
  77.             }  
  78.         }  
  79.           
  80.         return softColor(c);  
  81.     }  
  82.       
  83. /**  
  84. * 将颜色变浅,变淡  
  85. * @param color  
  86. * @return  
  87. */  
  88.     private static int softColor(int color){  
  89.         int r = (int) (Color.red(color)*1.3);  
  90.         int b = (int) (Color.blue(color)*1.3);  
  91.         int g = (int) (Color.green(color)*1.3);  
  92.         int af = 0xFF;  
  93.           
  94.         if(b >= 0xFF){  
  95.             b = 0xFF;  
  96.         }  
  97.         if(r>=0xFF){  
  98.             r = 0xFF;  
  99.         }  
  100.         if(g>=0xFF){  
  101.             g = 0xFF;  
  102.         }  
  103.           
  104.         return af<<24|r<<16|g<<8|b;  
  105.     } 


转自 :http://blog.youkuaiyun.com/tangxuankai/article/details/51038124

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值