我正在尝试在android中的文本上创建大纲/笔划。由于没有“简单的方法”来创建笔画,我尝试用画布实现它。我尝试了这个Textview扩展类(OutlineTextView.java):
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.TextView;
public class OutlineTextView extends TextView {
public OutlineTextView(Context context) {
super(context);
initPaint();
}
public OutlineTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initPaint();
}
public OutlineTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initPaint();
}
private void initPaint() {
mTextPaint = new TextPaint();
mTextPaint.setAntiAlias(true);
mTextPaint.setTextSize(getTextSize());
mTextPaint.setColor(mColor);
mTextPaint.setStyle(Paint.Style.FILL);
mTextPaint.setTypeface(getTypeface());
mTextPaintOutline = new TextPaint();
mTextPaintOutline.setAntiAlias(true);
mTextPaintOutline.setTextSize(getTextSize());
mTextPaintOutline.setColor(mBorderColor);
mTextPaintOutline.setStyle(Paint.Style.STROKE);
mTextPaintOutline.setTypeface(getTypeface());
mTextPaintOutline.setStrokeWidth(mBorderSize);
}
public void setText(String text) {
super.setText(text);
mText = text.toString();
requestLayout();
invalidate();
}
public void setTextSize(float size) {
super.setTextSize(size);
requestLayout();
invalidate();
initPaint();
}
public void setTextColor(int color) {
super.setTextColor(color);
mColor = color;
invalidate();
initPaint();
}
public void setShadowLayer(float radius, float dx, float dy, int color) {
super.setShadowLayer(radius, dx, dy, color);
mBorderSize = radius;
mBorderColor = color;
requestLayout();
invalidate();
initPaint();
}
public void setTypeface(Typeface tf, int style) {
super.setTypeface(tf, style);
requestLayout();
invalidate();
initPaint();
}
public void setTypeface(Typeface tf) {
super.setTypeface(tf);
requestLayout();
invalidate();
initPaint();
}
@Override
protected void onDraw(Canvas canvas) {
Layout layout = new StaticLayout(getText(), mTextPaintOutline, getWidth(), Layout.Alignment.ALIGN_CENTER, mSpacingMult, mSpacingAdd, mIncludePad);
layout.draw(canvas);
layout = new StaticLayout(getText(), mTextPaint, getWidth(), Layout.Alignment.ALIGN_CENTER, mSpacingMult, mSpacingAdd, mIncludePad);
layout.draw(canvas);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Layout layout = new StaticLayout(getText(), mTextPaintOutline, measureWidth(widthMeasureSpec), Layout.Alignment.ALIGN_CENTER, mSpacingMult, mSpacingAdd, mIncludePad);
int ex = (int) (mBorderSize * 2 + 1);
setMeasuredDimension(measureWidth(widthMeasureSpec) + ex, measureHeight(heightMeasureSpec) * layout.getLineCount() + ex);
}
private int measureWidth(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = (int) mTextPaintOutline.measureText(mText) + getPaddingLeft() + getPaddingRight();
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
private int measureHeight(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
mAscent = (int) mTextPaintOutline.ascent();
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = (int) (-mAscent + mTextPaintOutline.descent()) + getPaddingTop() + getPaddingBottom();
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
private TextPaint mTextPaint;
private TextPaint mTextPaintOutline;
private String mText = "";
private int mAscent = 0;
private float mBorderSize;
private int mBorderColor;
private int mColor;
private float mSpacingMult = 1.0f;
private float mSpacingAdd = 0;
private boolean mIncludePad = true;
}
我在主要活动中使用此课程如下:
OutlineTextView t2b = new OutlineTextView(this);
t2b.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL);
t2b.setTextSize(44);
t2b.setTextColor(Color.parseColor(Color.Green));
t2b.setShadowLayer(10, 1, 1, Color.BLACK);
Typeface type2 = Typeface.createFromAsset(getAssets(),"fonts/HelveticaLTStd-Bold.otf");
t2b.setTypeface(type2);
t2b.setText(percentage + "%");
fl.addView(t2b);
fl是一个FrameLayout。现在输出是一个描边文本,它看起来就像我想要的那样。我的视图是一长串的图块,每个图块都是一个描边文本。文字不是我的问题。问题是滚动时的性能。如果我向下或向上滚动,性能会变慢并且不稳定。我认为问题是我绘制描边线的方式。我尝试了很多笔划文本的变化,并尝试了每个建议的笔画线变化。每次我都有同样的问题。如果我绘制没有笔划线的文本(但仍使用OutlineTextView),它会再次滚动,没有滞后,没有性能下降。只是绘制笔划线似乎会减慢系统速度。
P.S:这个OutlineTextView.java不是我自己的,我只是从互联网上得到它,因为这似乎画了一个描边文字。
如果有人能帮助我,我将感激不尽。