带删除线的TextView,删除线颜色与宽度可以设置

一.定义三个属性:删除线的颜色,宽度,是否显示删除线

<declare-styleable name="DeleteLineTextView">
    <attr name="showDeleteLine">
        <enum name="show" value="0" />
        <enum name="hide" value="1" />
    </attr>
    <attr name="deleteLineColor" format="color|reference" />
    <attr name="deleteLineWidth" format="dimension" />
</declare-styleable>
二.自定义view继承自TextView

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

/**
 * 带删除线的TextView,删除线颜色与宽度可以设置
 */

public class DeleteLineTextView extends TextView {

    public static final String TAG = "DeleteLineTextView";
    Paint linePaint;
    int showDeleteLine;
    int deleteLineColor;
    int deleteLineWidth;
    int i;
    public static final int SHOW = 0;
    public static final int HIDE = 1;

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

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

    public DeleteLineTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DeleteLineTextView);
        int indexCount = typedArray.getIndexCount();
        showDeleteLine = typedArray.getInt(R.styleable.DeleteLineTextView_showDeleteLine, HIDE);
        deleteLineColor = typedArray.getColor(R.styleable.DeleteLineTextView_deleteLineColor, Color.BLACK);
        deleteLineWidth = (int) typedArray.getDimension(R.styleable.DeleteLineTextView_deleteLineWidth, 1f);
        typedArray.recycle();
        Log.e(TAG, "deleteLineWidth:" + deleteLineWidth);
        i = dip2px(context, deleteLineWidth);
        linePaint = new Paint();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (showDeleteLine == SHOW) {
            linePaint.setAntiAlias(true);
            linePaint.setColor(deleteLineColor);
            linePaint.setStrokeWidth(i);
            int lineCount = this.getLineCount();
            if (lineCount != 0) {
                int lineHeight = this.getLineHeight();
                int baseline = this.getBaseline();
                int viewWidth = getWidth();
                int paddingLeft = getPaddingLeft();
                int paddingRight = getPaddingRight();
                TextPaint paint = getPaint();
                String text = (String) getText();
                int textLength = text.length();
                Rect rect = new Rect();
                paint.getTextBounds(text, 0, textLength, rect);
                int singleLineHeight = rect.height();
                float textWidth = rect.width();
                int halfHeight = baseline - singleLineHeight / 2;
                if (lineCount == 1) {
                    canvas.drawLine(paddingLeft, halfHeight,
                            textWidth + paddingLeft, halfHeight, linePaint);
                } else {
                    //多行时,计算最后一行字符串所占长度
                    float sigleTextWidth = textWidth / textLength;//一个字符的宽度
                    int textSumInLine =
                            (int) ((viewWidth - paddingLeft - paddingRight) / sigleTextWidth);//每行中字符的个数,int向下取余
                    int sigleLineTextWidth = 
                            (int) (textSumInLine * sigleTextWidth);
                    int lastLineTextCount = 
                            textLength - textSumInLine * (lineCount - 1);
                    int lastLineWidth = 
                            (int) (lastLineTextCount * sigleTextWidth);
                    for (int i = 0; i < lineCount; i++) {
                        if (i == lineCount - 1) {
                            canvas.drawLine(paddingLeft, 
                                    halfHeight + i * lineHeight, 
                                    lastLineWidth + paddingLeft, 
                                    halfHeight + i * lineHeight, linePaint);
                        } else {
                            canvas.drawLine(paddingLeft, 
                                    halfHeight + i * lineHeight, 
                                    sigleLineTextWidth + paddingLeft, 
                                    halfHeight + i * lineHeight, linePaint);
                        }
                    }
                }

            }
        }
        super.onDraw(canvas);
    }

    public void setShowDeleteLine(boolean show) {
        if (show) {
            this.showDeleteLine = SHOW;
        } else {
            this.showDeleteLine = HIDE;
        }
    }

    public void setDeleteLineColor(int deleteLineColor) {
        this.deleteLineColor = deleteLineColor;
    }

    public void setDeleteLineWidth(Context context,int deleteLineWidth) {
        i = dip2px(context, deleteLineWidth);
    }

    public static int dip2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
三.XML中使用

xmlns:app="http://schemas.android.com/apk/res-auto"
<com.ycq.rongtest.DeleteLineTextView
    android:id="@+id/linetextview"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:background="#00ff00"
    android:paddingLeft="10dp"
    android:paddingRight="15dp"
    android:paddingTop="20dp"
    android:text="12345678900987876123456789009876"
    android:textSize="21sp"
    app:deleteLineWidth="1dp"
    app:deleteLineColor="#00ffff"
    app:showDeleteLine="show"/>
四.Activity中使用

DeleteLineTextView linetextview = (DeleteLineTextView) findViewById(R.id.linetextview);
linetextview.setDeleteLineColor(Color.parseColor("#0000ff"));
linetextview.setShowDeleteLine(true);
linetextview.setDeleteLineWidth(this,1);


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值