Search in sources :

Example 31 with TextPaint

use of android.text.TextPaint in project actor-platform by actorapp.

the class UserSpan method draw.

@Override
public void draw(Canvas canvas, CharSequence charSequence, int start, int end, float x, int top, int y, int bottom, Paint paint) {
    int size = (int) textPaint.measureText(userText);
    Paint debug = new Paint();
    debug.setColor(0xffebebeb);
    debug.setStyle(Paint.Style.FILL_AND_STROKE);
    canvas.drawRoundRect(new RectF(x + Screen.dp(4), y - Screen.dp(20), x + size + Screen.dp(4 + 24), y + Screen.dp(8)), Screen.dp(14), Screen.dp(14), debug);
    canvas.drawText(userText, x + Screen.dp(4 + 12), y, textPaint);
}
Also used : RectF(android.graphics.RectF) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 32 with TextPaint

use of android.text.TextPaint in project CoCoin by Nightonke.

the class PaperButton method onDraw.

@Override
protected void onDraw(@NonNull Canvas canvas) {
    super.onDraw(canvas);
    int radius = 0;
    int shadowColor = changeColorAlpha(mShadowColor, MIN_SHADOW_COLOR_ALPHA);
    long elapsed = System.currentTimeMillis() - mStartTime;
    switch(mState) {
        case StateNormal:
            shadowColor = changeColorAlpha(mShadowColor, MIN_SHADOW_COLOR_ALPHA);
            break;
        case StateTouchDown:
            ripplePaint.setAlpha(255);
            if (elapsed < ANIMATION_DURATION) {
                radius = Math.round(elapsed * getWidth() / 2 / ANIMATION_DURATION);
                float shadowAlpha = (MAX_SHADOW_COLOR_ALPHA - MIN_SHADOW_COLOR_ALPHA) * elapsed / ANIMATION_DURATION + MIN_SHADOW_COLOR_ALPHA;
                shadowColor = changeColorAlpha(mShadowColor, shadowAlpha);
            } else {
                radius = getWidth() / 2;
                shadowColor = changeColorAlpha(mShadowColor, MAX_SHADOW_COLOR_ALPHA);
            }
            postInvalidate();
            break;
        case StateTouchUp:
            if (elapsed < ANIMATION_DURATION) {
                int alpha = Math.round((ANIMATION_DURATION - elapsed) * 255 / ANIMATION_DURATION);
                ripplePaint.setAlpha(alpha);
                radius = getWidth() / 2 + Math.round(elapsed * getWidth() / 2 / ANIMATION_DURATION);
                float shadowAlpha = (MAX_SHADOW_COLOR_ALPHA - MIN_SHADOW_COLOR_ALPHA) * (ANIMATION_DURATION - elapsed) / ANIMATION_DURATION + MIN_SHADOW_COLOR_ALPHA;
                shadowColor = changeColorAlpha(mShadowColor, shadowAlpha);
            } else {
                mState = StateNormal;
                radius = 0;
                ripplePaint.setAlpha(0);
                shadowColor = changeColorAlpha(mShadowColor, MIN_SHADOW_COLOR_ALPHA);
            }
            postInvalidate();
            break;
    }
    backgroundPaint.setShadowLayer(mShadowRadius, mShadowOffsetX, mShadowOffsetY, shadowColor);
    canvas.drawRoundRect(getRectF(), mCornerRadius, mCornerRadius, backgroundPaint);
    canvas.save();
    if (mState == StateTouchDown || mState == StateTouchUp) {
        if (rippleClipPath == null) {
            rippleClipPath = new Path();
            rippleClipPath.addRoundRect(getRectF(), mCornerRadius, mCornerRadius, Path.Direction.CW);
        }
        canvas.clipPath(rippleClipPath);
    }
    canvas.drawCircle(mTouchPoint.x, mTouchPoint.y, radius, ripplePaint);
    canvas.restore();
    if (mText != null && mText.length() > 0) {
        int y = (int) (getHeight() / 2 - ((textPaint.descent() + textPaint.ascent()) / 2));
        canvas.drawText(mText.toString(), getWidth() / 2, y, textPaint);
    }
}
Also used : TextPaint(android.text.TextPaint)

Example 33 with TextPaint

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

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

the class TextUtil method adjustTextSize.

/**
     * Re-sizes the textSize of the TextView so that the text fits within the bounds of the View.
     */
private static void adjustTextSize(TextView view, TextPaint paint, float minTextSize, float maxTextSize, int maxLines, float precision) {
    if (maxLines <= 0 || maxLines == Integer.MAX_VALUE) {
        // Don't auto-size since there's no limit on lines.
        return;
    }
    int targetWidth = view.getWidth() - view.getPaddingLeft() - view.getPaddingRight();
    if (targetWidth <= 0) {
        return;
    }
    CharSequence text = view.getText();
    TransformationMethod method = view.getTransformationMethod();
    if (method != null) {
        text = method.getTransformation(text, view);
    }
    Context context = view.getContext();
    Resources r = Resources.getSystem();
    DisplayMetrics displayMetrics;
    float size = maxTextSize;
    float high = size;
    float low = 0;
    if (context != null) {
        r = context.getResources();
    }
    displayMetrics = r.getDisplayMetrics();
    paint.set(view.getPaint());
    paint.setTextSize(size);
    if ((maxLines == 1 && paint.measureText(text, 0, text.length()) > targetWidth) || getLineCount(text, paint, size, targetWidth, displayMetrics) > maxLines) {
        size = getAutofitTextSize(text, paint, targetWidth, maxLines, low, high, precision, displayMetrics);
    }
    if (size < minTextSize) {
        size = minTextSize;
    }
    view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
}
Also used : Context(android.content.Context) TransformationMethod(android.text.method.TransformationMethod) SingleLineTransformationMethod(android.text.method.SingleLineTransformationMethod) Resources(android.content.res.Resources) DisplayMetrics(android.util.DisplayMetrics) TextPaint(android.text.TextPaint)

Example 35 with TextPaint

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

the class AutofitHelper 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)

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