下面列出了android.graphics.Paint#Align ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Paint createTypePaint(Typeface typeface, float textSize, int color, boolean antialias, Paint.Align align) {
Paint paint = new Paint();
paint.setColor(color);
paint.setTypeface(typeface);
paint.setTextSize(textSize);
paint.setAntiAlias(antialias);
paint.setTextAlign(align);
paint.setHinting(Paint.HINTING_ON);
paint.setDither(true);
return paint;
}
@Override
public void draw(@NonNull Rect outRect, @NonNull Canvas canvas, CharSequence text, @IntRange(from = 0) int start, @IntRange(from = 0) int end, float x, int top, int y, int bottom, @NonNull Paint paint) {
Paint.Align oldAlign = paint.getTextAlign();
Paint.Style oldStyle = paint.getStyle();
float oldStrokeWidth = paint.getStrokeWidth();
if (align != null) {
paint.setTextAlign(align);
}
if (style != null) {
paint.setStyle(style);
}
if (strokeWidth > 0) {
paint.setStrokeWidth(strokeWidth);
}
super.draw(outRect, canvas, text, start, end, x, top, y, bottom, paint);
switch (paint.getTextAlign()) {
case CENTER:
canvas.drawText(text, start, end, x + (outRect.right - outRect.left) / 2, y, paint);
break;
default:
canvas.drawText(text, start, end, x, y, paint);
break;
}
paint.setTextAlign(oldAlign);
paint.setStyle(oldStyle);
paint.setStrokeWidth(oldStrokeWidth);
}
float drawBase(Canvas canvas)
{
boolean rtl = Utilities.isTextRtl(m_item.m_title);
float verticalPosition = m_paints[0].getTextSize() + s_eightDp;
int startPadding = rtl ? SCREEN - s_eightDp : s_eightDp;
int endPadding = rtl ? s_eightDp : SCREEN - s_eightDp;
Paint.Align start = rtl ? Paint.Align.RIGHT : Paint.Align.LEFT;
Paint.Align end = rtl ? Paint.Align.LEFT : Paint.Align.RIGHT;
// Draw the time.
m_paints[1].setTextAlign(end);
canvas.drawText(getTime(m_item.m_time), endPadding, verticalPosition, m_paints[1]);
String[] info = {m_item.m_title, m_item.m_urlTrimmed};
// Draw the title and the url.
for(int i = 0; 2 > i; i++)
{
m_paints[i].setTextAlign(start);
canvas.drawText(info[i], startPadding, verticalPosition, m_paints[i]);
verticalPosition += m_paints[i].getTextSize();
}
return m_hasImage ? verticalPosition : verticalPosition + getDp(4.0F);
}
public static void drawText(Canvas c, String text, float x, float y,
Paint paint,
PointF anchor, float angleDegrees) {
float drawOffsetX = 0.f;
float drawOffsetY = 0.f;
paint.getTextBounds(text, 0, text.length(), mDrawTextRectBuffer);
final float lineHeight = mDrawTextRectBuffer.height();
// Android sometimes has pre-padding
drawOffsetX -= mDrawTextRectBuffer.left;
// Android does not snap the bounds to line boundaries,
// and draws from bottom to top.
// And we want to normalize it.
drawOffsetY += lineHeight;
// To have a consistent point of reference, we always draw left-aligned
Paint.Align originalTextAlign = paint.getTextAlign();
paint.setTextAlign(Paint.Align.LEFT);
if (angleDegrees != 0.f) {
// Move the text drawing rect in a way that it always rotates around its center
drawOffsetX -= mDrawTextRectBuffer.width() * 0.5f;
drawOffsetY -= lineHeight * 0.5f;
float translateX = x;
float translateY = y;
// Move the "outer" rect relative to the anchor, assuming its centered
if (anchor.x != 0.5f || anchor.y != 0.5f) {
final FSize rotatedSize = getSizeOfRotatedRectangleByDegrees(
mDrawTextRectBuffer.width(),
lineHeight,
angleDegrees);
translateX -= rotatedSize.width * (anchor.x - 0.5f);
translateY -= rotatedSize.height * (anchor.y - 0.5f);
}
c.save();
c.translate(translateX, translateY);
c.rotate(angleDegrees);
c.drawText(text, drawOffsetX, drawOffsetY, paint);
c.restore();
}
else {
if (anchor.x != 0.f || anchor.y != 0.f) {
drawOffsetX -= mDrawTextRectBuffer.width() * anchor.x;
drawOffsetY -= lineHeight * anchor.y;
}
drawOffsetX += x;
drawOffsetY += y;
c.drawText(text, drawOffsetX, drawOffsetY, paint);
}
paint.setTextAlign(originalTextAlign);
}
public Paint.Align getAlign() {
if (align == null) {
return defaultAlign;
}
return align;
}
/**
* Sets the paint alignment that draws the subtitle.
* @param align {@link android.graphics.Paint.Align}
*/
public void setSubtitleTextAlign(Paint.Align align) {
this.subtitlePaint.setTextAlign(align);
invalidateConfig();
}
/**
* 设置字体位置
*/
public void setTextAlign(Paint.Align textAlign) {
this.textAlign = textAlign;
}
public TextSpan(Paint.Align align, Paint.Style style, float strokeWidth, CharacterStyle... styles) {
super(styles);
this.align = align;
this.strokeWidth = strokeWidth;
this.style = style;
}
public static void drawXAxisValue(Canvas c, String text, float x, float y,
Paint paint,
MPPointF anchor, float angleDegrees) {
float drawOffsetX = 0.f;
float drawOffsetY = 0.f;
final float lineHeight = paint.getFontMetrics(mFontMetricsBuffer);
paint.getTextBounds(text, 0, text.length(), mDrawTextRectBuffer);
// Android sometimes has pre-padding
drawOffsetX -= mDrawTextRectBuffer.left;
// Android does not snap the bounds to line boundaries,
// and draws from bottom to top.
// And we want to normalize it.
drawOffsetY += -mFontMetricsBuffer.ascent;
// To have a consistent point of reference, we always draw left-aligned
Paint.Align originalTextAlign = paint.getTextAlign();
paint.setTextAlign(Paint.Align.LEFT);
if (angleDegrees != 0.f) {
// Move the text drawing rect in a way that it always rotates around its center
drawOffsetX -= mDrawTextRectBuffer.width() * 0.5f;
drawOffsetY -= lineHeight * 0.5f;
float translateX = x;
float translateY = y;
// Move the "outer" rect relative to the anchor, assuming its centered
if (anchor.x != 0.5f || anchor.y != 0.5f) {
final FSize rotatedSize = getSizeOfRotatedRectangleByDegrees(
mDrawTextRectBuffer.width(),
lineHeight,
angleDegrees);
translateX -= rotatedSize.width * (anchor.x - 0.5f);
translateY -= rotatedSize.height * (anchor.y - 0.5f);
FSize.recycleInstance(rotatedSize);
}
c.save();
c.translate(translateX, translateY);
c.rotate(angleDegrees);
c.drawText(text, drawOffsetX, drawOffsetY, paint);
c.restore();
} else {
if (anchor.x != 0.f || anchor.y != 0.f) {
drawOffsetX -= mDrawTextRectBuffer.width() * anchor.x;
drawOffsetY -= lineHeight * anchor.y;
}
drawOffsetX += x;
drawOffsetY += y;
c.drawText(text, drawOffsetX, drawOffsetY, paint);
}
paint.setTextAlign(originalTextAlign);
}
public static void drawMultilineText(Canvas c, StaticLayout textLayout,
float x, float y,
TextPaint paint,
MPPointF anchor, float angleDegrees) {
float drawOffsetX = 0.f;
float drawOffsetY = 0.f;
float drawWidth;
float drawHeight;
final float lineHeight = paint.getFontMetrics(mFontMetricsBuffer);
drawWidth = textLayout.getWidth();
drawHeight = textLayout.getLineCount() * lineHeight;
// Android sometimes has pre-padding
drawOffsetX -= mDrawTextRectBuffer.left;
// Android does not snap the bounds to line boundaries,
// and draws from bottom to top.
// And we want to normalize it.
drawOffsetY += drawHeight;
// To have a consistent point of reference, we always draw left-aligned
Paint.Align originalTextAlign = paint.getTextAlign();
paint.setTextAlign(Paint.Align.LEFT);
if (angleDegrees != 0.f) {
// Move the text drawing rect in a way that it always rotates around its center
drawOffsetX -= drawWidth * 0.5f;
drawOffsetY -= drawHeight * 0.5f;
float translateX = x;
float translateY = y;
// Move the "outer" rect relative to the anchor, assuming its centered
if (anchor.x != 0.5f || anchor.y != 0.5f) {
final FSize rotatedSize = getSizeOfRotatedRectangleByDegrees(
drawWidth,
drawHeight,
angleDegrees);
translateX -= rotatedSize.width * (anchor.x - 0.5f);
translateY -= rotatedSize.height * (anchor.y - 0.5f);
FSize.recycleInstance(rotatedSize);
}
c.save();
c.translate(translateX, translateY);
c.rotate(angleDegrees);
c.translate(drawOffsetX, drawOffsetY);
textLayout.draw(c);
c.restore();
} else {
if (anchor.x != 0.f || anchor.y != 0.f) {
drawOffsetX -= drawWidth * anchor.x;
drawOffsetY -= drawHeight * anchor.y;
}
drawOffsetX += x;
drawOffsetY += y;
c.save();
c.translate(drawOffsetX, drawOffsetY);
textLayout.draw(c);
c.restore();
}
paint.setTextAlign(originalTextAlign);
}
/**
* Draws the speed
*
* @param canvas the {@link Canvas} upon which to draw
*/
private void drawSpeed(Canvas canvas) {
mPaint.setTypeface(mSpeedTypeface);
int speed = (int) mOrientation.getLocation().getSpeed();
String uomStr;
switch (uom) {
case UOM_KMH:
speed /= KMH_IN_MPS;
uomStr = " km/h";
break;
case UOM_MPH:
speed /= MPH_IN_MPS;
uomStr = " mph";
break;
case UOM_KT:
speed /= KT_IN_MPS;
uomStr = " kt";
break;
case UOM_MPS:
default:
uomStr = " m/s";
}
if (speed > 1000) {
speed = 999;
}
final DecimalFormat smallNumberFormat = new DecimalFormat("0.0");
String speedStr = speed < 10 ? smallNumberFormat.format(speed)
: Integer.toString(speed);
String testStr = "000";
mPaint.setTextSize(SPEED_TEXT_HEIGHT);
mPaint.getTextBounds(testStr, 0, testStr.length(), mTextBounds);
mPaint.setTextSize(UOM_TEXT_HEIGHT);
mPaint.getTextBounds(uomStr, 0, uomStr.length(), mTextBounds);
Paint.Align oldAlign = mPaint.getTextAlign();
mPaint.setTextAlign(Paint.Align.RIGHT);
mPaint.setTextSize(SPEED_TEXT_HEIGHT);
mPaint.setColor(SPEED_COLOR);
canvas.drawText(speedStr, getWidth() - 218, getHeight() - 40, mPaint);
mPaint.setTextAlign(Paint.Align.LEFT);
mPaint.setTextSize(UOM_TEXT_HEIGHT);
mPaint.setColor(SPEED_UOM_COLOR);
canvas.drawText(uomStr, 442, getHeight() - 40, mPaint);
mPaint.setTextAlign(oldAlign);
}
public static void drawMultilineText(Canvas c, StaticLayout textLayout,
float x, float y,
TextPaint paint,
PointF anchor, float angleDegrees) {
float drawOffsetX = 0.f;
float drawOffsetY = 0.f;
float drawWidth;
float drawHeight;
final float lineHeight = paint.getFontMetrics(mFontMetricsBuffer);
drawWidth = textLayout.getWidth();
drawHeight = textLayout.getLineCount() * lineHeight;
// Android sometimes has pre-padding
drawOffsetX -= mDrawTextRectBuffer.left;
// Android does not snap the bounds to line boundaries,
// and draws from bottom to top.
// And we want to normalize it.
drawOffsetY += drawHeight;
// To have a consistent point of reference, we always draw left-aligned
Paint.Align originalTextAlign = paint.getTextAlign();
paint.setTextAlign(Paint.Align.LEFT);
if (angleDegrees != 0.f) {
// Move the text drawing rect in a way that it always rotates around its center
drawOffsetX -= drawWidth * 0.5f;
drawOffsetY -= drawHeight * 0.5f;
float translateX = x;
float translateY = y;
// Move the "outer" rect relative to the anchor, assuming its centered
if (anchor.x != 0.5f || anchor.y != 0.5f) {
final FSize rotatedSize = getSizeOfRotatedRectangleByDegrees(
drawWidth,
drawHeight,
angleDegrees);
translateX -= rotatedSize.width * (anchor.x - 0.5f);
translateY -= rotatedSize.height * (anchor.y - 0.5f);
}
c.save();
c.translate(translateX, translateY);
c.rotate(angleDegrees);
c.translate(drawOffsetX, drawOffsetY);
textLayout.draw(c);
c.restore();
} else {
if (anchor.x != 0.f || anchor.y != 0.f) {
drawOffsetX -= drawWidth * anchor.x;
drawOffsetY -= drawHeight * anchor.y;
}
drawOffsetX += x;
drawOffsetY += y;
c.save();
c.translate(drawOffsetX, drawOffsetY);
textLayout.draw(c);
c.restore();
}
paint.setTextAlign(originalTextAlign);
}
public static void drawXAxisValue(Canvas c, String text, float x, float y,
Paint paint,
MPPointF anchor, float angleDegrees) {
float drawOffsetX = 0.f;
float drawOffsetY = 0.f;
final float lineHeight = paint.getFontMetrics(mFontMetricsBuffer);
paint.getTextBounds(text, 0, text.length(), mDrawTextRectBuffer);
// Android sometimes has pre-padding
drawOffsetX -= mDrawTextRectBuffer.left;
// Android does not snap the bounds to line boundaries,
// and draws from bottom to top.
// And we want to normalize it.
drawOffsetY += -mFontMetricsBuffer.ascent;
// To have a consistent point of reference, we always draw left-aligned
Paint.Align originalTextAlign = paint.getTextAlign();
paint.setTextAlign(Paint.Align.LEFT);
if (angleDegrees != 0.f) {
// Move the text drawing rect in a way that it always rotates around its center
drawOffsetX -= mDrawTextRectBuffer.width() * 0.5f;
drawOffsetY -= lineHeight * 0.5f;
float translateX = x;
float translateY = y;
// Move the "outer" rect relative to the anchor, assuming its centered
if (anchor.x != 0.5f || anchor.y != 0.5f) {
final FSize rotatedSize = getSizeOfRotatedRectangleByDegrees(
mDrawTextRectBuffer.width(),
lineHeight,
angleDegrees);
translateX -= rotatedSize.width * (anchor.x - 0.5f);
translateY -= rotatedSize.height * (anchor.y - 0.5f);
FSize.recycleInstance(rotatedSize);
}
c.save();
c.translate(translateX, translateY);
c.rotate(angleDegrees);
c.drawText(text, drawOffsetX, drawOffsetY, paint);
c.restore();
} else {
if (anchor.x != 0.f || anchor.y != 0.f) {
drawOffsetX -= mDrawTextRectBuffer.width() * anchor.x;
drawOffsetY -= lineHeight * anchor.y;
}
drawOffsetX += x;
drawOffsetY += y;
c.drawText(text, drawOffsetX, drawOffsetY, paint);
}
paint.setTextAlign(originalTextAlign);
}
public TextSpan(Paint.Align align, Paint.Style style, float strokeWidth, CharacterStyle... styles) {
super(styles);
this.align = align;
this.strokeWidth = strokeWidth;
this.style = style;
}
public void setAlign(Paint.Align align) {
paint.setTextAlign(align);
}
public static void drawXAxisValue(Canvas c, String text, float x, float y,
Paint paint,
MPPointF anchor, float angleDegrees) {
float drawOffsetX = 0.f;
float drawOffsetY = 0.f;
final float lineHeight = paint.getFontMetrics(mFontMetricsBuffer);
paint.getTextBounds(text, 0, text.length(), mDrawTextRectBuffer);
// Android sometimes has pre-padding
drawOffsetX -= mDrawTextRectBuffer.left;
// Android does not snap the bounds to line boundaries,
// and draws from bottom to top.
// And we want to normalize it.
drawOffsetY += -mFontMetricsBuffer.ascent;
// To have a consistent point of reference, we always draw left-aligned
Paint.Align originalTextAlign = paint.getTextAlign();
paint.setTextAlign(Paint.Align.LEFT);
if (angleDegrees != 0.f) {
// Move the text drawing rect in a way that it always rotates around its center
drawOffsetX -= mDrawTextRectBuffer.width() * 0.5f;
drawOffsetY -= lineHeight * 0.5f;
float translateX = x;
float translateY = y;
// Move the "outer" rect relative to the anchor, assuming its centered
if (anchor.x != 0.5f || anchor.y != 0.5f) {
final FSize rotatedSize = getSizeOfRotatedRectangleByDegrees(
mDrawTextRectBuffer.width(),
lineHeight,
angleDegrees);
translateX -= rotatedSize.width * (anchor.x - 0.5f);
translateY -= rotatedSize.height * (anchor.y - 0.5f);
FSize.recycleInstance(rotatedSize);
}
c.save();
c.translate(translateX, translateY);
c.rotate(angleDegrees);
c.drawText(text, drawOffsetX, drawOffsetY, paint);
c.restore();
} else {
if (anchor.x != 0.f || anchor.y != 0.f) {
drawOffsetX -= mDrawTextRectBuffer.width() * anchor.x;
drawOffsetY -= lineHeight * anchor.y;
}
drawOffsetX += x;
drawOffsetY += y;
c.drawText(text, drawOffsetX, drawOffsetY, paint);
}
paint.setTextAlign(originalTextAlign);
}
public static void drawMultilineText(Canvas c, StaticLayout textLayout,
float x, float y,
TextPaint paint,
MPPointF anchor, float angleDegrees) {
float drawOffsetX = 0.f;
float drawOffsetY = 0.f;
float drawWidth;
float drawHeight;
final float lineHeight = paint.getFontMetrics(mFontMetricsBuffer);
drawWidth = textLayout.getWidth();
drawHeight = textLayout.getLineCount() * lineHeight;
// Android sometimes has pre-padding
drawOffsetX -= mDrawTextRectBuffer.left;
// Android does not snap the bounds to line boundaries,
// and draws from bottom to top.
// And we want to normalize it.
drawOffsetY += drawHeight;
// To have a consistent point of reference, we always draw left-aligned
Paint.Align originalTextAlign = paint.getTextAlign();
paint.setTextAlign(Paint.Align.LEFT);
if (angleDegrees != 0.f) {
// Move the text drawing rect in a way that it always rotates around its center
drawOffsetX -= drawWidth * 0.5f;
drawOffsetY -= drawHeight * 0.5f;
float translateX = x;
float translateY = y;
// Move the "outer" rect relative to the anchor, assuming its centered
if (anchor.x != 0.5f || anchor.y != 0.5f) {
final FSize rotatedSize = getSizeOfRotatedRectangleByDegrees(
drawWidth,
drawHeight,
angleDegrees);
translateX -= rotatedSize.width * (anchor.x - 0.5f);
translateY -= rotatedSize.height * (anchor.y - 0.5f);
FSize.recycleInstance(rotatedSize);
}
c.save();
c.translate(translateX, translateY);
c.rotate(angleDegrees);
c.translate(drawOffsetX, drawOffsetY);
textLayout.draw(c);
c.restore();
} else {
if (anchor.x != 0.f || anchor.y != 0.f) {
drawOffsetX -= drawWidth * anchor.x;
drawOffsetY -= drawHeight * anchor.y;
}
drawOffsetX += x;
drawOffsetY += y;
c.save();
c.translate(drawOffsetX, drawOffsetY);
textLayout.draw(c);
c.restore();
}
paint.setTextAlign(originalTextAlign);
}
/**
* 获取字体位置
*
* @return Align
*/
public Paint.Align getTextAlign() {
return textAlign;
}
/**
* Returns the text alignment of the description.
*
* @return
*/
public Paint.Align getTextAlign() {
return mTextAlign;
}
/**
* Sets the text alignment of the description text. Default RIGHT.
*
* @param align
*/
public void setTextAlign(Paint.Align align) {
this.mTextAlign = align;
}