最近改系统Launcher 遇到不少头疼的问题,Launcher中,图标大小不统一非常影响体验,
也不美观,市场上的launcher多数做法都是给第三方应用添加底图,源码中自带了用来调试用的
给图标添加一个方框的代码,是在是看不下去...(个人审美观的问题...不喜勿喷...)
话不多说,上源码...
package/apps/Launcher4/src/com/android/launcher4/Utilities.java
//在源码Utilities.java的createIconBitmap(Drawable icon, Context context, boolean isCustomIcon)方法中添加
public static Bitmap createIconBitmap(Drawable icon, Context context, boolean isCustomIcon) {
synchronized (sCanvas) { // we share the statics :-(
if (sIconWidth == -1) {
initStatics(context);
}
int width = sIconWidth;
int height = sIconHeight;
if (icon instanceof PaintDrawable) {
PaintDrawable painter = (PaintDrawable) icon;
painter.setIntrinsicWidth(width);
painter.setIntrinsicHeight(height);
} else if (icon instanceof BitmapDrawable) {
// Ensure the bitmap has a density.
BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
Bitmap bitmap = bitmapDrawable.getBitmap();
if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
}
}
int sourceWidth = icon.getIntrinsicWidth();
int sourceHeight = icon.getIntrinsicHeight();
if (sourceWidth > 0 && sourceHeight > 0) {
// Scale the icon proportionally to the icon dimensions
final float ratio = (float) sourceWidth / sourceHeight;
if (sourceWidth > sourceHeight) {
height = (int) (width / ratio);
} else if (sourceHeight > sourceWidth) {
width = (int) (height * ratio);
}
}
// no intrinsic size --> use default size
int textureWidth = sIconTextureWidth;
int textureHeight = sIconTextureHeight;
textureWidth += isCustomIcon ? 0 : 42;
textureHeight += isCustomIcon ? 0 : 42;
final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
Bitmap.Config.ARGB_8888);
final Canvas canvas = sCanvas;
canvas.setBitmap(bitmap);
final int left = (textureWidth-width) / 2;
final int top = (textureHeight-height) / 2;
// 源码自带的给图标增加五颜六色的框 相当难看.....
@SuppressWarnings("all") // suppress dead code warning
final boolean debug = false;
if (debug) {
// draw a big box for the icon for debugging
canvas.drawColor(sColors[sColorIndex]);
if (++sColorIndex >= sColors.length) sColorIndex = 0;
Paint debugPaint = new Paint();
debugPaint.setColor(0xffcccc00);
canvas.drawRect(left, top, left+width, top+height, debugPaint);
}
// 第三方icon添加底图begin
if (!isCustomIcon){//isCustomIcon标示系统应用
int resourceId = R.drawable.ic_background_for_every_icon; //要添加的底图资源文件
Bitmap backBitmap = BitmapFactory.decodeResource(context.getResources(), resourceId);
int backWidth = backBitmap.getWidth();
int backHeight = backBitmap.getHeight();
if(backWidth != sIconWidth || backHeight != sIconHeight){
Matrix matric = new Matrix();
matric.postScale((float)0.9 , (float)0.9);//缩放底图
canvas.drawBitmap(Bitmap.createBitmap(backBitmap ,left, top, left+width+left,
top+height+top ,matric ,true) ,8 ,9 ,null);
}else{
canvas.drawBitmap(backBitmap ,0.0f ,0.0f ,null);
}
}
// end
sOldBounds.set(icon.getBounds());
icon.setBounds(left, top, left+width, top+height);
icon.draw(canvas);
icon.setBounds(sOldBounds);
canvas.setBitmap(null);
return bitmap;
}
}
完结