方法1: 使用RoundedBitmapDrawable

public static RoundedBitmapDrawable bitmapToRoundedDrawable(@NonNull Resources res, @NonNull Bitmap bitmap,                                                                boolean circular, float cornerRadius) {        RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(res, bitmap);        drawable.setAlpha(255);//设置透明度        drawable.setAntiAlias(true);//设置抗锯齿        drawable.setDither(true);//设置防抖动        drawable.setGravity(Gravity.CENTER);        if (circular) {            drawable.setCircular(true);//设置正圆形        } else {            drawable.setCornerRadius(cornerRadius);//设置圆角半径        }        return drawable;    }final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test);//圆形照片final Drawable circleDrawable = Util.bitmapToRoundedDrawable(getResources(), bitmap, true, 0);mImageView1.setImageDrawable(circleDrawable);//圆角照片final Drawable roundedDrawable = Util.bitmapToRoundedDrawable(getResources(), bitmap, false, 100);mImageView2.setImageDrawable(roundedDrawable);

使用RoundedBitmapDrawable生成带边框的圆形照片:

public static Drawable bitmapToRoundedDrawableWithBorder(Resources res, Bitmap bitmap) {        //原图宽度        int bitmapWidth = bitmap.getWidth();        //原图高度        int bitmapHeight = bitmap.getHeight();        //边框宽度 pixel        int borderWidthHalf = 20;        //转换为正方形后的宽高        int bitmapSquareWidth = Math.min(bitmapWidth, bitmapHeight);        //最终图像的宽高        int newBitmapSquareWidth = bitmapSquareWidth + borderWidthHalf;        Bitmap roundedBitmap = Bitmap.createBitmap(newBitmapSquareWidth, newBitmapSquareWidth, Bitmap.Config.ARGB_8888);        Canvas canvas = new Canvas(roundedBitmap);        int x = borderWidthHalf + bitmapSquareWidth - bitmapWidth;        int y = borderWidthHalf + bitmapSquareWidth - bitmapHeight;        //裁剪后图像,注意X,Y要除以2 来进行一个中心裁剪        canvas.drawBitmap(bitmap, x / 2, y / 2, null);        Paint borderPaint = new Paint();        borderPaint.setAntiAlias(true);        borderPaint.setStyle(Paint.Style.STROKE);        borderPaint.setStrokeWidth(borderWidthHalf);        borderPaint.setColor(Color.GRAY);        //添加边框        canvas.drawCircle(canvas.getWidth() / 2, canvas.getWidth() / 2, newBitmapSquareWidth / 2, borderPaint);        return bitmapToRoundedDrawable(res, roundedBitmap, true, 0);    }final Drawable roundedDrawableWithBorder = Util.bitmapToRoundedDrawableWithBorder(getResources(), bitmap);mImageView3.setImageDrawable(roundedDrawableWithBorder);

方法2: 使用PorterDuffXfermode(PorterDuff.Mode.SRC_IN)实现圆角照片

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {        try {            int width = bitmap.getWidth();            int height = bitmap.getHeight();            Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);            Canvas canvas = new Canvas(output);            final Paint paint = new Paint();            final Rect rect = new Rect(0, 0, width, height);            final RectF rectF = new RectF(rect);            final float roundPx = 200;            paint.setAntiAlias(true);            canvas.drawARGB(0, 0, 0, 0);            paint.setColor(Color.BLACK);            canvas.drawRoundRect(rectF, roundPx, roundPx, paint);            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));            final Rect src = new Rect(0, 0, width, bitmap.getHeight());            canvas.drawBitmap(bitmap, src, rect, paint);            return output;        } catch (Exception e) {            e.printStackTrace();            return bitmap;        }    }final Bitmap roundBitmap = Util.getRoundedCornerBitmap(bitmap);mImageView4.setImageBitmap(roundBitmap);

方法3: 使用BitmapShader带边框的圆形照片

public static Bitmap getRoundBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int radius, int boarder) {        if (bitmap == null) {            return null;        }        int width = bitmap.getWidth();        int height = bitmap.getHeight();        float widthScale = outWidth * 1f / width;        float heightScale = outHeight * 1f / height;        Matrix matrix = new Matrix();        matrix.setScale(widthScale, heightScale);        //创建输出的bitmap        Bitmap desBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);        //创建canvas并传入desBitmap,这样绘制的内容都会在desBitmap上        Canvas canvas = new Canvas(desBitmap);        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);        //创建着色器        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);        //给着色器配置matrix        bitmapShader.setLocalMatrix(matrix);        paint.setShader(bitmapShader);        //创建矩形区域并且预留出border        RectF rect = new RectF(boarder, boarder, outWidth - boarder, outHeight - boarder);        //把传入的bitmap绘制到圆角矩形区域内        canvas.drawRoundRect(rect, radius, radius, paint);        if (boarder > 0) {            //绘制boarder            Paint boarderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);            boarderPaint.setColor(Color.BLACK);            boarderPaint.setStyle(Paint.Style.STROKE);            boarderPaint.setStrokeWidth(boarder);            canvas.drawRoundRect(rect, radius, radius, boarderPaint);        }        return desBitmap;    }final Bitmap roundBitmap1 = Util.getRoundBitmapByShader(bitmap, 800, 800, 400, 50);mImageView5.setImageBitmap(roundBitmap1);

方法4: 使用CareView使用圆角

                            

 

更多相关文章

  1. android使用shape设置下边框
  2. Android 自定义shape圆形按钮
  3. 怎么给Android(安卓)控件添加边框(样式)?
  4. Android 给ImageView自定义设置边框
  5. android渐变色,边角,边框,
  6. android 边框圆角
  7. android 自定义圆形的ImageView
  8. Android圆型头像实现-可设置边框
  9. android UI自定义设计圆形ImageView

随机推荐

  1. Android(安卓)Interface Definition Lang
  2. Android每天定时任务启动方法
  3. android文件、资源读取总结
  4. Android Studio升级后 出现在No subject
  5. android 判断是wifi还是4g网络
  6. Android存储空间不足
  7. DownloadError: android.googlesource.co
  8. Android:Duplicate class inxmlbeans-2.6.
  9. android资源文件详细介绍
  10. 豆微下载