直播代碼android 圓角圖標 和不規則圓角(邊框)

將 原圖 變成 圓角 或者不規則的形狀,應爲桌面主題需要用到。

當然 方法很多 這邊提供如下方法。

// 圓角
    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;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章