Android系统Launcher3图标去除默认白边,添加圆角白底

Android系统Launcher3图标去除默认白边,添加圆角白底

需求

在Android系统的Launcher3图标中,如果不是自适应图标会有默认的白边,需要去除白边并且添加圆角白底。

系统版本

Android 13

修改涉及到的文件

主要涉及到以下文件

/frameworks/libs/systemui/iconloaderlib/src/com/android/launcher3/icons/BaseIconFactory.java

修改步骤

  1. 去除白边
    将normalizeAndWrapToAdaptiveIcon方法下添加白边的代码注释掉即可。
@Nullable
protected Drawable normalizeAndWrapToAdaptiveIcon(@Nullable Drawable icon,
        final boolean shrinkNonAdaptiveIcons, @Nullable final RectF outIconBounds,
        @NonNull final float[] outScale) {
    if (icon == null) {
        return null;
    }
    float scale = 1f;
    
//        if (shrinkNonAdaptiveIcons && !(icon instanceof AdaptiveIconDrawable)) {
//            if (mWrapperIcon == null) {
//                mWrapperIcon = mContext.getDrawable(R.drawable.adaptive_icon_drawable_wrapper)
//                        .mutate();
//            }
//            AdaptiveIconDrawable dr = (AdaptiveIconDrawable) mWrapperIcon;
//            dr.setBounds(0, 0, 1, 1);
//            boolean[] outShape = new boolean[1];
//            scale = getNormalizer().getScale(icon, outIconBounds, dr.getIconMask(), outShape);
//            if (!outShape[0]) {
//                FixedScaleDrawable fsd = ((FixedScaleDrawable) dr.getForeground());
//                fsd.setDrawable(icon);
//                fsd.setScale(scale);
//                icon = dr;
//                scale = getNormalizer().getScale(icon, outIconBounds, null, null);
//                ((ColorDrawable) dr.getBackground()).setColor(mWrapperBackgroundColor);
//            }
//        } else {
            scale = getNormalizer().getScale(icon, outIconBounds, null, null);
//        }
    
    outScale[0] = scale;
    return icon;
}
  1. 添加圆角白底
    将createIconBitmap方法下原代码注释掉,然后添加绘制正方圆角白底代码。
@NonNull
protected Bitmap createIconBitmap(@Nullable final Drawable icon, final float scale,
        @BitmapGenerationMode int bitmapGenerationMode) {
//        final int size = mIconBitmapSize;
//        final Bitmap bitmap;
//        switch (bitmapGenerationMode) {
//            case MODE_ALPHA:
//                bitmap = Bitmap.createBitmap(size, size, Config.ALPHA_8);
//                break;
//            case MODE_HARDWARE:
//            case MODE_HARDWARE_WITH_SHADOW: {
//                return BitmapRenderer.createHardwareBitmap(size, size, canvas ->
//                        drawIconBitmap(canvas, icon, scale, bitmapGenerationMode, null));
//            }
//            case MODE_WITH_SHADOW:
//            default:
//                bitmap = Bitmap.createBitmap(size, size, Config.ARGB_8888);
//                break;
//        }
//        if (icon == null) {
//            return bitmap;
//        }
//        mCanvas.setBitmap(bitmap);
//        drawIconBitmap(mCanvas, icon, scale, bitmapGenerationMode, bitmap);
//        mCanvas.setBitmap(null);
//        return bitmap;

    final int size = mIconBitmapSize;
    int width = size;
    int height = size;

    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(mContext.getResources().getDisplayMetrics());
        }
    }
    
    int sourceWidth = icon.getIntrinsicWidth();
    int sourceHeight = icon.getIntrinsicHeight();
    if (sourceWidth > 0 && sourceHeight > 0) {
        final float ratio = (float) sourceWidth / sourceHeight;
        if (sourceWidth > sourceHeight) {
            height = (int) (width / ratio);
        } else if (sourceHeight > sourceWidth) {
            width = (int) (height * ratio);
        }
    }

    int textureWidth = size;
    int textureHeight = size;
    Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight, Bitmap.Config.ARGB_8888);
    final Canvas canvas = mCanvas;
    canvas.setBitmap(bitmap);

    final int left = (textureWidth - width) / 2;
    final int top = (textureHeight - height) / 2;

    mOldBounds.set(icon.getBounds());
    icon.setBounds(left, top, left + width, top + height);
    icon.draw(canvas);
    icon.setBounds(mOldBounds);
    canvas.setBitmap(null);
    return getRoundCornerBitmap(bitmap, 132.0f / 28.0f);
}

private Bitmap getRoundCornerBitmap(Bitmap bitmap, float ratio) {
    if (bitmap == null || bitmap.isRecycled()) {
        return null;
    }

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    if (width <= 0 || height <= 0) {
        return null;
    }

    if (ratio < 0) {
        ratio = 1;
    }
    
    try {
        Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        
        Canvas canvas = new Canvas(output);
        canvas.setDrawFilter(new PaintFlagsDrawFilter(0, ANTI_ALIAS_FLAG | FILTER_BITMAP_FLAG));
        
        final Paint paint = new Paint();
        paint.setAntiAlias(true);

 		canvas.drawARGB(0, 0, 0, 0);
        canvas.save();
 		float scale = (float) mIconBitmapSize / output.getWidth();
        canvas.scale(scale, scale, width / 2f, height / 2f);
		
        final RectF rectF = new RectF(1f, 1f, width - 1f, height -1f);
        float rx = width / ratio;
        float ry = height / ratio;
        paint.setColor(Color.WHITE);
        canvas.drawRoundRect(rectF, rx, ry, paint);
        
        final Rect rect = new Rect(0, 0, width, height);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        
        canvas.restore();
        bitmap.recycle();
        
        return output;
    } catch (Exception e) {
        // handle exception
    }
    return null;
}

对于圆角的计算可以按照设计的需求进行更改,还可以根据需求修改画笔颜色绘制。

总结

以上就是添加Android系统Launcher3图标去除默认白边,添加圆角白底的方法。

  • 14
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值