Search in sources :

Example 1 with StaticLayout

use of android.text.StaticLayout in project philm by chrisbanes.

the class AutofitTextView method getLineCount.

private static int getLineCount(String text, TextPaint paint, float size, float width, DisplayMetrics displayMetrics) {
    paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, size, displayMetrics));
    StaticLayout layout = new StaticLayout(text, paint, (int) width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
    return layout.getLineCount();
}
Also used : StaticLayout(android.text.StaticLayout)

Example 2 with StaticLayout

use of android.text.StaticLayout in project UltimateAndroid by cymcsg.

the class AutofitTextView method getTextSize.

/**
     * Recursive binary search to find the best size for the text
     */
private static float getTextSize(CharSequence text, TextPaint paint, float targetWidth, int maxLines, float low, float high, float precision, DisplayMetrics displayMetrics) {
    float mid = (low + high) / 2.0f;
    int lineCount = 1;
    StaticLayout layout = null;
    paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, mid, displayMetrics));
    if (maxLines != 1) {
        layout = new StaticLayout(text, paint, (int) targetWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
        lineCount = layout.getLineCount();
    }
    if (SPEW)
        Log.d(TAG, "low=" + low + " high=" + high + " mid=" + mid + " target=" + targetWidth + " maxLines=" + maxLines + " lineCount=" + lineCount);
    if (lineCount > maxLines) {
        return getTextSize(text, paint, targetWidth, maxLines, low, mid, precision, displayMetrics);
    } else if (lineCount < maxLines) {
        return getTextSize(text, paint, targetWidth, maxLines, mid, high, precision, displayMetrics);
    } else {
        float maxLineWidth = 0;
        if (maxLines == 1) {
            maxLineWidth = paint.measureText(text, 0, text.length());
        } else {
            for (int i = 0; i < lineCount; i++) {
                if (layout.getLineWidth(i) > maxLineWidth) {
                    maxLineWidth = layout.getLineWidth(i);
                }
            }
        }
        if ((high - low) < precision) {
            return low;
        } else if (maxLineWidth > targetWidth) {
            return getTextSize(text, paint, targetWidth, maxLines, low, mid, precision, displayMetrics);
        } else if (maxLineWidth < targetWidth) {
            return getTextSize(text, paint, targetWidth, maxLines, mid, high, precision, displayMetrics);
        } else {
            return mid;
        }
    }
}
Also used : StaticLayout(android.text.StaticLayout) TextPaint(android.text.TextPaint)

Example 3 with StaticLayout

use of android.text.StaticLayout in project MPAndroidChart by PhilJay.

the class PieChartRenderer method drawCenterText.

/**
     * draws the description text in the center of the pie chart makes most
     * sense when center-hole is enabled
     */
protected void drawCenterText(Canvas c) {
    CharSequence centerText = mChart.getCenterText();
    if (mChart.isDrawCenterTextEnabled() && centerText != null) {
        MPPointF center = mChart.getCenterCircleBox();
        MPPointF offset = mChart.getCenterTextOffset();
        float x = center.x + offset.x;
        float y = center.y + offset.y;
        float innerRadius = mChart.isDrawHoleEnabled() && !mChart.isDrawSlicesUnderHoleEnabled() ? mChart.getRadius() * (mChart.getHoleRadius() / 100f) : mChart.getRadius();
        RectF holeRect = mRectBuffer[0];
        holeRect.left = x - innerRadius;
        holeRect.top = y - innerRadius;
        holeRect.right = x + innerRadius;
        holeRect.bottom = y + innerRadius;
        RectF boundingRect = mRectBuffer[1];
        boundingRect.set(holeRect);
        float radiusPercent = mChart.getCenterTextRadiusPercent() / 100f;
        if (radiusPercent > 0.0) {
            boundingRect.inset((boundingRect.width() - boundingRect.width() * radiusPercent) / 2.f, (boundingRect.height() - boundingRect.height() * radiusPercent) / 2.f);
        }
        if (!centerText.equals(mCenterTextLastValue) || !boundingRect.equals(mCenterTextLastBounds)) {
            // Next time we won't recalculate StaticLayout...
            mCenterTextLastBounds.set(boundingRect);
            mCenterTextLastValue = centerText;
            float width = mCenterTextLastBounds.width();
            // If width is 0, it will crash. Always have a minimum of 1
            mCenterTextLayout = new StaticLayout(centerText, 0, centerText.length(), mCenterTextPaint, (int) Math.max(Math.ceil(width), 1.f), Layout.Alignment.ALIGN_CENTER, 1.f, 0.f, false);
        }
        //float layoutWidth = Utils.getStaticLayoutMaxWidth(mCenterTextLayout);
        float layoutHeight = mCenterTextLayout.getHeight();
        c.save();
        if (Build.VERSION.SDK_INT >= 18) {
            Path path = mDrawCenterTextPathBuffer;
            path.reset();
            path.addOval(holeRect, Path.Direction.CW);
            c.clipPath(path);
        }
        c.translate(boundingRect.left, boundingRect.top + (boundingRect.height() - layoutHeight) / 2.f);
        mCenterTextLayout.draw(c);
        c.restore();
        MPPointF.recycleInstance(center);
        MPPointF.recycleInstance(offset);
    }
}
Also used : RectF(android.graphics.RectF) Path(android.graphics.Path) MPPointF(com.github.mikephil.charting.utils.MPPointF) StaticLayout(android.text.StaticLayout)

Example 4 with StaticLayout

use of android.text.StaticLayout in project LuaViewSDK by alibaba.

the class TextUtil method getAutofitTextSize.

/**
     * Recursive binary search to find the best size for the text.
     */
private static float getAutofitTextSize(CharSequence text, TextPaint paint, float targetWidth, int maxLines, float low, float high, float precision, DisplayMetrics displayMetrics) {
    float mid = (low + high) / 2.0f;
    int lineCount = 1;
    StaticLayout layout = null;
    paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, mid, displayMetrics));
    if (maxLines != 1) {
        layout = new StaticLayout(text, paint, (int) targetWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
        lineCount = layout.getLineCount();
    }
    if (SPEW)
        Log.d(TAG, "low=" + low + " high=" + high + " mid=" + mid + " target=" + targetWidth + " maxLines=" + maxLines + " lineCount=" + lineCount);
    if (lineCount > maxLines) {
        // For the case that `text` has more newline characters than `maxLines`.
        if ((high - low) < precision) {
            return low;
        }
        return getAutofitTextSize(text, paint, targetWidth, maxLines, low, mid, precision, displayMetrics);
    } else if (lineCount < maxLines) {
        return getAutofitTextSize(text, paint, targetWidth, maxLines, mid, high, precision, displayMetrics);
    } else {
        float maxLineWidth = 0;
        if (maxLines == 1) {
            maxLineWidth = paint.measureText(text, 0, text.length());
        } else {
            for (int i = 0; i < lineCount; i++) {
                if (layout.getLineWidth(i) > maxLineWidth) {
                    maxLineWidth = layout.getLineWidth(i);
                }
            }
        }
        if ((high - low) < precision) {
            return low;
        } else if (maxLineWidth > targetWidth) {
            return getAutofitTextSize(text, paint, targetWidth, maxLines, low, mid, precision, displayMetrics);
        } else if (maxLineWidth < targetWidth) {
            return getAutofitTextSize(text, paint, targetWidth, maxLines, mid, high, precision, displayMetrics);
        } else {
            return mid;
        }
    }
}
Also used : StaticLayout(android.text.StaticLayout) TextPaint(android.text.TextPaint)

Example 5 with StaticLayout

use of android.text.StaticLayout in project LuaViewSDK by alibaba.

the class AutofitHelper method getLineCount.

private static int getLineCount(CharSequence text, TextPaint paint, float size, float width, DisplayMetrics displayMetrics) {
    paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, size, displayMetrics));
    StaticLayout layout = new StaticLayout(text, paint, (int) width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
    return layout.getLineCount();
}
Also used : StaticLayout(android.text.StaticLayout)

Aggregations

StaticLayout (android.text.StaticLayout)80 TextPaint (android.text.TextPaint)56 Paint (android.graphics.Paint)45 Layout (android.text.Layout)21 DynamicLayout (android.text.DynamicLayout)9 RectF (android.graphics.RectF)6 Bitmap (android.graphics.Bitmap)3 NonNull (android.support.annotation.NonNull)3 BoringLayout (android.text.BoringLayout)3 SpannableStringBuilder (android.text.SpannableStringBuilder)3 KeyEvent (android.view.KeyEvent)3 MotionEvent (android.view.MotionEvent)3 AccessibilityEvent (android.view.accessibility.AccessibilityEvent)3 SuppressLint (android.annotation.SuppressLint)2 Canvas (android.graphics.Canvas)2 Rect (android.graphics.Rect)2 Spanned (android.text.Spanned)2 TruncateAt (android.text.TextUtils.TruncateAt)2 StyleSpan (android.text.style.StyleSpan)2 Animator (android.animation.Animator)1