package com.example.myapplication;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.AttributeSet;
import androidx.appcompat.widget.AppCompatTextView;
public class TextViewLine extends AppCompatTextView {
//下划线颜色
int mUnderLineColor = Color.TRANSPARENT;
//删除线颜色
int mStrikeLineColor = Color.RED;
//作用区域内text颜色
int mTextLengthColor = Color.TRANSPARENT;
boolean mTextBold = false;
boolean mStrikeLine = true;
boolean mUnderLine = false;
int mStart = -1;
int mEnd = -1;
SpannableStringBuilder mStyle;
public TextViewLine(Context context) {
this(context, null);
}
private void initPaint(Context context, AttributeSet attrs) {
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TextViewLine);
//删除线的颜色和样式
mUnderLineColor = array.getColor(R.styleable.TextViewLine_setUnderLineColor, Color.TRANSPARENT);
mStrikeLineColor = array.getColor(R.styleable.TextViewLine_setStrikeLineColor, Color.RED);
mTextLengthColor = array.getColor(R.styleable.TextViewLine_setTextLengthColor, Color.TRANSPARENT);
mTextBold = array.getBoolean(R.styleable.TextViewLine_setLengthTextBold, false);
mStart = array.getInt(R.styleable.TextViewLine_setTextStartColorIndex, -1);
mEnd = array.getInt(R.styleable.TextViewLine_setTextEndColorIndex, -1);
mStyle = new SpannableStringBuilder(this.getText().toString());
paint = new Paint();
paint.setColor(mStrikeLineColor);
// paint.setStrokeJoin(Paint.Join.ROUND);
// paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(5);
paint.setStrikeThruText(mStrikeLine);
if (this.getTextSize() > 0 && mStart != -1 && mEnd > mStart && mEnd <= this.getTextSize()) {
if (mTextBold) {
mStyle.setSpan(new StyleSpan(Typeface.BOLD), mStart, mEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (mTextLengthColor != Color.TRANSPARENT) {
mStyle.setSpan(new ForegroundColorSpan(mTextLengthColor), mStart, mEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
this.setText(mStyle);
}
}
}
public TextViewLine(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initPaint(context, attrs);
}
public TextViewLine(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
private Paint paint;
/**
* 设置start-end区域的textcolor和是否加粗
* @param start 文字作用域起点
* @param end 文字作用域终点(不包括)
* @param TextLengthColor 文字作用域颜色
* @param bold 文字作用域是否加粗
*/
public void setmTextLengthColorAndBold(int start,int end,int TextLengthColor,boolean bold){
if (getTextSize() > 0 && start>=0 && end > start && end <= getTextSize()) {
mStyle.setSpan(new ForegroundColorSpan(TextLengthColor), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mStyle.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
this.setText(mStyle);
}
}
/**
* 根据关键字设置颜色和加粗
* @param keyword
* @param textColor
* @param bold
*/
public void setKeyWordColorAndBold(String keyword,int textColor,boolean bold){
int index = 0;
while (index < getTextSize()) {
int begin = getText().toString().indexOf(keyword, index);
if (begin == -1) break;
int end = begin + keyword.length();
mStyle.setSpan(new ForegroundColorSpan(textColor), begin, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
if(bold) {
mStyle.setSpan(new StyleSpan(Typeface.BOLD), begin, end, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
}
index = end;
}
this.setText(mStyle);
}
@Override
protected void onDraw(Canvas canvas) {
//TextView布局的高度和宽度
float x = this.getWidth();
float y = this.getHeight();
//根据Textview的高度和宽度设置删除线的位置
//四个参数的意思:起始x的位置,起始y的位置,终点x的位置,终点y的位置
canvas.drawLine(0f, y / 2, x, y / 2, paint);
if (mUnderLineColor != Color.TRANSPARENT) {
paint.setStrikeThruText(false);
paint.setUnderlineText(mUnderLine);
paint.setColor(mUnderLineColor);
canvas.drawLine(0f, y, x, y, paint);
}
//super最后调用表示删除线在位于文字的上边
//super方法先调用删除线不显示
super.onDraw(canvas);
}
}
value下新建atts.xml,加入代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TextViewLine">
<attr name="setStrikeThruText" format="boolean"/>
<attr name="setUnderLineText" format="boolean"/>
<attr name="setLengthTextBold" format="boolean"/>
<attr name="setStrikeLineColor" format="color"/>
<attr name="setUnderLineColor" format="color"/>
<attr name="setTextStartColorIndex" format="integer"/>
<attr name="setTextEndColorIndex" format="integer"/>
<attr name="setTextLengthColor" format="color"/>
</declare-styleable>
</resources>