自定义控件--描边字体

来源:互联网 发布:bf是什么意思淘宝 编辑:程序博客网 时间:2024/05/17 20:21

今天给大家介绍一个具有描边效果的自定义控件(不同于阴影),废话不多说,先看看效果
这三行是不同字体的效果

如果你眼前一亮,我艹,我要的就是这个,嘿嘿…

public class StroketText extends TextView {    private TextView outlineTextView = null;    private int outSideColor;                           //描边颜色    private int strokeWidth;                            //描边粗细    public StroketText(Context context) {        super(context);        outlineTextView = new TextView(context);        init();    }    public StroketText(Context context, AttributeSet attrs) {        super(context, attrs);        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.StroketText);        if(array!=null){            outSideColor = array.getColor(R.styleable.StroketText_outsideColor, Color.TRANSPARENT);            strokeWidth = array.getInt(R.styleable.StroketText_stroketWidth, 2);        }        outlineTextView = new TextView(context, attrs);        init();    }    public StroketText(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.StroketText);        if(array!=null){            outSideColor = array.getColor(R.styleable.StroketText_outsideColor, Color.TRANSPARENT);            strokeWidth = array.getInt(R.styleable.StroketText_stroketWidth, 2);        }        outlineTextView = new TextView(context, attrs, defStyleAttr);        init();    }    public void init(){        TextPaint paint = outlineTextView.getPaint();        paint.setStrokeWidth(strokeWidth);              // 描边宽度        paint.setStyle(Paint.Style.STROKE);        outlineTextView.setTextColor(outSideColor);     // 描边颜色        outlineTextView.setGravity(getGravity());    }    @Override    public void setLayoutParams (ViewGroup.LayoutParams params)    {        super.setLayoutParams(params);        outlineTextView.setLayoutParams(params);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)    {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        // 设置轮廓文字        CharSequence outlineText = outlineTextView.getText();        if (outlineText == null || !outlineText.equals(this.getText()))        {            outlineTextView.setText(getText());            postInvalidate();        }        outlineTextView.measure(widthMeasureSpec, heightMeasureSpec);    }    @Override    protected void onLayout (boolean changed, int left, int top, int right, int bottom)    {        super.onLayout(changed, left, top, right, bottom);        outlineTextView.layout(left, top, right, bottom);    }    @Override    protected void onDraw(Canvas canvas)    {        outlineTextView.draw(canvas);        super.onDraw(canvas);    }}

大家也看的出来,控件中有两个自定义属性,分别控制描边的颜色和描边的宽度,这当然需要在res/values/attrs下添加

<declare-styleable name="StroketText">        <attr name="outsideColor" format="color|reference" />        <attr name="stroketWidth" format="integer|reference" />    </declare-styleable>

最后在xml布局中使用即可

<!--默认字体-->    <com.test.myView.StroketText        android:id="@+id/tv_default"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="默认字体"        android:textColor="@color/colorWhite"        android:textSize="25dp"        app:outsideColor="@color/colorAccent"        app:stroketWidth="10" />
0 0
原创粉丝点击