Search in sources :

Example 81 with TextPaint

use of android.text.TextPaint 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 82 with TextPaint

use of android.text.TextPaint in project android_frameworks_base by ParanoidAndroid.

the class TextAppearanceSpan method updateMeasureState.

@Override
public void updateMeasureState(TextPaint ds) {
    if (mTypeface != null || mStyle != 0) {
        Typeface tf = ds.getTypeface();
        int style = 0;
        if (tf != null) {
            style = tf.getStyle();
        }
        style |= mStyle;
        if (mTypeface != null) {
            tf = Typeface.create(mTypeface, style);
        } else if (tf == null) {
            tf = Typeface.defaultFromStyle(style);
        } else {
            tf = Typeface.create(tf, style);
        }
        int fake = style & ~tf.getStyle();
        if ((fake & Typeface.BOLD) != 0) {
            ds.setFakeBoldText(true);
        }
        if ((fake & Typeface.ITALIC) != 0) {
            ds.setTextSkewX(-0.25f);
        }
        ds.setTypeface(tf);
    }
    if (mTextSize > 0) {
        ds.setTextSize(mTextSize);
    }
}
Also used : Typeface(android.graphics.Typeface) TextPaint(android.text.TextPaint)

Example 83 with TextPaint

use of android.text.TextPaint in project android_frameworks_base by AOSPA.

the class StaticLayoutTest method moveCursorToRightCursorableOffset.

private void moveCursorToRightCursorableOffset(EditorState state, TextPaint paint) {
    assertEquals("The editor has selection", state.mSelectionStart, state.mSelectionEnd);
    final Layout layout = builder().setText(state.mText.toString()).setPaint(paint).build();
    final int newOffset = layout.getOffsetToRightOf(state.mSelectionStart);
    state.mSelectionStart = state.mSelectionEnd = newOffset;
}
Also used : TextPaint(android.text.TextPaint)

Example 84 with TextPaint

use of android.text.TextPaint in project android_frameworks_base by AOSPA.

the class StaticLayoutTest method moveCursorToLeftCursorableOffset.

private void moveCursorToLeftCursorableOffset(EditorState state, TextPaint paint) {
    assertEquals("The editor has selection", state.mSelectionStart, state.mSelectionEnd);
    final Layout layout = builder().setText(state.mText.toString()).setPaint(paint).build();
    final int newOffset = layout.getOffsetToLeftOf(state.mSelectionStart);
    state.mSelectionStart = state.mSelectionEnd = newOffset;
}
Also used : TextPaint(android.text.TextPaint)

Example 85 with TextPaint

use of android.text.TextPaint in project ABPlayer by winkstu.

the class AndroidDisplayer method measure.

@Override
public void measure(BaseDanmaku danmaku) {
    TextPaint paint = getPaint(danmaku);
    if (HAS_STROKE) {
        applyPaintConfig(danmaku, paint, true);
    }
    calcPaintWH(danmaku, paint);
    if (HAS_STROKE) {
        applyPaintConfig(danmaku, paint, false);
    }
}
Also used : TextPaint(android.text.TextPaint)

Aggregations

TextPaint (android.text.TextPaint)216 Paint (android.graphics.Paint)86 StaticLayout (android.text.StaticLayout)29 View (android.view.View)17 Bitmap (android.graphics.Bitmap)14 Typeface (android.graphics.Typeface)14 TypedArray (android.content.res.TypedArray)12 SpannableString (android.text.SpannableString)12 RectF (android.graphics.RectF)11 Spanned (android.text.Spanned)11 TextView (android.widget.TextView)11 Rect (android.graphics.Rect)8 StyleSpan (android.text.style.StyleSpan)8 ClickableSpan (android.text.style.ClickableSpan)7 PackageManager (android.content.pm.PackageManager)6 Canvas (android.graphics.Canvas)6 IBinder (android.os.IBinder)6 SpannableStringBuilder (android.text.SpannableStringBuilder)6 Context (android.content.Context)5 Resources (android.content.res.Resources)5