自定义控件--描边字体

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

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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值