ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

TextView自定义边框

2020-11-27 14:00:31  阅读:257  来源: 互联网

标签:自定义 mCornerRadius void float 边框 mPaint path public TextView


实现如下自定义边框,对TextView进行重写,canvas边框即可。重点是弧线的画法
在这里插入图片描述

画弧度主要是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"/>

标签:自定义,mCornerRadius,void,float,边框,mPaint,path,public,TextView
来源: https://blog.csdn.net/neabea2016/article/details/110229291

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有