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

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

         



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值