如題瘸羡,最近業(yè)務(wù)需求字體增加描邊漩仙。興高采烈的從網(wǎng)上copy下來demo運(yùn)行之后發(fā)現(xiàn)在TextView完成onMeasure之后setText出來顯示的問題就不帶描邊效果了,是代碼出現(xiàn)問題了嘛犹赖?還是我copy錯(cuò)了队他?
分析了一波源碼發(fā)現(xiàn),網(wǎng)上常見的demo都只考慮了界面初始化階段峻村。也就是你如果在xml文件中或者在TextView繪制成功之前設(shè)置了文本麸折,則渲染正常,但一旦如果文案發(fā)生變化粘昨,想再去更改時(shí)垢啼,發(fā)現(xiàn)描邊效果不見了窜锯。
怎么解決?
思路:只要文字變化的時(shí)候芭析,重新執(zhí)行onMeasure里的邏輯不就好了锚扎?
行動:
然后我簡單扒了下TextView的源碼,發(fā)現(xiàn)每次setText之后都會觸發(fā)
@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
super.onTextChanged(text, start, lengthBefore, lengthAfter);
if (mTextHelper != null && !PLATFORM_SUPPORTS_AUTOSIZE && mTextHelper.isAutoSizeEnabled()) {
mTextHelper.autoSizeText();
}
}
解決:
不賣關(guān)子了馁启,曬源碼吧
/**
* Title:帶描邊的TextView
* Describe:
* Remark:與網(wǎng)上常見的不同驾孔,我重寫了 {@link #onTextChanged(CharSequence, int, int, int)},這樣就支持動態(tài)修改了
* <p>
* Created by milo
* E-Mail : 303767416@qq.com
* 2020/7/16
*/
public class StrokeTextView extends AppCompatTextView {
private static final String TAG = "StrokeTextView";
private TextView borderText = null;///用于描邊的TextView
private int mStrokeWidth = 3;
private int mStrokeColor = R.color.module_magic_border_text;
public StrokeTextView(Context context) {
super(context);
borderText = new TextView(context);
init();
}
public StrokeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
borderText = new TextView(context, attrs);
init();
}
public StrokeTextView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
borderText = new TextView(context, attrs, defStyle);
init();
}
@Override
public void setLayoutParams(ViewGroup.LayoutParams params) {
super.setLayoutParams(params);
borderText.setLayoutParams(params);
}
@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
super.onTextChanged(text, start, lengthBefore, lengthAfter);
if (borderText != null) {
FZLogger.d(TAG, "onTextChanged .. borderText == " + borderText.getText().toString() + ", text == " + text.toString());
CharSequence borderChar = borderText.getText();
//兩個(gè)TextView上的文字必須一致
if (borderChar == null || !borderChar.equals(this.getText())) {
borderText.setText(getText());
this.postInvalidate();
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
CharSequence borderChar = borderText.getText();
//兩個(gè)TextView上的文字必須一致
if (borderChar == null || !borderChar.equals(this.getText())) {
borderText.setText(getText());
this.postInvalidate();
}
borderText.measure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
borderText.layout(left, top, right, bottom);
}
@Override
protected void onDraw(Canvas canvas) {
borderText.draw(canvas);
super.onDraw(canvas);
}
public void init() {
TextPaint tp1 = borderText.getPaint();
tp1.setStrokeWidth(mStrokeWidth); //設(shè)置描邊寬度
tp1.setStyle(Paint.Style.STROKE); //對文字只描邊
borderText.setTextColor(getResources().getColor(mStrokeColor)); //設(shè)置描邊顏色
borderText.setGravity(getGravity());
}
private void setStrokeWidth(int strokeWidth) {
this.mStrokeWidth = strokeWidth;
init();
invalidate();
}
private void setStrokeColor(@ColorRes int strokeColor) {
this.mStrokeColor = strokeColor;
init();
invalidate();
}
}
再到代碼中嘗試一下惯疙,是不是任意時(shí)刻調(diào)用 setText 都可以正確顯示描邊了呢翠勉。