TextView 文字描边

又是一周开始,但是心情不是很爽,被摆了一道。小小的鄙视一下!
老板不地道,咱该干咱的活就得干咱的活。
今天就记录一下上周要实现的一个小功能,就是给文字描边。

第一种实现思路

就是用两个TextView叠加,描边的TextView为底,实体TextView叠加在上面。
具体代码如下:

/*
 * StrokeTextView的目标是给文字描边
 * 实现方法是两个TextView叠加,只有描边的TextView为底,实体TextView叠加在上面
 * 看上去文字就有个不同颜色的边框了
 */
@SuppressLint("AppCompatCustomView")
public class StrokeTextView extends TextView {

    private TextView contentText = null;///用于描边的TextView
    private float borderWidth = 10;
    private int borderColor = Color.RED;
    public StrokeTextView(Context context) {
        this(context,null);
        contentText = new TextView(context);
    }

    public StrokeTextView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
        contentText = new TextView(context,attrs);
    }

    public StrokeTextView(Context context, AttributeSet attrs,
                          int defStyle) {
        super(context, attrs, defStyle);

        contentText = new TextView(context,attrs,defStyle);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.StrokeTextView);
        borderWidth = typedArray.getDimension(R.styleable.StrokeTextView_boder_width, 5);
        borderColor = typedArray.getColor(R.styleable.StrokeTextView_boder_color,0xffffff);
        typedArray.recycle();
        contentText.setTextColor(getTextColors());
        init();
    }

    public void init(){
        TextPaint tp1 = this.getPaint();
        tp1.setStrokeWidth(borderWidth);  //设置描边宽度
        tp1.setStyle(Paint.Style.STROKE); //设置画笔为边框模式
        tp1.setColor(borderColor);

        //对文字只描边
        this.setTextColor(borderColor); //设置描边颜色
        this.setGravity(getGravity());
    }

    @Override
    public void setLayoutParams (ViewGroup.LayoutParams params){
        super.setLayoutParams(params);
        contentText.setLayoutParams(params);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        CharSequence tt = contentText.getText();

        //两个TextView上的文字必须一致
        if(tt== null || !tt.equals(this.getText())){
            contentText.setText(getText());
            this.postInvalidate();
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        contentText.measure(widthMeasureSpec, heightMeasureSpec);
    }

    protected void onLayout (boolean changed, int left, int top, int right, int bottom){
        super.onLayout(changed, left, top, right, bottom);
        contentText.layout(left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);
        contentText.draw(canvas);
    }

}

自定义属性

  <declare-styleable name="StrokeTextView">
        <attr name="boder_width" format="dimension"/>
        <attr name="boder_color" format="color"/>
  </declare-styleable>

参数说明:
boder_widht :设置描边宽度;
boder_color :设置描边颜色
如何使用应该就不用赘述了,就当作一个普通的textView就可以。需要描边就把边框和颜色加上。

第二种实现思路

上一种是用了两个TextView,我们用同一个TextView,绘制两次,分别用不用的颜色不一样可以实现这个效果吗?试一试!

代码如下

@SuppressLint("AppCompatCustomView")
public class StrokeTextView1 extends TextView {
    private boolean hasStroke = false; // 默认不采用描边
    private Paint mTextPaint;
    private float borderWidth;
    private int borderColor;
    private ColorStateList textColor;
    public StrokeTextView1(Context context) {
        this(context,null);
    }

    public StrokeTextView1(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public StrokeTextView1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        m_TextPaint = getPaint();
        init(context,attrs,defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.StrokeTextView1);
        hasStroke = ta.getBoolean(R.styleable.StrokeTextView1_has_border,false);
        borderWidth = ta.getDimension(R.styleable.StrokeTextView1_border_width, 5);
        borderColor = ta.getColor(R.styleable.StrokeTextView1_border_color,0xffffff);
        ta.recycle();
        textColor = getTextColors();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (hasStroke) {
            // 描外层
            this.setTextColor(borderColor);
            mTextPaint.setStrokeWidth(borderWidth);  // 描边宽度
            mTextPaint.setStyle(Paint.Style.STROKE); //描边种类
            mTextPaint.setFakeBoldText(true); // 外层text采用粗体
            mTextPaint.setShadowLayer(1, 0, 0, 0); //字体的阴影效果,可以忽略
            super.onDraw(canvas);
            // 描内层,恢复原先的画笔
            this.setTextColor(textColor);
            mTextPaint.setStrokeWidth(0);
            mTextPaint.setStyle(Paint.Style.FILL);
            mTextPaint.setFakeBoldText(false);
            mTextPaint.setShadowLayer(0, 0, 0, 0);
        }
        super.onDraw(canvas);
    }

}

就是重写了onDraw()方法,外层描边,内侧填充。
其中自定义属性

    <declare-styleable name="StrokeTextView1" >
        <attr name="has_boder" format="boolean"/>
        <attr name="border_width" format="dimension"/>
        <attr name="border_color" format="color"/>
    </declare-styleable>

参数说明:
has_border :是否描边
border_widht :设置描边宽度
border_color :设置描边颜色
使用同样很简单,设置这几个属性就可以实现描边的效果。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 各种纯css图标 CSS3可以实现很多漂亮的图形,我收集了32种图形,在下面列出。直接用CSS3画出这些图形,要比...
    剑残阅读 9,676评论 0 8
  • 概述 在网易云课堂学习李南江老师的《从零玩转HTML5前端+跨平台开发》时,所整理的笔记。笔记内容为根据个人需求所...
    墨荀阅读 2,367评论 0 7
  • 一、CSS入门 1、css选择器 选择器的作用是“用于确定(选定)要进行样式设定的标签(元素)”。 有若干种形式的...
    宠辱不惊丶岁月静好阅读 1,619评论 0 6
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,511评论 0 17
  • 本文是针对刚学编程的小白,都是一些基础知识,如果想了解更多深层一点的东西,欢迎移步本人博客!! 博客地址 点击跳转...
    西巴撸阅读 551评论 0 0