带文字分类线

》导读:

在项目中有时会用到一些分类线,这里给大家定义了一个。
这里写图片描述

》控件

/**
 * Created by Kvin on 2017/2/5.
 */
public class ClassifyTextView extends View {
    private final int DEFAULT_LINE_WIDTH = -1;
    private Paint linePaint;
    private Paint txtPaint;

    //attr
    private float txtSize;
    private int txtColor;
    private String txt;
    private int lineColor;
    private int lineWidth;
    private int verSpace;

    public ClassifyTextView(Context context) {
        super(context);
    }

    public ClassifyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttr(context, attrs);
    }

    /**
     * init attr
     *
     * @param context
     * @param attrs
     */
    private void initAttr(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ClassifyTextView);
        txt = typedArray.getString(R.styleable.ClassifyTextView_ctvTxt);
        if (StringUtils.isEmpty(txt))txt="  ";
        txtSize = typedArray.getDimension(R.styleable.ClassifyTextView_ctvTxtSize, 5);
        verSpace = typedArray.getDimensionPixelSize(R.styleable.ClassifyTextView_ctvVerSpace, 5);
        lineWidth = typedArray.getDimensionPixelSize(R.styleable.ClassifyTextView_ctvLineWidth, DEFAULT_LINE_WIDTH);
        txtColor = typedArray.getColor(R.styleable.ClassifyTextView_ctvTxtColor, Color.BLACK);
        lineColor = typedArray.getColor(R.styleable.ClassifyTextView_ctvLineColor, Color.GRAY);
        typedArray.recycle();

        txtPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        txtPaint.setColor(txtColor);
        txtPaint.setTextSize(txtSize);

        linePaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        linePaint.setColor(lineColor);
        linePaint.setTextSize(1.0f);

    }

    public ClassifyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ClassifyTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //draw text
        float w = StringUtils.getTextWidth(txt, txtPaint);
        float h = StringUtils.getTextHeight(txtPaint);
        float lEnd = (canvas.getWidth() - w) / 2 - verSpace;
        float rStart = (canvas.getWidth() + w) / 2 + verSpace;

        canvas.drawText(txt, (canvas.getWidth() - w) / 2, h, txtPaint);
        //draw line
        if (lineWidth != DEFAULT_LINE_WIDTH) {
            canvas.drawLine(lEnd - lineWidth, h / 2, lEnd, h / 2, linePaint);
            canvas.drawLine(rStart, h / 2, rStart + lineWidth, h / 2, linePaint);
        } else {
            canvas.drawLine(0, h / 2, lEnd, h / 2, linePaint);
            canvas.drawLine(rStart, h / 2, canvas.getWidth(), h / 2, linePaint);
        }
    }

    /**
     * set text
     */
    public void setText(String msg) {
        txt = msg;
        postInvalidate();
    }
}

》属性:

  <!--Classify TextView -->
    <declare-styleable name="ClassifyTextView">
        <attr name="ctvTxt" format="string" />
        <attr name="ctvTxtColor" format="reference" />
        <attr name="ctvTxtSize" format="dimension" />
        <attr name="ctvLineWidth" format="dimension" />
        <attr name="ctvLineColor" format="reference" />
        <attr name="ctvVerSpace" format="dimension" />
    </declare-styleable>

》使用示例:

<com.kvin.toolkit.widget.ClassifyTextView
        android:id="@+id/history_tv"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:layout_marginTop="22dp"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_below="@+id/action_bar"
        app:ctvVerSpace="5dp"
        app:ctvTxtSize="13sp"
        app:ctvTxtColor="@color/gray_text"
        app:ctvTxt="历史搜索" />

》讨论:

目前发现将高度设置成wrap_content后的效果与match_parent一样。后期会处理这两个属性,然后在控件里给默认值。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值