imageview顯示圓形圖片

需要ImageView顯示圓形圖片做法如下

public static Bitmap toRoundCorner(Bitmap bitmap, float ratio) {
        System.out.println("圖片是否變成圓形模式瞭+++++++++++++");
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        canvas.drawRoundRect(rectF, bitmap.getWidth() / ratio,
                bitmap.getHeight() / ratio, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        System.out.println("pixels+++++++" + String.valueOf(ratio));

        return output;

    }

如果圖片是正方形的,將ratio設置為2,如果圖片不是正方形,自己再做個截圖吧!將圖片截成需要的正方形。顯示圓角邊長1/4,則傳入8,以此類推!

出處:https://www.apkbus.com/android-123489-1-1.html

發佈留言