下面列出了android.text.TextPaint#setSubpixelText ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@SuppressWarnings("ResourceType") // We're hacking `spacingMult = styledAttributes.getFloat`
SubtitlePainter(Context context) {
int[] viewAttr = {android.R.attr.lineSpacingExtra, android.R.attr.lineSpacingMultiplier};
TypedArray styledAttributes = context.obtainStyledAttributes(null, viewAttr, 0, 0);
spacingAdd = styledAttributes.getDimensionPixelSize(0, 0);
spacingMult = styledAttributes.getFloat(1, 1);
styledAttributes.recycle();
Resources resources = context.getResources();
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
int twoDpInPx = Math.round((TWO_DP * displayMetrics.densityDpi) / DisplayMetrics.DENSITY_DEFAULT);
cornerRadius = twoDpInPx;
outlineWidth = twoDpInPx;
shadowRadius = twoDpInPx;
shadowOffset = twoDpInPx;
textPaint = new TextPaint();
textPaint.setAntiAlias(true);
textPaint.setSubpixelText(true);
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Style.FILL);
}
@SuppressWarnings("ResourceType")
public SubtitlePainter(Context context) {
int[] viewAttr = {android.R.attr.lineSpacingExtra, android.R.attr.lineSpacingMultiplier};
TypedArray styledAttributes = context.obtainStyledAttributes(null, viewAttr, 0, 0);
spacingAdd = styledAttributes.getDimensionPixelSize(0, 0);
spacingMult = styledAttributes.getFloat(1, 1);
styledAttributes.recycle();
Resources resources = context.getResources();
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
int twoDpInPx = Math.round((2f * displayMetrics.densityDpi) / DisplayMetrics.DENSITY_DEFAULT);
cornerRadius = twoDpInPx;
outlineWidth = twoDpInPx;
shadowRadius = twoDpInPx;
shadowOffset = twoDpInPx;
textPaint = new TextPaint();
textPaint.setAntiAlias(true);
textPaint.setSubpixelText(true);
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Style.FILL);
}
private void init() {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
mBadgeTextRect = new RectF();
mBadgeBackgroundRect = new RectF();
mDragPath = new Path();
mBadgeCenter = new PointF();
mDragCenter = new PointF();
mRowBadgeCenter = new PointF();
mControlPoint = new PointF();
mInnertangentPoints = new ArrayList<>();
mBadgeTextPaint = new TextPaint();
mBadgeTextPaint.setAntiAlias(true);
mBadgeTextPaint.setSubpixelText(true);
mBadgeTextPaint.setFakeBoldText(true);
mBadgeTextPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
mBadgeBackgroundPaint = new Paint();
mBadgeBackgroundPaint.setAntiAlias(true);
mBadgeBackgroundPaint.setStyle(Paint.Style.FILL);
mBadgeBackgroundBorderPaint = new Paint();
mBadgeBackgroundBorderPaint.setAntiAlias(true);
mBadgeBackgroundBorderPaint.setStyle(Paint.Style.STROKE);
mColorBackground = 0xFFE84E40;
mColorBadgeText = 0xFFFFFFFF;
mBadgeTextSize = DisplayUtil.dp2px(getContext(), 11);
mBadgePadding = DisplayUtil.dp2px(getContext(), 5);
mBadgeNumber = 0;
mBadgeGravity = Gravity.END | Gravity.TOP;
mGravityOffsetX = DisplayUtil.dp2px(getContext(), 1);
mGravityOffsetY = DisplayUtil.dp2px(getContext(), 1);
mFinalDragDistance = DisplayUtil.dp2px(getContext(), 90);
mShowShadow = true;
mDrawableBackgroundClip = false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setTranslationZ(1000);
}
}
private void init() {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
mBadgeTextRect = new RectF();
mBadgeBackgroundRect = new RectF();
mDragPath = new Path();
mBadgeCenter = new PointF();
mDragCenter = new PointF();
mRowBadgeCenter = new PointF();
mControlPoint = new PointF();
mInnertangentPoints = new ArrayList<>();
mBadgeTextPaint = new TextPaint();
mBadgeTextPaint.setAntiAlias(true);
mBadgeTextPaint.setSubpixelText(true);
mBadgeTextPaint.setFakeBoldText(true);
mBadgeTextPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
mBadgeBackgroundPaint = new Paint();
mBadgeBackgroundPaint.setAntiAlias(true);
mBadgeBackgroundPaint.setStyle(Paint.Style.FILL);
mBadgeBackgroundBorderPaint = new Paint();
mBadgeBackgroundBorderPaint.setAntiAlias(true);
mBadgeBackgroundBorderPaint.setStyle(Paint.Style.STROKE);
mColorBackground = 0xFFE84E40;
mColorBadgeText = 0xFFFFFFFF;
mBadgeTextSize = DisplayUtil.dp2px(getContext(), 11);
mBadgePadding = DisplayUtil.dp2px(getContext(), 5);
mBadgeNumber = 0;
mBadgeGravity = Gravity.END | Gravity.TOP;
mGravityOffsetX = DisplayUtil.dp2px(getContext(), 1);
mGravityOffsetY = DisplayUtil.dp2px(getContext(), 1);
mFinalDragDistance = DisplayUtil.dp2px(getContext(), 90);
mShowShadow = true;
mDrawableBackgroundClip = false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setTranslationZ(1000);
}
}
@Test
public void testHintCollapsedHeightMeasuredFromBaseline() {
final Activity activity = activityTestRule.getActivity();
final TextInputLayout layout = activity.findViewById(R.id.textinput_box_outline);
// Create a TextView and set it to a custom text and TextAppearance.
TextView textView = new TextView(layout.getContext());
textView.setText(HINT_TEXT);
TextViewCompat.setTextAppearance(textView, R.style.TextMediumGreenStyle);
// Create a TextPaint to measure the text's height from the baseline, and port over aspects of
// the TextAppearance from the textView.
TextPaint textPaint = new TextPaint();
textPaint.setSubpixelText(true);
textPaint.setColor(textView.getCurrentTextColor());
textPaint.setTypeface(textView.getTypeface());
textPaint.setTextSize(textView.getTextSize());
// Set the same custom text and text appearance on the outline box's hint.
onView(withId(R.id.textinput_box_outline))
.perform(setHint(HINT_TEXT))
.perform(setHintTextAppearance(R.style.TextMediumGreenStyle));
// Check that the hint's collapsed height is the same as the TextPaint's height, measured from
// the baseline (-ascent).
assertEquals(layout.getHintCollapsedTextHeight(), -textPaint.ascent(), 0.01);
}
public TextPaint createTextPaint(Typeface typeface, int size, int color) {
TextPaint res = new TextPaint(Paint.ANTI_ALIAS_FLAG);
res.setSubpixelText(true);
res.setTypeface(typeface);
res.setTextSize(Screen.sp(size));
res.setColor(color);
return res;
}
/**
* 初始化画笔
*/
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();
}
/**
* 作用:设置与文字相关的参数
*/
private void setTextParams() {
// 文字大小
mTextSize = ScreenUtils.spToPx(mSettingManager.getTextSize());
// 行间距
mTextInterval = ScreenUtils.dpToPx(mSettingManager.getLineSpacing());
// 段落间距
mTextPara = ScreenUtils.dpToPx(mSettingManager.getParagraphSpacing());
Typeface typeface;
try {
if (mSettingManager.getFontPath() != null) {
typeface = Typeface.createFromFile(mSettingManager.getFontPath());
} else {
typeface = Typeface.SANS_SERIF;
}
} catch (Exception e) {
ToastUtils.toast(mContext, "字体文件未找,到恢复默认字体");
mSettingManager.setReadBookFont(null);
typeface = Typeface.SANS_SERIF;
}
// 绘制提示的画笔
mTipPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mTipPaint.setColor(mTextColor);
mTipPaint.setTextAlign(Paint.Align.LEFT); // 绘制的起始点
mTipPaint.setTextSize(ScreenUtils.spToPx(DEFAULT_TIP_SIZE)); // Tip默认的字体大小
mTipPaint.setTypeface(typeface);
mTipPaint.setFakeBoldText(mSettingManager.getTextBold());
mTipPaint.setSubpixelText(true);
mTipPaint.setDither(true);
// 绘制标题的画笔
mTitlePaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mTitlePaint.setColor(mTextColor);
mTitlePaint.setTextSize(mTextSize * 1.25f);
mTitlePaint.setStyle(Paint.Style.FILL_AND_STROKE);
mTitlePaint.setTypeface(typeface);
mTitlePaint.setFakeBoldText(true);
mTitlePaint.setSubpixelText(true);
mTitlePaint.setDither(true);
// 绘制页面内容的画笔
mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setColor(mTextColor);
mTextPaint.setTextSize(mTextSize);
mTextPaint.setTypeface(typeface);
mTextPaint.setFakeBoldText(mSettingManager.getTextBold());
mTextPaint.setSubpixelText(true);
mTextPaint.setDither(true);
}