设为首页 加入收藏

TOP

Android ICON生成及优化(一)
2014-11-24 01:45:45 来源: 作者: 【 】 浏览:2
Tags:Android 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 for(int j=0; j if(iback_endy||jback_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*bac

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Linux shell中的那些小把戏 下一篇Android使用定制framework开发应用

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: