Java Code Examples for android.graphics.Paint#Align

The following examples show how to use android.graphics.Paint#Align . 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: ViewFeedItem.java    From rss with GNU General Public License v3.0 5 votes vote down vote up
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);
}
 
Example 2
Source File: Spans.java    From Pioneer with Apache License 2.0 5 votes vote down vote up
@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 3
Source File: PaintUtils.java    From tilt-game-android with MIT License 5 votes vote down vote up
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;
}
 
Example 4
Source File: SpeedHudView.java    From SpeedHud with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example 5
Source File: Utils.java    From StockChart-MPAndroidChart with MIT License 4 votes vote down vote up
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);
}
 
Example 6
Source File: Utils.java    From StockChart-MPAndroidChart with MIT License 4 votes vote down vote up
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 7
Source File: DrawText.java    From DrawReceipt with Apache License 2.0 4 votes vote down vote up
public void setAlign(Paint.Align align) {
    paint.setTextAlign(align);
}
 
Example 8
Source File: Spans.java    From Pioneer with Apache License 2.0 4 votes vote down vote up
public TextSpan(Paint.Align align, Paint.Style style, float strokeWidth, CharacterStyle... styles) {
    super(styles);
    this.align = align;
    this.strokeWidth = strokeWidth;
    this.style = style;
}
 
Example 9
Source File: Utils.java    From android-kline with Apache License 2.0 4 votes vote down vote up
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 JNChartDemo with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example 11
Source File: Utils.java    From Ticket-Analysis with MIT License 4 votes vote down vote up
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);
}
 
Example 12
Source File: Utils.java    From Ticket-Analysis with MIT License 4 votes vote down vote up
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 13
Source File: Spans.java    From Pioneer with Apache License 2.0 4 votes vote down vote up
public TextSpan(Paint.Align align, Paint.Style style, float strokeWidth, CharacterStyle... styles) {
    super(styles);
    this.align = align;
    this.strokeWidth = strokeWidth;
    this.style = style;
}
 
Example 14
Source File: Column.java    From DoraemonKit with Apache License 2.0 4 votes vote down vote up
/**
 * 设置字体位置
 */
public void setTextAlign(Paint.Align textAlign) {
    this.textAlign = textAlign;
}
 
Example 15
Source File: ImageTextStateDisplay.java    From empty-state-recyclerview with MIT License 4 votes vote down vote up
/**
 * 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();
}
 
Example 16
Source File: FontStyle.java    From DoraemonKit with Apache License 2.0 4 votes vote down vote up
public Paint.Align getAlign() {
    if (align == null) {
        return defaultAlign;
    }
    return align;
}
 
Example 17
Source File: Utils.java    From Stayfit with Apache License 2.0 4 votes vote down vote up
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 18
Source File: Description.java    From StockChart-MPAndroidChart with MIT License 2 votes vote down vote up
/**
 * Returns the text alignment of the description.
 *
 * @return
 */
public Paint.Align getTextAlign() {
    return mTextAlign;
}
 
Example 19
Source File: Description.java    From Ticket-Analysis with MIT License 2 votes vote down vote up
/**
 * Sets the text alignment of the description text. Default RIGHT.
 *
 * @param align
 */
public void setTextAlign(Paint.Align align) {
    this.mTextAlign = align;
}
 
Example 20
Source File: Column.java    From DoraemonKit with Apache License 2.0 2 votes vote down vote up
/**
 * 获取字体位置
 *
 * @return Align
 */
public Paint.Align getTextAlign() {
    return textAlign;
}