sld22525482
「已注销」
采纳率12.6%
2016-09-30 01:30 浏览 1.2k

使用textview描边如何动态修改描边的宽度和颜色?

// 文字描边
public class StrokeTextView extends TextView {
private TextView outlineTextView = null;

public StrokeTextView(Context context) {
    super(context);

    outlineTextView = new TextView(context);
    init();
}

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

    outlineTextView = new TextView(context, attrs);
    init();
}

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

    outlineTextView = new TextView(context, attrs, defStyle);
    init();
}

public void init() {
    TextPaint paint = outlineTextView.getPaint();
    paint.setStrokeWidth(3);// 描边宽度
    paint.setStyle(Style.STROKE);
    outlineTextView.setTextColor(Color.parseColor("#7F00FF"));// 描边颜色
    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);
}

}


想在代码中调用这个textview的时候 通过修改参数 动态修改描边宽度和描边颜色这两个值,自己尝试了一下有没有成功,求大神指点一下。。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答 默认 最新

  • FFZ2009 FFZ2009 2016-09-30 01:57

    看看这样行不行:

        public void setStrokeWidthAndColor(int width,int colorValue){
            TextPaint paint = outlineTextView.getPaint();
            paint.setStrokeWidth(width);// 描边宽度
            paint.setStyle(Paint.Style.STROKE);
    
            outlineTextView.setTextColor(colorValue);// 描边颜色
    
            outlineTextView.invalidate();//重画
        }
    
    点赞 评论 复制链接分享

相关推荐