TextView自定义边框

实现如下自定义边框,对TextView进行重写,canvas边框即可。重点是弧线的画法
TextView自定义边框

画弧度主要是arcTo和addArc两个方法

都是从3点钟开始,顺时针画,注意左上角和右下角是两倍的半径

  1. ArcTo 有两个传参方法可以通过RectF封装位置
    forceMoveTo:true 抬起画笔 false保留路径相当于上个点坐标lineTo左上角
public void arcTo(float left, float top, float right, float bottom, float startAngle,
            float sweepAngle, boolean forceMoveTo){
}
   public void arcTo(@NonNull RectF oval, float startAngle, float sweepAngle) {
        arcTo(oval.left, oval.top, oval.right, oval.bottom, startAngle, sweepAngle, false);
    }

2.addArc 相当于 ArcTo设置forceMoveTo 为 true

代码逻辑

StrokeTextView.class

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

import com.wttech.gm.R;


public class StrokeTextView extends AppCompatTextView {

    public static final int TYPE_All = 0;
    public static final int TYPE_TOP_BOTTOM = 1;
    public static final int TYPE_TOP_BOTTOM_LEFT = 2;
    public static final int TYPE_TOP_BOTTOM_RIGHT = 3;

    private int mType = TYPE_All;
    private Paint mPaint;
    private float mStrokeWidth = 2.0f;
    private int mColor = Color.BLUE;
    private float mCornerRadius = 30.0f;
    private Path path;
    private RectF rectF;

    public StrokeTextView(Context context) {
        super(context);
        init(context, null);
    }

    public StrokeTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public StrokeTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.StrokeTextView);
            mType = ta.getInteger(R.styleable.StrokeTextView_stroke_type, TYPE_All);
            ta.recycle();
        }
        path = new Path();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(mStrokeWidth);
    }

    public void setStrokeWidth(float mStrokeWidth) {
        this.mStrokeWidth = mStrokeWidth;
        invalidate();
    }

    public int getDecorateColor() {
        return mColor;
    }

    public void setDecorateColor(int color) {
        this.mColor = color;
        invalidate();
    }

    public float getStrokeWidth() {
        return mStrokeWidth;
    }

    public int getType() {
        return mType;
    }

    public void setType(int type) {
        this.mType = type;
        invalidate();
    }

    public float getDecorateCornerRadius() {
        return mCornerRadius;
    }

    public void setDecorateCornerRadius(float cornerRadius) {
        this.mCornerRadius = cornerRadius;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        setPaint();
        if (mType == TYPE_All) {
            drawRoundRect(canvas);
        } else if (mType == TYPE_TOP_BOTTOM) {
            drawTBRoundRect(canvas);
        } else if (mType == TYPE_TOP_BOTTOM_LEFT) {
            drawTBLRoundRect(canvas);
        } else if (mType == TYPE_TOP_BOTTOM_RIGHT) {
            drawTBRRoundRect(canvas);
        }
        super.onDraw(canvas);
    }

    private void drawTBRRoundRect(Canvas c) {
        path.reset();
        path.moveTo(0,0);
        //forceMoveTo true抬起画笔
        path.arcTo(getWidth() - mCornerRadius * 2, 0, getWidth(), mCornerRadius * 2, 270, 90, false);
        path.arcTo(getWidth() - mCornerRadius * 2, getHeight() - mCornerRadius * 2, getWidth(), getHeight(), 0, 90, false);
        path.lineTo(0, getHeight());
        c.drawPath(path, mPaint);
    }

    private void drawTBLRoundRect(Canvas c) {
        path.reset();
        rectF = new RectF(0, 0, mCornerRadius * 2, mCornerRadius * 2);
        path.addArc(rectF, 180, 90);
        path.lineTo(getWidth(), 0);
        path.moveTo(0, mCornerRadius);
        path.lineTo(0, getHeight() - mCornerRadius);
        rectF = new RectF(0, getHeight() - mCornerRadius * 2, mCornerRadius * 2, getHeight());
        path.addArc(rectF, 90, 90);
        path.moveTo(mCornerRadius, getHeight());
        path.lineTo(getWidth(), getHeight());

        c.drawPath(path, mPaint);
    }

    private void drawTBRoundRect(Canvas c) {
        path.reset();
        path.lineTo(getWidth(), 0);
        path.moveTo(getWidth(), getHeight());
        path.lineTo(0, getHeight());
        c.drawPath(path, mPaint);
    }

    private void drawRoundRect(Canvas c) {
        path.reset();
        rectF = new RectF(0, 0, getWidth(), getHeight());
        path.addRoundRect(rectF, mCornerRadius, mCornerRadius, Path.Direction.CW);
        c.drawPath(path, mPaint);
    }

    private void setPaint() {
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mStrokeWidth);
        mPaint.setColor(mColor);
    }
}

attrs.xml

    <declare-styleable name="StrokeTextView">
        <attr name="stroke_type" format="enum">
            <enum name="all" value="0"/>
            <enum name="top_bottom_left" value="2"/>
            <enum name="top_bottom_right" value="3"/>
        </attr>
    </declare-styleable>

用法

  <com.customview.StrokeTextView
                android:text="12123123"               
                android:paddingVertical="@dimen/dp3"
                android:paddingHorizontal="@dimen/dp5"
                app:stroke_type="top_bottom_left"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
上一篇:android自定义局部透明遮罩-新手引导说明


下一篇:android 温度折线图绘制