Java Code Examples for android.graphics.Paint#getTextAlign()
The following examples show how to use
android.graphics.Paint#getTextAlign() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextStateDisplay.java From empty-state-recyclerview with MIT License | 6 votes |
private float getDx(final int width, final int horizontalGravity, final Paint paint, final StaticLayout layout) { final boolean centered = paint.getTextAlign() == Paint.Align.CENTER; final float dx; switch (horizontalGravity) { // No support for GravityCompat.END case Gravity.CENTER_HORIZONTAL: dx = (width >> 1) - (centered ? 0 : (layout.getWidth() >> 1) - getPaddingLeft()); break; default: case GravityCompat.START: dx = getPaddingLeft(); break; } return dx; }
Example 2
Source File: DrawUtils.java From DoraemonKit with Apache License 2.0 | 5 votes |
public static float getTextCenterX(int left, int right, Paint paint){ Paint.Align align = paint.getTextAlign(); if(align == Paint.Align.RIGHT){ return right; }else if(align == Paint.Align.LEFT){ return left; }else{ return (right +left)/2; } }
Example 3
Source File: DrawTextView.java From zone-sdk with MIT License | 5 votes |
private void drawText_(Canvas canvas, Paint paint, int i) { int baseLineY = (getHeight() - 100 - Margin) - (TextSize + Margin) * i; canvas.drawText(content, getWidth() / 2, baseLineY, paint); //baseLine drawLine(canvas, baseLineY, Color.BLUE); //fm drawLine(canvas, baseLineY + fm.top, Color.BLACK); drawLine(canvas, baseLineY + fm.ascent, Color.MAGENTA); drawLine(canvas, baseLineY + fm.descent, Color.GREEN); drawLine(canvas, baseLineY + fm.bottom, Color.RED); // drawLine(canvas,baseLineY+fm.leading,Color.MAGENTA); int width = (int) paint.measureText(content); Rect bounds = new Rect(); paint.getTextBounds(content, 0, content.length(), bounds); switch (paint.getTextAlign()) { case LEFT: bounds.offset(getWidth() / 2, baseLineY); break; case RIGHT: bounds.offset(getWidth() / 2 - width, baseLineY); break; case CENTER: bounds.offset((getWidth() - width) / 2, baseLineY); break; } canvas.drawRect(bounds, paintStokeBlue); }
Example 4
Source File: DrawUtils.java From zone-sdk with MIT License | 5 votes |
private Text(Canvas canvas, String content, float x, float y, @NonNull Paint paint) { this.canvas = canvas; this.content = content; this.x = x; this.y = y; this.paint = paint; oldAlign = paint.getTextAlign(); }
Example 5
Source File: Spans.java From Pioneer with Apache License 2.0 | 5 votes |
@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); }
Example 6
Source File: Spans.java From Pioneer with Apache License 2.0 | 5 votes |
@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); }
Example 7
Source File: ChartUtils.java From fingen with Apache License 2.0 | 4 votes |
public static void drawXAxisValue(Canvas c, String text, float x, float y, Paint paint, PointF anchor, float angleDegrees, int color) { 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); Paint.Style oldStyle = paint.getStyle(); paint.setStyle(Paint.Style.STROKE); float oldStroleWidth = paint.getStrokeWidth(); paint.setStrokeWidth(ScreenUtils.dpToPx(3f, FGApplication.getContext())); int oldColor = paint.getColor(); paint.setColor(color); 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); paint.setStyle(oldStyle); paint.setStrokeWidth(oldStroleWidth); paint.setColor(oldColor); }
Example 8
Source File: Utils.java From StockChart-MPAndroidChart with MIT License | 4 votes |
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); }
Example 9
Source File: Utils.java From Ticket-Analysis with MIT License | 4 votes |
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); }
Example 10
Source File: Utils.java From android-kline with Apache License 2.0 | 4 votes |
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); }
Example 11
Source File: Utils.java From Stayfit with Apache License 2.0 | 4 votes |
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); }
Example 12
Source File: Utils.java From NetKnight with Apache License 2.0 | 4 votes |
public static void drawXAxisValue(Canvas c, String text, float x, float y, Paint paint, PointF 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); } 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); }
Example 13
Source File: Utils.java From JNChartDemo with Apache License 2.0 | 4 votes |
public static void drawXAxisValue(Canvas c, String text, float x, float y, Paint paint, PointF 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); } 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); }