Java Code Examples for android.text.TextPaint#set()

The following examples show how to use android.text.TextPaint#set() . 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: AndroidDisplayer.java    From DanDanPlayForAndroid with MIT License 6 votes vote down vote up
public TextPaint getPaint(BaseDanmaku danmaku, boolean fromWorkerThread) {
    TextPaint paint;
    if (fromWorkerThread) {
        paint = PAINT;
    } else {
        paint = PAINT_DUPLICATE;
        paint.set(PAINT);
    }
    paint.setTextSize(danmaku.textSize);
    applyTextScaleConfig(danmaku, paint);

    //ignore the transparent textShadowColor
    if (!HAS_SHADOW || SHADOW_RADIUS <= 0 || danmaku.textShadowColor == 0) {
        paint.clearShadowLayer();
    } else {
        paint.setShadowLayer(SHADOW_RADIUS, 0, 0, danmaku.textShadowColor);
    }
    paint.setAntiAlias(ANTI_ALIAS);
    return paint;
}
 
Example 2
Source File: SpinningWheelView.java    From SpinningWheelAndroid with Apache License 2.0 6 votes vote down vote up
private void drawWheelItems(Canvas canvas) {
    float cx = circle.getCx();
    float cy = circle.getCy();
    float radius = circle.getRadius();
    float x = cx - radius + (wheelStrokeRadius * 5);
    float y = cy;
    float textWidth = radius - (wheelStrokeRadius * 10);
    TextPaint textPaint = new TextPaint();
    textPaint.set(this.textPaint);

    float angle = getAnglePerItem() / 2;

    for (int i = 0; i < getItemSize(); i++) {
        CharSequence item = TextUtils
                .ellipsize(items.get(i).toString(), textPaint, textWidth, TextUtils.TruncateAt.END);
        canvas.save();
        canvas.rotate(angle + 180, cx, cy); // +180 for start from right
        canvas.drawText(item.toString(), x, y, this.textPaint);
        canvas.restore();

        angle += getAnglePerItem();
    }
}
 
Example 3
Source File: AndroidDisplayer.java    From letv with Apache License 2.0 6 votes vote down vote up
private synchronized TextPaint getPaint(BaseDanmaku danmaku, boolean quick) {
    TextPaint paint;
    if (quick) {
        paint = this.PAINT_DUPLICATE;
        paint.set(this.PAINT);
    } else {
        paint = this.PAINT;
    }
    paint.reset();
    paint.setTextSize(danmaku.textSize);
    applyTextScaleConfig(danmaku, paint);
    if (!this.HAS_SHADOW || this.SHADOW_RADIUS <= 0.0f || danmaku.textShadowColor == 0) {
        paint.clearShadowLayer();
    } else {
        paint.setShadowLayer(this.SHADOW_RADIUS, 0.0f, 0.0f, danmaku.textShadowColor);
    }
    paint.setAntiAlias(this.ANTI_ALIAS);
    return paint;
}
 
Example 4
Source File: Styled.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
/**
     * Returns the advance widths for a uniform left-to-right run of text with
     * no style changes in the middle of the run. If any style is replacement
     * text, the first character will get the width of the replacement and the
     * remaining characters will get a width of 0.
     * 
     * @param paint the paint, will not be modified
     * @param workPaint a paint to modify; on return will reflect the original
     *        paint plus the effect of all spans on the run
     * @param text the text
     * @param start the start of the run
     * @param end the limit of the run
     * @param widths array to receive the advance widths of the characters. Must
     *        be at least a large as (end - start).
     * @param fmi FontMetrics information; can be null
     * @return the actual number of widths returned
     */
    public static int getTextWidths(TextPaint paint,
                                    TextPaint workPaint,
                                    Spanned text, int start, int end,
                                    float[] widths, Paint.FontMetricsInt fmi) {
// Jota Text Editor
//        MetricAffectingSpan[] spans =
//            text.getSpans(start, end, MetricAffectingSpan.class);
//
//		ReplacementSpan replacement = null;
        workPaint.set(paint);
//
//		for (int i = 0; i < spans.length; i++) {
//			MetricAffectingSpan span = spans[i];
//			if (span instanceof ReplacementSpan) {
//				replacement = (ReplacementSpan)span;
//			}
//			else {
//				span.updateMeasureState(workPaint);
//			}
//		}
//
//        if (replacement == null) {
            workPaint.getFontMetricsInt(fmi);
            workPaint.getTextWidths(text, start, end, widths);
//        } else {
//            int wid = replacement.getSize(workPaint, text, start, end, fmi);
//
//            if (end > start) {
//                widths[0] = wid;
//                for (int i = start + 1; i < end; i++)
//                    widths[i - start] = 0;
//            }
//        }
        return end - start;
    }
 
Example 5
Source File: TextLayoutBuilder.java    From TextLayoutBuilder with Apache License 2.0 5 votes vote down vote up
/** Create a new paint after the builder builds for the first time. */
void createNewPaintIfNeeded() {
  // Once after build() is called, it is not safe to set properties
  // on the paint as we cache the text layouts.
  // Hence we create a new paint object,
  // if we ever change one of the paint's properties.
  if (mForceNewPaint) {
    TextPaint newPaint = new TextPaint(paint);
    newPaint.set(paint);
    paint = newPaint;
    mForceNewPaint = false;
  }
}
 
Example 6
Source File: Styled.java    From JotaTextEditor with Apache License 2.0 5 votes vote down vote up
/**
     * Returns the advance widths for a uniform left-to-right run of text with
     * no style changes in the middle of the run. If any style is replacement
     * text, the first character will get the width of the replacement and the
     * remaining characters will get a width of 0.
     * 
     * @param paint the paint, will not be modified
     * @param workPaint a paint to modify; on return will reflect the original
     *        paint plus the effect of all spans on the run
     * @param text the text
     * @param start the start of the run
     * @param end the limit of the run
     * @param widths array to receive the advance widths of the characters. Must
     *        be at least a large as (end - start).
     * @param fmi FontMetrics information; can be null
     * @return the actual number of widths returned
     */
    public static int getTextWidths(TextPaint paint,
                                    TextPaint workPaint,
                                    Spanned text, int start, int end,
                                    float[] widths, Paint.FontMetricsInt fmi) {
// Jota Text Editor
//        MetricAffectingSpan[] spans =
//            text.getSpans(start, end, MetricAffectingSpan.class);
//
//		ReplacementSpan replacement = null;
        workPaint.set(paint);
//
//		for (int i = 0; i < spans.length; i++) {
//			MetricAffectingSpan span = spans[i];
//			if (span instanceof ReplacementSpan) {
//				replacement = (ReplacementSpan)span;
//			}
//			else {
//				span.updateMeasureState(workPaint);
//			}
//		}
//
//        if (replacement == null) {
            workPaint.getFontMetricsInt(fmi);
            workPaint.getTextWidths(text, start, end, widths);
//        } else {
//            int wid = replacement.getSize(workPaint, text, start, end, fmi);
//
//            if (end > start) {
//                widths[0] = wid;
//                for (int i = start + 1; i < end; i++)
//                    widths[i - start] = 0;
//            }
//        }
        return end - start;
    }
 
Example 7
Source File: AutofitHelper.java    From kAndroid with Apache License 2.0 4 votes vote down vote up
/**
 * Re-sizes the textSize of the TextView so that the text fits within the bounds of the View.
 */
private static void autofit(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);
}
 
Example 8
Source File: TextUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Re-sizes the textSize of the TextView so that the text fits within the bounds of the View.
 */
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);
}
 
Example 9
Source File: AutofitHelper.java    From stynico with MIT License 4 votes vote down vote up
/**
    * Re-sizes the textSize of the TextView so that the text fits within the bounds of the View.
    */
   private static void autofit(AppCompatTextView 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);
   }
 
Example 10
Source File: AutofitHelper.java    From DarkCalculator with MIT License 4 votes vote down vote up
/**
 * Re-sizes the textSize of the TextView so that the text fits within the bounds of the View.
 */
private static void autofit(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);
}
 
Example 11
Source File: SystemText.java    From remixed-dungeon with GNU General Public License v3.0 4 votes vote down vote up
public SystemText(final String text, float size, boolean multiline) {
	super(0, 0, 0, 0);

	if (fontScale != fontScale) {
		updateFontScale();
	}

	if (tf == null) {
		if (Game.smallResScreen()) {
			tf = Typeface.create((String) null, Typeface.BOLD);
			oversample = 1;
		} else {
			tf = Typeface.create((String) null, Typeface.NORMAL);
			oversample = 4;
		}
	}

	size *= fontScale;

	needWidth = multiline;

	if (size == 0) {
		throw new TrackedRuntimeException("zero sized font!!!");
	}

	float textSize = size * oversample;
	if (!textPaints.containsKey(textSize)) {
		TextPaint tx = new TextPaint();

		tx.setTextSize(textSize);
		tx.setStyle(Paint.Style.FILL_AND_STROKE);
		tx.setHinting(Paint.HINTING_ON);
		tx.setAntiAlias(true);

		tx.setColor(Color.WHITE);

		tx.setTypeface(tf);

		TextPaint cp = new TextPaint();
		cp.set(tx);
		cp.setStyle(Paint.Style.FILL_AND_STROKE);
		cp.setStrokeWidth(textSize * 0.2f);
		cp.setColor(Color.BLACK);
		cp.setAntiAlias(true);

		textPaints.put(textSize, tx);
		contourPaints.put(textSize, cp);
	}

	textPaint = textPaints.get(textSize);
	contourPaint = contourPaints.get(textSize);

	this.text(text);
	texts.add(this);
}