Android ICON生成及优化
Android ICON生成及优化
Android系统中Launcher桌面图标简答来讲就是通过PackageManager获取对应APP的Icon即可,所获取的ICON就是简单地标准图标,对于Android来说,其标准的ICON为72*72简单桌面实现可以直接根据自身ICON标准,对该icon进行放大缩小后绘制即可。不过就目前来说,由于Android并未规定图标的标准样式,所以各家所制作的ICON各式各样,有的就是简单图标,有的是在方形背板基础上绘制ICON。所以各家桌面在自己绘制ICON时,都会对其增加背板,以统一各厂家APP图标,以标准的样式进行展示。如果背板是标准的,那问题也很好办,例如背板80*80的方形,图标缩放为72*72,直接绘制在(4,4)的位置即可。而随着各家桌面的不断进化,对图标绘制的要求也越来越高,各家允许使用自定义的背板,简单来说就是背板可以使方的、圆的、甚至是不规则的图形。这就使得ICON绘制时不能采用预定义配置的方式,需要根据背板的实际样式对ICON进行裁剪后绘制。简单来说就是按照背板的样式裁剪ICON,使ICON在背板中留出一定宽度的边沿后,在背板正中心位置进行绘制。
初拿到这个需求后,稍稍一考虑,很简单!从实现角度分析需求就是按照比背板小的边缘对ICON进行裁剪。分解来说就是:1.如何识别背板的边缘。2.如何进行裁剪。背板的边缘很好识别,通过getAlpha获取背板的Alpha即可。如何裁剪那,有点图像处理知识的就知道,只需要通过简单地4点采样,判断当前点是否需要切掉即可。说干就干,第一版的代码如下:
/**/
/*背板留出5像素边*/
private static final int EDGE_WIDTH = 5;
/*alpha值的最低值,低于该值则认为是透明*/
private static final int ALPHA_BLUR = 200;
/**
* 返回图标按背板裁剪后得Bitmap
* @param background 背板的Bitmap
* @param icon 图标的Bitmap
* */
public static Bitmap getBitmapWithNoScale(Drawable background, Bitmap icon){
/*首先调整icon大小与背板一致,通过缩放或居中显示*/
if(icon.getWidth() > background.getIntrinsicWidth()){
icon = Bitmap.createScaledBitmap(icon, background.getIntrinsicWidth(), background.getIntrinsicHeight(), true);
}
if(icon.getWidth() < background.getIntrinsicWidth() ){
Bitmap tmp = null;
try {
tmp = Bitmap.createBitmap(background.getIntrinsicWidth(), background.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
} catch (OutOfMemoryError e) {
// 如果发生了OOM问题, 重新申请一次
tmp = Bitmap.createBitmap(background.getIntrinsicWidth(), background.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
e.printStackTrace();
}
Canvas mCanvas = new Canvas(tmp);
mCanvas.drawBitmap(icon,(background.getIntrinsicWidth()-icon.getWidth())/2,(background.getIntrinsicHeight()-icon.getHeight())/2, null);
icon = tmp;
}
return getBitmapAlpha(background, icon);
}
private static ImageView mGarbage ;
/*obj 背板,res 图标*/
private static Bitmap getBitmapAlpha(Drawable backgroundDrawable, Bitmap icon){
// Bitmap myAlpha = obj.extractAlpha();
Bitmap background = ((BitmapDrawable)backgroundDrawable).getBitmap();
Bitmap alpha2 = background.extractAlpha();
int back_width = background.getWidth();
int back_height = background.getHeight();
int icon_width = icon.getWidth();
int icon_height = icon.getHeight();
int alpha_arr[] = new int[back_width*back_height];
alpha2 = alpha2.copy(Config.ARGB_8888, true);
alpha2.getPixels(alpha_arr, 0, back_width, 0, 0, back_width, back_height);
int icon_arr[] = new int[icon_width*icon_height];
icon.getPixels(icon_arr, 0, icon_width, 0, 0, icon_width, icon_height);
int back_startx = 0, back_starty = 0, back_endx = 0, back_endy = 0;
/*在图标比背板小的情况下,图标在背板中心位置,相对于背板的位置*/
int icon_startx,icon_starty,icon_endx,icon_endy;
int icon_offset = 0;
if(icon_width < back_width){
/*图标比较小哈,将其居中显示裁剪*/
Log.e("pluszhang","get icon smaller");
back_startx = EDGE_WIDTH;
back_endx = back_width-EDGE_WIDTH-1;
back_starty = EDGE_WIDTH;
back_endy = back_height-EDGE_WIDTH-1;
icon_offset = back_width-icon_width > EDGE_WIDTH*2?0:EDGE_WIDTH-(back_width-icon_width)/2;
}
else if(icon_width == back_width){
/*直接裁剪即可*/
back_startx = EDGE_WIDTH;
back_endx = back_width-EDGE_WIDTH-1;
back_starty = EDGE_WIDTH;
back_endy = back_height-EDGE_WIDTH-1;
}
for(int i=0; i<icon_height;i++){
for(int j=0; j<icon_width;j++){
if(i<back_starty||i>back_endy||j<back_startx||j>back_endx){
icon_arr[i*icon_width+j] = 0;
}
else{
if((alpha_arr[(i-EDGE_WIDTH)*back_width+j] >> 24 &0xff) < ALPHA_BLUR ||
(alpha_arr[(i+EDGE_WIDTH)*back_width+j] >> 24&0xff)< ALPHA_BLUR ||
(alpha_arr[(j-EDGE_WIDTH)+i*back_width] >> 24&0xff)< ALPHA_BLUR ||
(alpha_arr[(j+EDGE_WIDTH)+i*back_width] >> 24&0xff)< ALPHA_BLUR){
icon_arr[i*icon_width+j] = 0;
}
}
}
}
mGarbage.setBackgroundDrawable(backgroundDrawable);
mGarbage.setImageBitmap(Bitmap.createBitmap(icon_arr, icon_width, icon_height, Config.ARGB_8888));
return mGarbage.getDrawingCache();
}
以上这段代码是采用切的方法实现对图标处理方法,主要来讲就是首先进行标准化,将ICON与背板处理到相同大小,只缩不放大,防止ICON变形,通过背板的Alpha视图,采样绘制ICON,最终实现对ICON的绘制。通过对上述代码分析,由于对图标采用的是扫描方式进行处理,也就是说80X80的图标,要计算6400次,一个图标还好,要是有2,3百个图标,效率确实有些低。关键是效果不太好,由于该方法采用的是采样的方式,对于背板特别不规则的会导致图标边缘切割有许多毛刺,这个问题是该算法自身的问题,采用切的算法,不会有更好的效果,这是该算法本身决定的,如果要达到最优效果,只有更换算法换一种思路。
|
评论暂时关闭