Java Code Examples for android.text.StaticLayout#getWidth()

The following examples show how to use android.text.StaticLayout#getWidth() . 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 vote down vote up
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: TextSticker.java    From EasyPhotos with Apache License 2.0 5 votes vote down vote up
private void resetSize() {
    textLayout = new StaticLayout(text, textPaint, textLayoutWidth,
            Layout.Alignment.ALIGN_CENTER, 1.0F, 0.0F, true);
    textWidth = minWidth;
    textHeight = minHeight;
    if (textWidth < textLayout.getWidth()) {
        textWidth = textLayout.getWidth();
    }
    if (textHeight < textLayout.getHeight()) {
        textHeight = textLayout.getHeight();
    }
    minScale = minWidth / textWidth;

}
 
Example 3
Source File: TextSticker.java    From imsdk-android with MIT License 5 votes vote down vote up
private void resetSize() {
    textLayout = new StaticLayout(text, textPaint, textLayoutWidth,
            Layout.Alignment.ALIGN_CENTER, 1.0F, 0.0F, true);
    textWidth = minWidth;
    textHeight = minHeight;
    if (textWidth < textLayout.getWidth()) {
        textWidth = textLayout.getWidth();
    }
    if (textHeight < textLayout.getHeight()) {
        textHeight = textLayout.getHeight();
    }
    minScale = minWidth / textWidth;

}
 
Example 4
Source File: Button.java    From Carbon with Apache License 2.0 5 votes vote down vote up
public boolean testSize(float suggestedSize, RectF availableSpace) {
    paint.setTextSize(suggestedSize);
    paint.setTypeface(getTypeface());
    String text = getText().toString();
    if (maxLines == 1) {
        textRect.bottom = paint.getFontSpacing();
        textRect.right = paint.measureText(text);
        return availableSpace.width() >= textRect.right && availableSpace.height() >= textRect.bottom;
    } else {
        StaticLayout layout = new StaticLayout(text, paint, (int) availableSpace.right, Layout.Alignment.ALIGN_NORMAL, spacingMult, spacingAdd, true);
        if (maxLines != -1 && layout.getLineCount() > maxLines)
            return false;
        return availableSpace.width() >= layout.getWidth() && availableSpace.height() >= layout.getHeight();
    }
}
 
Example 5
Source File: EditText.java    From Carbon with Apache License 2.0 5 votes vote down vote up
public boolean testSize(float suggestedSize, RectF availableSpace) {
    paint.setTextSize(suggestedSize);
    paint.setTypeface(getTypeface());
    String text = getText().toString();
    if (maxLines == 1) {
        textRect.bottom = paint.getFontSpacing();
        textRect.right = paint.measureText(text);
        return availableSpace.width() >= textRect.right && availableSpace.height() >= textRect.bottom;
    } else {
        StaticLayout layout = new StaticLayout(text, paint, (int) availableSpace.right, Layout.Alignment.ALIGN_NORMAL, spacingMult, spacingAdd, true);
        if (maxLines != -1 && layout.getLineCount() > maxLines)
            return false;
        return availableSpace.width() >= layout.getWidth() && availableSpace.height() >= layout.getHeight();
    }
}
 
Example 6
Source File: TextView.java    From Carbon with Apache License 2.0 5 votes vote down vote up
public boolean testSize(float suggestedSize, RectF availableSpace) {
    paint.setTextSize(suggestedSize);
    paint.setTypeface(getTypeface());
    String text = getText().toString();
    if (maxLines == 1) {
        textRect.bottom = paint.getFontSpacing();
        textRect.right = paint.measureText(text);
        return availableSpace.width() >= textRect.right && availableSpace.height() >= textRect.bottom;
    } else {
        StaticLayout layout = new StaticLayout(text, paint, (int) availableSpace.right, Layout.Alignment.ALIGN_NORMAL, spacingMult, spacingAdd, true);
        if (maxLines != -1 && layout.getLineCount() > maxLines)
            return false;
        return availableSpace.width() >= layout.getWidth() && availableSpace.height() >= layout.getHeight();
    }
}
 
Example 7
Source File: DanMuPainter.java    From LLApp with Apache License 2.0 5 votes vote down vote up
protected void drawTextBackground(DanMuModel danMuView, Canvas canvas, DanMuChannel danMuChannel) {
    CharSequence text = danMuView.text;
    StaticLayout staticLayout = new StaticLayout(text,
            paint,
            (int) Math.ceil(StaticLayout.getDesiredWidth(text, paint)),
            Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);

    int textBackgroundHeight = staticLayout.getHeight()
            + danMuView.textBackgroundPaddingTop
            + danMuView.textBackgroundPaddingBottom;

    float top = danMuView.getY()
            + (danMuChannel.height - textBackgroundHeight) / 2;

    float x = danMuView.getX()
            + danMuView.marginLeft
            + danMuView.avatarWidth
            - danMuView.textBackgroundMarginLeft;

    Rect rectF = new Rect((int)x,
            (int)top,
            (int)(x + danMuView.levelMarginLeft
                    + danMuView.levelBitmapWidth
                    + danMuView.textMarginLeft
                    + danMuView.textBackgroundMarginLeft
                    + staticLayout.getWidth()
                    + danMuView.textBackgroundPaddingRight),
            (int)(top + textBackgroundHeight));

    danMuView.textBackground.setBounds(rectF);
    danMuView.textBackground.draw(canvas);
}
 
Example 8
Source File: TextSelectionHelper.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected int getCharOffsetFromCord(int x, int y, int offsetX, int offsetY, ArticleSelectableView view, boolean maybe) {
    if (view == null) {
        return -1;
    }

    int line = -1;
    x -= offsetX;
    y -= offsetY;

    arrayList.clear();
    view.fillTextLayoutBlocks(arrayList);

    int childIndex;
    if (maybe) {
        childIndex = maybeTextIndex;
    } else {
        childIndex = startPeek ? startViewChildPosition : endViewChildPosition;
    }
    StaticLayout layout = arrayList.get(childIndex).getLayout();
    if (x < 0) {
        x = 1;
    }
    if (y < 0) {
        y = 1;
    }
    if (x > layout.getWidth()) {
        x = layout.getWidth();
    }
    if (y > layout.getLineBottom(layout.getLineCount() - 1)) {
        y = (int) (layout.getLineBottom(layout.getLineCount() - 1) - 1);
    }

    for (int i = 0; i < layout.getLineCount(); i++) {
        if (y > layout.getLineTop(i) && y < layout.getLineBottom(i)) {
            line = i;
            break;
        }
    }
    if (line >= 0) {
        return layout.getOffsetForHorizontal(line, x);
    }

    return -1;
}
 
Example 9
Source File: BitmapUtils.java    From EasyPhotos with Apache License 2.0 4 votes vote down vote up
/**
 * 给图片添加带文字和图片的水印,水印会根据图片宽高自动缩放处理
 *
 * @param watermark              水印图片
 * @param image                  要加水印的图片
 * @param srcWaterMarkImageWidth 水印对应的原图片宽度,即ui制作水印时候参考的图片画布宽度,应该是已知的图片最大宽度
 * @param text                   要添加的文字
 * @param offsetX                添加水印的X轴偏移量
 * @param offsetY                添加水印的Y轴偏移量
 * @param addInLeft              true 在左下角添加水印,false 在右下角添加水印
 */
public static void addWatermarkWithText(@NonNull Bitmap watermark, Bitmap image, int srcWaterMarkImageWidth, @NonNull String text, int offsetX, int offsetY, boolean addInLeft) {
    float imageWidth = image.getWidth();
    float imageHeight = image.getHeight();
    if (0 == imageWidth || 0 == imageHeight) {
        throw new RuntimeException("AlbumBuilder: 加水印的原图宽或高不能为0!");
    }
    float watermarkWidth = watermark.getWidth();
    float watermarkHeight = watermark.getHeight();
    float scale = imageWidth / (float) srcWaterMarkImageWidth;
    if (scale > 1) scale = 1;
    else if (scale < 0.4) scale = 0.4f;
    float scaleWatermarkWidth = watermarkWidth * scale;
    float scaleWatermarkHeight = watermarkHeight * scale;
    Bitmap scaleWatermark = Bitmap.createScaledBitmap(watermark, (int) scaleWatermarkWidth, (int) scaleWatermarkHeight, true);
    Canvas canvas = new Canvas(image);
    TextPaint textPaint = new TextPaint();
    textPaint.setAntiAlias(true);
    textPaint.setColor(Color.WHITE);
    float textsize = (float) (scaleWatermark.getHeight() * 2) / (float) 3;
    textPaint.setTextSize(textsize);
    StaticLayout staticLayout = new StaticLayout(text, textPaint, canvas.getWidth() / 3, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
    int textWidth = staticLayout.getWidth();
    int textHeight = staticLayout.getHeight();
    canvas.save();
    if (addInLeft) {
        canvas.translate(scaleWatermarkWidth + offsetX + scaleWatermarkWidth / 6, imageHeight - textHeight - offsetY - scaleWatermarkHeight / 6);
    } else {
        canvas.translate(imageWidth - offsetX - textWidth, imageHeight - textHeight - offsetY - scaleWatermarkHeight / 6);
    }
    staticLayout.draw(canvas);
    canvas.restore();

    Paint sacleWatermarkPaint = new Paint();
    sacleWatermarkPaint.setAntiAlias(true);
    if (addInLeft) {
        canvas.drawBitmap(scaleWatermark, offsetX, imageHeight - textHeight - offsetY - scaleWatermarkHeight / 6, sacleWatermarkPaint);
    } else {
        canvas.drawBitmap(scaleWatermark, imageWidth - textWidth - offsetX - scaleWatermarkWidth - scaleWatermarkWidth / 6, imageHeight - textHeight - offsetY - scaleWatermarkHeight / 6, sacleWatermarkPaint);
    }
    recycle(scaleWatermark);
}
 
Example 10
Source File: TextSelectionHelper.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected int getCharOffsetFromCord(int x, int y, int offsetX, int offsetY, ArticleSelectableView view, boolean maybe) {
    if (view == null) {
        return -1;
    }

    int line = -1;
    x -= offsetX;
    y -= offsetY;

    arrayList.clear();
    view.fillTextLayoutBlocks(arrayList);

    int childIndex;
    if (maybe) {
        childIndex = maybeTextIndex;
    } else {
        childIndex = startPeek ? startViewChildPosition : endViewChildPosition;
    }
    StaticLayout layout = arrayList.get(childIndex).getLayout();
    if (x < 0) {
        x = 1;
    }
    if (y < 0) {
        y = 1;
    }
    if (x > layout.getWidth()) {
        x = layout.getWidth();
    }
    if (y > layout.getLineBottom(layout.getLineCount() - 1)) {
        y = (int) (layout.getLineBottom(layout.getLineCount() - 1) - 1);
    }

    for (int i = 0; i < layout.getLineCount(); i++) {
        if (y > layout.getLineTop(i) && y < layout.getLineBottom(i)) {
            line = i;
            break;
        }
    }
    if (line >= 0) {
        return layout.getOffsetForHorizontal(line, x);
    }

    return -1;
}
 
Example 11
Source File: DanMuDispatcher.java    From LLApp with Apache License 2.0 4 votes vote down vote up
private void measure(DanMuModel danMuView, DanMuChannel danMuChannel) {
    if (danMuView.isMeasured()) {
       return;
    }

    CharSequence text = danMuView.text;
    if(!TextUtils.isEmpty(text)) {
        paint.setTextSize(danMuView.textSize);
        StaticLayout staticLayout = new StaticLayout(text,
                paint,
                (int) Math.ceil(StaticLayout.getDesiredWidth(text, paint)),
                Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);

        float textWidth = danMuView.getX()
                + danMuView.marginLeft
                + danMuView.avatarWidth
                + danMuView.levelMarginLeft
                + danMuView.levelBitmapWidth
                + danMuView.textMarginLeft
                + staticLayout.getWidth()
                + danMuView.textBackgroundPaddingRight;
        danMuView.setWidth((int) textWidth);

        float textHeight = staticLayout.getHeight()
                + danMuView.textBackgroundPaddingTop
                + danMuView.textBackgroundPaddingBottom;
        if(danMuView.avatar != null && danMuView.avatarHeight > textHeight) {
            danMuView.setHeight((int)(danMuView.getY() + danMuView.avatarHeight));
        } else {
            danMuView.setHeight((int)(danMuView.getY() + textHeight));
        }
    }

    if (danMuView.getDisplayType() == DanMuModel.RIGHT_TO_LEFT) {
        danMuView.setStartPositionX(danMuChannel.width);
    } else if (danMuView.getDisplayType() == DanMuModel.LEFT_TO_RIGHT) {
        danMuView.setStartPositionX(-danMuView.getWidth());
    }

    danMuView.setMeasured(true);
    danMuView.setStartPositionY(danMuChannel.topY);
    danMuView.setAlive(true);
}
 
Example 12
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 13
Source File: Utils.java    From NetKnight 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 14
Source File: Utils.java    From Stayfit 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 15
Source File: Utils.java    From android-kline 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,
                                     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 16
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 17
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 18
Source File: BitmapUtils.java    From imsdk-android with MIT License 4 votes vote down vote up
/**
 * 给图片添加带文字和图片的水印,水印会根据图片宽高自动缩放处理
 *
 * @param watermark              水印图片
 * @param image                  要加水印的图片
 * @param srcWaterMarkImageWidth 水印对应的原图片宽度,即ui制作水印时候参考的图片画布宽度,应该是已知的图片最大宽度
 * @param text                   要添加的文字
 * @param offsetX                添加水印的X轴偏移量
 * @param offsetY                添加水印的Y轴偏移量
 * @param addInLeft              true 在左下角添加水印,false 在右下角添加水印
 */
public static void addWatermarkWithText(@NonNull Bitmap watermark, Bitmap image, int srcWaterMarkImageWidth, @NonNull String text, int offsetX, int offsetY, boolean addInLeft) {
    float imageWidth = image.getWidth();
    float imageHeight = image.getHeight();
    if (0 == imageWidth || 0 == imageHeight) {
        throw new RuntimeException("AlbumBuilder: 加水印的原图宽或高不能为0!");
    }
    float watermarkWidth = watermark.getWidth();
    float watermarkHeight = watermark.getHeight();
    float scale = imageWidth / (float) srcWaterMarkImageWidth;
    if (scale > 1) scale = 1;
    else if (scale < 0.4) scale = 0.4f;
    float scaleWatermarkWidth = watermarkWidth * scale;
    float scaleWatermarkHeight = watermarkHeight * scale;
    Bitmap scaleWatermark = Bitmap.createScaledBitmap(watermark, (int) scaleWatermarkWidth, (int) scaleWatermarkHeight, true);
    Canvas canvas = new Canvas(image);
    TextPaint textPaint = new TextPaint();
    textPaint.setAntiAlias(true);
    textPaint.setColor(Color.WHITE);
    float textsize = (float) (scaleWatermark.getHeight() * 2) / (float) 3;
    textPaint.setTextSize(textsize);
    StaticLayout staticLayout = new StaticLayout(text, textPaint, canvas.getWidth() / 3, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
    int textWidth = staticLayout.getWidth();
    int textHeight = staticLayout.getHeight();
    canvas.save();
    if (addInLeft) {
        canvas.translate(scaleWatermarkWidth + offsetX + scaleWatermarkWidth / 6, imageHeight - textHeight - offsetY - scaleWatermarkHeight / 6);
    } else {
        canvas.translate(imageWidth - offsetX - textWidth, imageHeight - textHeight - offsetY - scaleWatermarkHeight / 6);
    }
    staticLayout.draw(canvas);
    canvas.restore();

    Paint sacleWatermarkPaint = new Paint();
    sacleWatermarkPaint.setAntiAlias(true);
    if (addInLeft) {
        canvas.drawBitmap(scaleWatermark, offsetX, imageHeight - textHeight - offsetY - scaleWatermarkHeight / 6, sacleWatermarkPaint);
    } else {
        canvas.drawBitmap(scaleWatermark, imageWidth - textWidth - offsetX - scaleWatermarkWidth - scaleWatermarkWidth / 6, imageHeight - textHeight - offsetY - scaleWatermarkHeight / 6, sacleWatermarkPaint);
    }
    recycle(scaleWatermark);
}