Android圆形图片的实现以及设置边缘颜色、宽度

Android圆形图片的实现以及设置边缘颜色、宽度

	/**
     * 获取指定颜色边缘的圆形图片
     *
     * @param bitmap bitmap对象
     * @param width  期望的宽
     * @param height 期望的高
     * @param color  颜色
     * @param border 边缘
     * @return bitmap对象
     */
    public static Bitmap getCircleBitmap(Bitmap bitmap, int width, int height, int color, int border) {
        if (bitmap == null) {
            return null;
        }
        int radius;
        int mWidth = bitmap.getWidth();
        int mHeight = bitmap.getHeight();
        float widthScale = width * 1f / mWidth;
        float heightScale = height * 1f / mHeight;

        Matrix matrix = new Matrix();
        matrix.setScale(widthScale, heightScale);
        Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        if (height > width) {
            radius = width / 2;
        } else {
            radius = height / 2;
        }
        //创建canvas
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        bitmapShader.setLocalMatrix(matrix);
        paint.setShader(bitmapShader);
        canvas.drawCircle(width / 2, height / 2, radius - border, paint);
        if (border > 0) {
            //绘制border
            Paint borderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            borderPaint.setColor(color);
            borderPaint.setStyle(Paint.Style.STROKE);
            borderPaint.setStrokeWidth(border);
            canvas.drawCircle(width / 2, height / 2, radius - border, borderPaint);
        }
        return result;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值