ImageView实现圆形展示以及添加边框、内边框

package com.tripshop.trip.ui.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;

/**
 * Created by luyujian on 2018/9/28.
 * 圆形的imageView
 */

public class RoundImageView extends AppCompatImageView {
    private Xfermode xfermode;

    private int width;
    private int height;
    private float radius;

    private RectF srcRectF;

    //带有边框的圆形ImageView的宽度
    private int borderWidth;
    //带有边框的圆形ImageView的颜色
    private int borderColor;
    //是否是带边框的圆形ImageView
    private boolean isBorderRound;

    //带有边框的圆形ImageView的内边框宽度
    private int innerBorderWidth;
    //带有边框的圆形ImageView的内边框颜色
    private int innerBorderColor;
    //是否是带边框的并且带有内边框的圆形ImageView
    private boolean isInnerBorderRound;

    private Path path = new Path();
    private Paint paint = new Paint();

    public RoundImageView(Context context) {
        this(context, null);
    }

    public RoundImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        srcRectF = new RectF();
        xfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
        radius = Math.min(width, height) / 2.0f;
        srcRectF.set(width / 2.0f - radius, height / 2.0f - radius, width / 2.0f + radius, height / 2.0f + radius);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // 使用图形混合模式来显示指定区域的图片
        canvas.saveLayer(srcRectF, null, Canvas.ALL_SAVE_FLAG);
        float sx = 1.0f * width / width;
        float sy = 1.0f * height / height;
        // 缩小画布,使图片内容不被border、padding覆盖
        canvas.scale(sx, sy, width / 2.0f, height / 2.0f);

        super.onDraw(canvas);
        paint.reset();
        path.reset();

        path.addCircle(width / 2.0f, height / 2.0f, radius, Path.Direction.CCW);

        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setXfermode(xfermode);
        canvas.drawPath(path, paint);
        paint.setXfermode(null);

        // 恢复画布
        canvas.restore();

        //画圆角ImageView的边框
        if (isBorderRound) {
            drawRoundBorder(canvas);
        }
    }

    /**
     * 设置带有边框的圆角ImageView
     *
     * @param borderWidth 边框宽度
     * @param borderColor 边框颜色
     */
    public void setRoundBorderImageView(int borderWidth, int borderColor) {
        this.isBorderRound = true;
        this.borderColor = borderColor;
        this.borderWidth = borderWidth;
    }

    /**
     * 设置带有边框的圆角ImageView
     *
     * @param borderWidth      边框宽度
     * @param borderColor      边框颜色
     * @param innerBorderWidth 内边框宽度
     * @param innerBorderColor 内边框颜色
     */
    public void setRoundBorderImageView(int borderWidth, int borderColor, int innerBorderWidth, int innerBorderColor) {
        this.isBorderRound = true;
        this.isInnerBorderRound = true;
        this.borderColor = borderColor;
        this.borderWidth = borderWidth;
        this.innerBorderWidth = innerBorderWidth;
        this.innerBorderColor = innerBorderColor;
    }

    /**
     * 画圆角ImageView的边框
     *
     * @param canvas 画布
     */
    private void drawRoundBorder(Canvas canvas) {
        drawCircleBorder(canvas, borderWidth, borderColor, radius - borderWidth / 2.0f);
        if (isInnerBorderRound) {
            drawCircleBorder(canvas, innerBorderWidth, innerBorderColor, radius - borderWidth - innerBorderWidth / 2.0f);
        }
    }

    /**
     * 画圆角ImageView的边框
     *
     * @param canvas      画布
     * @param borderWidth 边框宽度
     * @param borderColor 边框颜色
     * @param radius      弧度
     */
    private void drawCircleBorder(Canvas canvas, int borderWidth, int borderColor, float radius) {
        initBorderPaint(borderWidth, borderColor);
        path.addCircle(width / 2.0f, height / 2.0f, radius, Path.Direction.CCW);
        canvas.drawPath(path, paint);
    }


    /**
     * 初始画圆角ImageView边框的画笔
     *
     * @param borderWidth 边框宽度
     * @param borderColor 边框颜色
     */
    private void initBorderPaint(int borderWidth, int borderColor) {
        path.reset();
        paint.setStrokeWidth(borderWidth);
        paint.setColor(borderColor);
        paint.setStyle(Paint.Style.STROKE);
    }
}

整理来自:https://blog.csdn.net/sinat_17775997/article/details/80852965

 

你可能感兴趣的:(ImageView实现圆形展示以及添加边框、内边框)