Android Launcher3分析和修改2 ---图标和标题

1.根据不同需求加载不同Launcher布局

路径是:\Launcher\src\com.android.launcher3.DynamicGrid中

boolean hasAA = true ;//!LauncherAppState.isDisableAllApps();

 static float DEFAULT_ICON_SIZE_DP = 60;
  static float DEFAULT_ICON_SIZE_PX = 0;

deviceProfiles.add(new DeviceProfile("Nexus 4",

                335, 567,  4, 4,  DEFAULT_ICON_SIZE_DP, 13, (hasAA ? 5 : 5), 56, R.xml.default_workspace_4x4,
                R.xml.default_workspace_4x4_no_all_apps));

      DeviceProfile的各个参数依次代表:配置名字(任意定义)、 最小宽度(单位是dp)、最小高度(单位是dp)、桌面行数、桌面   列 数、 桌面Icon的size(单位是dp)、桌面Icon的文字size(单位是dp)、 Hotseat的Icon个数、Hotseat的Icon的size(单位是dp)

 2、Launcher 图标统一样式 ---四角矩形

路径是:\Launcher\src\com.android.launcher3.BubbleTextView中

在applyFromShortcutInfo(){方法中

....

//FastBitmapDrawable iconDrawable = Utilities.createIconDrawable(b);

        Bitmap bb = zoomBitmap(b, 148 , 148);

        /*Bitmap cc = circleMaskBitmap(bb);*///绘制圆形图片
        Bitmap cc = roundBitmapByShader(bb,148,148,40);//绘制圆角矩形

        FastBitmapDrawable iconDrawable = Utilities.createIconDrawable(cc);

.....

}

在 applyFromApplicationInfo(AppInfo info) {
        LauncherAppState app = LauncherAppState.getInstance();

        DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();

//Drawable topDrawable = Utilities.createIconDrawable(info.iconBitmap);

        Bitmap dd = zoomBitmap(info.iconBitmap, 148 ,148);
        /*Bitmap ee = circleMaskBitmap(dd);*///绘制圆形图片
        Bitmap ee = roundBitmapByShader(dd,148,148,40);//绘制圆角矩形

        Drawable topDrawable = Utilities.createIconDrawable(ee);

.....}

 private  Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float scaleWidth = ((float) width / w);
        float scaleHeight = ((float) height / h);
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
        return newbmp;
    }
    /**
     * 利用BitmapShader绘制圆角图片
     *
     * @param bitmap
     *              待处理图片
     * @param outWidth
     *              结果图片宽度,一般为控件的宽度
     * @param outHeight
     *              结果图片高度,一般为控件的高度
     * @param radius
     *              圆角半径大小
     * @return
     *              结果图片
     */
    private Bitmap roundBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int radius) {
        if(bitmap == null) {
            throw new NullPointerException("Bitmap can't be null");
        }
        android.util.Log.i("wanchengguo","587bitmap.getWidth()="+bitmap.getWidth());
        // 初始化缩放比
        float widthScale = outWidth * 1.0f / bitmap.getWidth();
        float heightScale = outHeight * 1.0f / bitmap.getHeight();
        Matrix matrix = new Matrix();
        matrix.setScale(widthScale, heightScale);
        
        // 初始化绘制纹理图
        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        
        // 根据控件大小对纹理图进行拉伸缩放处理
        bitmapShader.setLocalMatrix(matrix);


        // 初始化目标bitmap
        Bitmap targetBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);


        // 初始化目标画布
        Canvas targetCanvas = new Canvas(targetBitmap);


        // 初始化画笔
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setShader(bitmapShader);


        // 利用画笔将纹理图绘制到画布上面
        targetCanvas.drawRoundRect(new RectF(0, 0, outWidth, outWidth), radius, radius, paint);
       
        android.util.Log.i("wanchengguo","613targetBitmap.getWidth()="+targetBitmap.getWidth());
        return targetBitmap;
    }


    /*    绘制圆形图片*/
    private  Bitmap circleMaskBitmap(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        
        float roundPx;
        float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
        if (width <= height) {
            roundPx = 30;/*width / 2;*/
            top = 0;
            bottom = width;
            left = 0;
            right = width;
            height = width;
            dst_left = 0;
            dst_top = 0;
            dst_right = width;
            dst_bottom = width;
        } else {
            roundPx = 30;/*height / 2;*/
            float clip = (width - height) / 2;
            left = clip;
            right = width - clip;
            top = 0;
            bottom = height;
            width = height;
            dst_left = 0;
            dst_top = 0;
            dst_right = height;
            dst_bottom = height;
        }
        Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        Canvas canvas = new Canvas(output);


        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
        final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
        final RectF rectF = new RectF(dst);


        paint.setAntiAlias(true);


        canvas.drawARGB(0, 0, 0, 0);


        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);


        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, src, dst, paint);
        return output;
    }
    private  Bitmap drawableToBitmap(Drawable drawable) {
        Bitmap bitmap = Bitmap.createBitmap(
                drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(),
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;


    }

猜你喜欢

转载自blog.csdn.net/qingcai_yuanzi/article/details/80190540