android+主界面所有应用程序图标添加统一背景主题,Android 4.0替Launcher主界面所有应用程序图标添加统一背景主题...
當前位置:我的異常網(wǎng)? Android???Android 4.0替Launcher主界面所有應(yīng)用程序圖標添加
Android 4.0替Launcher主界面所有應(yīng)用程序圖標添加統(tǒng)一背景主題
www.myexceptions.net??網(wǎng)友分享于:2013-01-22??瀏覽:422次
Android 4.0為Launcher主界面所有應(yīng)用程序圖標添加統(tǒng)一背景主題 .
Android主界面的應(yīng)用程序圖標風格迥異,為了使用戶看上去感覺比較統(tǒng)一,決定對其圖標進行設(shè)置統(tǒng)一背景。
在ApplicationPackageManager.java里有個public Drawable getDrawable(String packageName, int resid,ApplicationInfo appInfo) 的方法用于返回應(yīng)用的圖標。所以,我們可以在解析完成并在它被放入到緩存cache里面前,就對圖標進行處理,使之成為我們想要的效果。
//icon:獲取的應(yīng)用程序圖標? 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;//這些值可以自己進行看情況設(shè)置,我這只是一個測試版
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;
}
這樣處理之后就能達到我們的效果了。
文章評論
總結(jié)
以上是生活随笔為你收集整理的android+主界面所有应用程序图标添加统一背景主题,Android 4.0替Launcher主界面所有应用程序图标添加统一背景主题...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2023央视春晚主持人阵容公布:首次迎来
- 下一篇: android 视频开发sd卡,Andr