需要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,以此类推!
时间: 2024-10-16 01:33:13