Android实现圆角照片和圆形照片

方法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使用圆角

<android.support.v7.widget.CardView
                android:layout_width="300dp"
                android:layout_height="300dp"
                app:cardBackgroundColor="#1ac"
                app:cardCornerRadius="12dp"
                app:cardElevation="0dp"
                app:cardPreventCornerOverlap="false">

                <ImageView
                    android:id="@+id/iv6"
                    android:layout_width="300dp"
                    android:layout_height="300dp"
                    android:scaleType="centerCrop" />
            </android.support.v7.widget.CardView>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值