下面列出了android.text.TextPaint#setLetterSpacing ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static float getLetterSpacing(CharSequence text, TextPaint paint, float targetWidth,
float low, float high, float precision) {
float mid = (low + high) / 2.0f;
paint.setLetterSpacing(mid);
float measuredWidth = paint.measureText(text, 0, text.length());
if (high - low < precision) {
if (measuredWidth < targetWidth) {
return mid;
} else {
return low;
}
} else if (measuredWidth > targetWidth) {
return getLetterSpacing(text, paint, targetWidth, low, mid, precision);
} else if (measuredWidth < targetWidth) {
return getLetterSpacing(text, paint, targetWidth, mid, high, precision);
} else {
return mid;
}
}
private void apply(TextPaint paint) {
// mLetterSpacing and paint.getTextSize() are both in pixels,
// yielding an accurate em value.
if (!Float.isNaN(mLetterSpacing)) {
paint.setLetterSpacing(mLetterSpacing / paint.getTextSize());
}
}
private Layout createLayout(ReflowData data, Context context, boolean enforceMaxLines) {
TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
paint.setTextSize(data.textSize);
paint.setColor(data.textColor);
paint.setLetterSpacing(data.letterSpacing);
if (data.fontName != null) {
paint.setTypeface(FontUtil.get(context, data.fontName));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
StaticLayout.Builder builder = StaticLayout.Builder.obtain(
data.text, 0, data.text.length(), paint, data.textWidth)
.setLineSpacing(data.lineSpacingAdd, data.lineSpacingMult)
.setBreakStrategy(data.breakStrategy);
if (enforceMaxLines && data.maxLines != -1) {
builder.setMaxLines(data.maxLines);
builder.setEllipsize(TextUtils.TruncateAt.END);
}
return builder.build();
} else {
return new StaticLayout(
data.text,
paint,
data.textWidth,
Layout.Alignment.ALIGN_NORMAL,
data.lineSpacingMult,
data.lineSpacingAdd,
true);
}
}
private void apply(TextPaint paint) {
paint.setLetterSpacing(mLetterSpacing / paint.getTextSize());
}
/**
* 初始化画笔
*/
private void initPaint() {
Typeface typeface;
try {
if (!TextUtils.isEmpty(readBookControl.getFontPath())) {
typeface = Typeface.createFromFile(readBookControl.getFontPath());
} else {
typeface = Typeface.SANS_SERIF;
}
} catch (Exception e) {
Toast.makeText(mContext, "字体文件未找,到恢复默认字体", Toast.LENGTH_SHORT).show();
readBookControl.setReadBookFont(null);
typeface = Typeface.SANS_SERIF;
}
// 绘制提示的画笔
mTipPaint = new TextPaint();
mTipPaint.setColor(readBookControl.getTextColor());
mTipPaint.setTextAlign(Paint.Align.LEFT); // 绘制的起始点
mTipPaint.setTextSize(ScreenUtils.spToPx(DEFAULT_TIP_SIZE)); // Tip默认的字体大小
mTipPaint.setTypeface(Typeface.create(typeface, Typeface.NORMAL));
mTipPaint.setAntiAlias(true);
mTipPaint.setSubpixelText(true);
// 绘制标题的画笔
mTitlePaint = new TextPaint();
mTitlePaint.setColor(readBookControl.getTextColor());
mTitlePaint.setTextSize(mTitleSize);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mTitlePaint.setLetterSpacing(readBookControl.getTextLetterSpacing());
}
mTitlePaint.setStyle(Paint.Style.FILL_AND_STROKE);
mTitlePaint.setTypeface(Typeface.create(typeface, Typeface.BOLD));
mTitlePaint.setTextAlign(Paint.Align.CENTER);
mTitlePaint.setAntiAlias(true);
// 绘制页面内容的画笔
mTextPaint = new TextPaint();
mTextPaint.setColor(readBookControl.getTextColor());
mTextPaint.setTextSize(mTextSize);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mTextPaint.setLetterSpacing(readBookControl.getTextLetterSpacing());
}
int bold = readBookControl.getTextBold() ? Typeface.BOLD : Typeface.NORMAL;
mTextPaint.setTypeface(Typeface.create(typeface, bold));
mTextPaint.setAntiAlias(true);
// 绘制结束的画笔
mTextEndPaint = new TextPaint();
mTextEndPaint.setColor(readBookControl.getTextColor());
mTextEndPaint.setTextSize(mTextEndSize);
mTextEndPaint.setTypeface(Typeface.create(typeface, Typeface.NORMAL));
mTextEndPaint.setAntiAlias(true);
mTextEndPaint.setSubpixelText(true);
mTextEndPaint.setTextAlign(Paint.Align.CENTER);
// 绘制电池的画笔
mBatteryPaint = new TextPaint();
mBatteryPaint.setAntiAlias(true);
mBatteryPaint.setDither(true);
mBatteryPaint.setTextSize(ScreenUtils.spToPx(DEFAULT_TIP_SIZE - 3));
mBatteryPaint.setTypeface(Typeface.createFromAsset(mContext.getAssets(), "number.ttf"));
setupTextInterval();
// 初始化页面样式
initPageStyle();
}