android 圆角图标 和不规则圆角(边框)

android 圆角图标 和不规则圆角(边框)

1.png

需要求就是如图 所示

将 原图 变成 圆角 或者不规则的形状,应为桌面主题需要用到。

当然 方法很多 这边提供如下方法。

// 圓角
    private Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
        Bitmap roundBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(roundBitmap);
        int color = 0xff424242;
        Paint paint = new Paint();
        Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        RectF rectF = new RectF(rect);
        float roundPx = 50;
        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, rect, rect, paint);
        return roundBitmap;
    }

    // 不规则角
    private Bitmap getRoundedCornerBitmap(Bitmap bitmap, Bitmap bg) {
        Paint paint = new Paint();
        float scaleX = (float) (bitmap.getWidth() * 1.0 / bg.getWidth()) + 0.1f;
        float scaleY = (float) (bitmap.getHeight() * 1.0 / bg.getHeight()) + 0.1f;
        Bitmap scaleBitmap = scaleBitmap(bg, scaleX, scaleY);

        Bitmap roundBitmap = Bitmap.createBitmap(scaleBitmap.getWidth(), scaleBitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(roundBitmap);
        paint.setAntiAlias(true);
        canvas.drawBitmap(scaleBitmap, 0, 0, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, scaleBitmap.getWidth() / 2 - bitmap.getWidth() / 2, scaleBitmap.getHeight() / 2
                - bitmap.getHeight() / 2, paint);
        return roundBitmap;
    }

DEMO 下载地址

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容