下面列出了android.text.TextPaint#setFlags ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void applyStyle(TextPaint p) {
Typeface typeface = getTypeface();
if (typeface != null) {
p.setTypeface(typeface);
}
if ((flags & FLAG_STYLE_UNDERLINE) != 0) {
p.setFlags(p.getFlags() | Paint.UNDERLINE_TEXT_FLAG);
} else {
p.setFlags(p.getFlags() &~ Paint.UNDERLINE_TEXT_FLAG);
}
if ((flags & FLAG_STYLE_STRIKE) != 0) {
p.setFlags(p.getFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
} else {
p.setFlags(p.getFlags() &~ Paint.STRIKE_THRU_TEXT_FLAG);
}
}
@Override
public void updateMeasureState(TextPaint p) {
p.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
@Override
public void updateMeasureState(TextPaint p) {
if (typeface != null) {
p.setTypeface(typeface);
}
if (textSize != 0) {
p.setTextSize(textSize);
}
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
@Override
public void updateDrawState(TextPaint tp) {
if (typeface != null) {
tp.setTypeface(typeface);
}
if (textSize != 0) {
tp.setTextSize(textSize);
}
if (color != 0) {
tp.setColor(color);
}
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
@Override
public void updateDrawState(TextPaint tp) {
tp.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
@Override
public void updateDrawState(TextPaint p) {
if (textPaint != null) {
p.setColor(textPaint.getColor());
p.setTypeface(textPaint.getTypeface());
p.setFlags(textPaint.getFlags());
p.setTextSize(textPaint.getTextSize());
p.baselineShift = textPaint.baselineShift;
p.bgColor = textPaint.bgColor;
}
}
@Override
public void updateDrawState(TextPaint p) {
p.setColor(textPaint.getColor());
p.setTypeface(textPaint.getTypeface());
p.setFlags(textPaint.getFlags());
p.setTextSize(textPaint.getTextSize());
p.baselineShift = textPaint.baselineShift;
p.bgColor = textPaint.bgColor;
}
@Override
public void updateDrawState(TextPaint tp) {
if (typeface != null) {
tp.setTypeface(typeface);
}
if (textSize != 0) {
tp.setTextSize(textSize);
}
if (color != 0) {
tp.setColor(color);
}
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
@Override
public void updateMeasureState(TextPaint p) {
if (textSize != 0) {
p.setTextSize(textSize);
}
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
style.applyStyle(p);
}
@Override
public void updateDrawState(TextPaint p) {
if (textPaint != null) {
p.setColor(textPaint.getColor());
p.setTypeface(textPaint.getTypeface());
p.setFlags(textPaint.getFlags());
p.setTextSize(textPaint.getTextSize());
p.baselineShift = textPaint.baselineShift;
p.bgColor = textPaint.bgColor;
}
}
@Override
public void updateMeasureState(TextPaint p) {
p.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
@Override
public void updateMeasureState(TextPaint p) {
p.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
@Override
public void updateMeasureState(TextPaint p) {
if (textPaint != null) {
p.setColor(textPaint.getColor());
p.setTypeface(textPaint.getTypeface());
p.setFlags(textPaint.getFlags());
p.setTextSize(textPaint.getTextSize());
p.baselineShift = textPaint.baselineShift;
p.bgColor = textPaint.bgColor;
}
}
@Override
public void updateDrawState(TextPaint p) {
if (textPaint != null) {
p.setColor(textPaint.getColor());
p.setTypeface(textPaint.getTypeface());
p.setFlags(textPaint.getFlags());
p.setTextSize(textPaint.getTextSize());
p.baselineShift = textPaint.baselineShift;
p.bgColor = textPaint.bgColor;
}
}
@Override
public void updateDrawState(TextPaint paint) {
paint.setTypeface(mTypeface);
paint.setFlags(paint.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
private void drawStopWatch(Canvas canvas) {
if (showBorder) {
drawCustomBorder(canvas);
}
if (clockBackground != null) {
Paint paint = new Paint();
paint.setAntiAlias(true);
Bitmap bitmap = ((BitmapDrawable) clockBackground).getBitmap();
RectF rectF = new RectF(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Canvas tCanvas = new Canvas(output);
switch (borderStyle) {
case rectangle:
tCanvas.drawRect(defaultRectF, paint);
break;
case circle:
tCanvas.drawCircle(centerX, centerY, radius, paint);
break;
case rounded_rectangle:
float rx = radius - (radius * (100 - borderRadiusRx)) / 100;
float ry = radius - (radius * (100 - borderRadiusRy)) / 100;
tCanvas.drawRoundRect(defaultRectF, rx, ry, paint);
break;
}
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
tCanvas.drawBitmap(bitmap, null, rectF, paint);
canvas.drawBitmap(output, null, rectF, new Paint());
}
TextPaint textPaint = new TextPaint();
textPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
textPaint.setAntiAlias(true);
textPaint.setTextSize(size * 0.25f);
textPaint.setColor(this.valuesColor);
textPaint.setTypeface(this.valuesFont);
String stopwatchValue = String.format(Locale.getDefault(), "%02d", mMinutes) + ":" + String.format(Locale.getDefault(), "%02d", mSeconds);
SpannableStringBuilder spannableString = new SpannableStringBuilder(stopwatchValue);
StaticLayout layout = new StaticLayout(spannableString, textPaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER, 1, 1, true);
canvas.translate(centerX - layout.getWidth() / 2, centerY - layout.getHeight() / 2);
layout.draw(canvas);
}
private void drawHoursValues(Canvas canvas) {
if (!showHoursValues)
return;
Rect rect = new Rect();
TextPaint textPaint = new TextPaint();
textPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
textPaint.setAntiAlias(true);
textPaint.setColor(this.valuesColor);
textPaint.setTypeface(this.valuesFont);
textPaint.setTextSize(size * DEFAULT_HOURS_VALUES_TEXT_SIZE);
float degreeSpace = 0f;
if (showDegrees)
degreeSpace = DEFAULT_DEGREE_STROKE_WIDTH + 0.06f;
int rText = (int) (centerX - (size * DEFAULT_HOURS_VALUES_TEXT_SIZE) - (size * degreeSpace));
for (int i = FULL_ANGLE; i > 0; i = i - valueStep.getId()) {
int value = i / 30;
String formatted;
switch (valueType) {
case roman:
formatted = ClockUtils.toRoman(value);
break;
case arabic:
formatted = ClockUtils.toArabic(value);
break;
default:
formatted = String.format(Locale.getDefault(), "%02d", value);
break;
}
if (valueDisposition.getId() == 0) {
if ((i % REGULAR_ANGLE) == 0) {
textPaint.setTextSize(size * DEFAULT_HOURS_VALUES_TEXT_SIZE);
textPaint.setAlpha(FULL_ALPHA);
} else {
textPaint.setTextSize(size * (DEFAULT_HOURS_VALUES_TEXT_SIZE - 0.03f));
textPaint.setAlpha(CUSTOM_ALPHA);
}
} else {
textPaint.setTextSize(size * DEFAULT_HOURS_VALUES_TEXT_SIZE);
textPaint.setAlpha(FULL_ALPHA);
}
int textX = (int) (centerX + rText * Math.cos(Math.toRadians(REGULAR_ANGLE - i)));
int textY = (int) (centerX - rText * Math.sin(Math.toRadians(REGULAR_ANGLE - i)));
textPaint.getTextBounds(formatted, 0, formatted.length(), rect);
canvas.drawText(formatted, textX - rect.width() / formatted.length(), textY + rect.height() / formatted.length(), textPaint);
}
}
@Override
public void updateDrawState(TextPaint tp) {
tp.setTypeface(typeface);
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
@Override public void updateDrawState(TextPaint tp) {
tp.setTypeface(typeface);
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
public static Drawable createBadgeDrawable(Context context, BootstrapBrand brand, int width,
int height, String badgeText, boolean insideAnObject) {
if (badgeText == null) {
return null;
}
else {
Paint badgePaint = new Paint();
Rect canvasBounds = new Rect();
TextPaint badgeTextPaint = new TextPaint();
badgePaint.setFlags(Paint.ANTI_ALIAS_FLAG);
badgeTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
badgeTextPaint.setTextAlign(Paint.Align.CENTER);
badgeTextPaint.setTextSize((float) (height * 0.7));
if (insideAnObject) {
badgePaint.setColor(brand.defaultTextColor(context));
badgeTextPaint.setColor(brand.defaultFill(context));
}
else {
badgePaint.setColor(brand.defaultFill(context));
badgeTextPaint.setColor(brand.defaultTextColor(context));
}
int rectLength = (int) badgeTextPaint.measureText(badgeText);
Bitmap canvasBitmap = Bitmap.createBitmap(width + rectLength, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(canvasBitmap);
canvas.getClipBounds(canvasBounds);
int firstCircleDx = canvasBounds.left + canvasBounds.height() / 2;
int circleDy = canvasBounds.height() / 2;
int circleRadius = canvasBounds.height() / 2;
int secondCircleDx = firstCircleDx + rectLength;
Rect rect = new Rect();
rect.left = firstCircleDx;
rect.top = 0;
rect.right = rect.left + rectLength;
rect.bottom = canvasBounds.height();
canvas.drawCircle(firstCircleDx, circleDy, circleRadius, badgePaint);
canvas.drawRect(rect, badgePaint);
canvas.drawCircle(secondCircleDx, circleDy, circleRadius, badgePaint);
canvas.drawText(badgeText, canvasBounds.width() / 2, canvasBounds.height() / 2 - ((badgeTextPaint.descent() +
badgeTextPaint.ascent()) / 2),
badgeTextPaint);
return new BitmapDrawable(context.getResources(), canvasBitmap);
}
}