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);

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/48298aa094fa67f4412d25cca9a66048.html