android.text.StaticLayout#getWidth ( )源码实例Demo

下面列出了android.text.StaticLayout#getWidth ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

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;
}
 
源代码2 项目: EasyPhotos   文件: TextSticker.java
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;

}
 
源代码3 项目: imsdk-android   文件: TextSticker.java
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;

}
 
源代码4 项目: LLApp   文件: DanMuPainter.java
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);
}
 
源代码5 项目: Carbon   文件: TextView.java
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();
    }
}
 
源代码6 项目: Carbon   文件: EditText.java
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();
    }
}
 
源代码7 项目: Carbon   文件: Button.java
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();
    }
}
 
源代码8 项目: EasyPhotos   文件: BitmapUtils.java
/**
 * 给图片添加带文字和图片的水印,水印会根据图片宽高自动缩放处理
 *
 * @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);
}
 
源代码9 项目: imsdk-android   文件: BitmapUtils.java
/**
 * 给图片添加带文字和图片的水印,水印会根据图片宽高自动缩放处理
 *
 * @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);
}
 
源代码10 项目: StockChart-MPAndroidChart   文件: Utils.java
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);
}
 
源代码11 项目: Ticket-Analysis   文件: Utils.java
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);
}
 
源代码12 项目: android-kline   文件: Utils.java
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);
}
 
源代码13 项目: Stayfit   文件: Utils.java
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);
}
 
源代码14 项目: NetKnight   文件: Utils.java
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);
}
 
源代码15 项目: JNChartDemo   文件: Utils.java
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);
}
 
源代码16 项目: LLApp   文件: DanMuDispatcher.java
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);
}
 
源代码17 项目: Telegram-FOSS   文件: TextSelectionHelper.java
@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;
}
 
源代码18 项目: Telegram   文件: TextSelectionHelper.java
@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;
}