Android 4.0为Launcher主界面所有应用程序图标添加统一背景主题
2012-09-05 18:25
453 查看
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;
}
这样处理之后就能达到我们的效果了。
在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;
}
这样处理之后就能达到我们的效果了。
相关文章推荐
- 说说Android桌面(Launcher应用)背后的故事(二)——应用程序的添加
- Android 统一为项目中的所有 Activity 添加自定义TopBar
- android Launcher3中定制第三方apk图标,实现类似主题功能
- Delphi 图标,应用程序背景图片添加
- Android 4.0 Launcher2源码分析——桌面快捷图标的拖拽
- Android 4.0 Launcher2源码分析——桌面快捷图标的拖拽
- android launcher开发之图标背景以及默认配置
- android Launcher3中定制第三方apk图标,实现类似主题功能
- 在Android桌面添加应用程序的快捷方式图标
- android launcher 相关知识 开机启动 wallpape 图标的脱拽 如何取得Android已安装的应用程序
- Android4.0.3 Launcher应用程序图标放置区域修改
- Android 4.0 修改Launcher 应用图标文字大小
- Android Launcher开发添加应用程序的快捷方式
- Android Launcher开发(四)Launcher概述及简单添加和删除应用程序的快捷方式
- 说说Android桌面(Launcher应用)背后的故事(二)——应用程序的添加
- Android 3d TOS Launcher 之桌面图标主题加框规格化
- Launcher 读取第三方图标颜色添加相应的背景
- android 如何定制Launcher主菜单中应用程序图标在主菜单中的显示顺序
- android launcher开发之图标背景以及默认配置
- android 在应用程序中添加快捷图标