Android 4.0为Launcher主界面所有应用程序图标添加统一背景主题 .

   Android主界面的应用程序图标风格迥异,为了使用户看上去感觉比较统一,决定对其图标进行设置统一背景。

   在ApplicationPackageManager.java里有个public Drawable getDrawable(String packageName, int resid,ApplicationInfo appInfo) 的方法用于返回应用的图标。所以,我们可以在解析完成并在它被放入到缓存cache里面前,就对图标进行处理,使之成为我们想要的效果。

     //icon:获取的应用程序图标  idImg:想要添加的背景view id
    private Drawable buildTrayForIcon(Drawable icon, int idImg){
        
         Resources res = this.getResources();
         BitmapDrawable bd = new BitmapDrawable(buildTrayForIcon(icon, BitmapFactory.decodeResource(res, idImg)));
         bd.setTargetDensity(res.getDisplayMetrics());
         return bd;
    }


    private Bitmap buildTrayForIcon(Drawable icon, Bitmap background){
        
         if(icon == null){
                return null;
         }
       
          
         final int backgroundWith = background.getWidth();
       
         final int backgrouncHeight = background.getHeight();
       
         int sourceWidth = icon.getIntrinsicWidth();
         int sourceHeight = icon.getIntrinsicHeight();
        
     
         /**
          * 这里需要做的工作是:如何确保原图会在要加上图的中间
          * 如果原图比背景图要大的话就会使原图画不出来。所以这时候
          * 给出了一个固定的大小值来限定,right-left小于原图的宽,
          * 或者bottom-top小于原图的高 将原图进行缩放
          */
         int left = (backgroundWith - sourceWidth) / 2;
         int top = (backgrouncHeight - sourceHeight) / 2 ;
         int right=left+sourceWidth;
         int bottom=top+sourceHeight;
        
         if(left<0 ){
             left=0;
             left+=15;//这些值可以自己进行看情况设置,我这只是一个测试版
            
             right=backgroundWith-15;
         }
        
         if(top<0){
             top=0;
             top+=15;
            
             bottom=backgrouncHeight-15;
         }
           
         Bitmap compoundBitmap = null;
         compoundBitmap = Bitmap.createBitmap(backgroundWith, backgrouncHeight, Config.ARGB_8888);
         //Drawable d=new Drawable(compoundBitmap);
         //Drawable drawable = new BitmapDrawable(compoundBitmap); 
        
         Canvas canvas = new Canvas(compoundBitmap);
         canvas.drawBitmap(background, 0, 0, null);
         //Rect r=drawable.getBounds();
        

         //sOldBounds.set(icon.getBounds().left,);
       
         //icon.setBounds(left, top, left+width, top+height);
         icon.setBounds(left, top, right, bottom);
         icon.draw(canvas);
         //icon.setBounds(sOldBounds);
     
         return compoundBitmap;
        }

       这样处理之后就能达到我们的效果了。


猜你喜欢

转载自bcf.iteye.com/blog/1770309
今日推荐