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

The following examples show how to use android.text.TextPaint#setLetterSpacing() . 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: AutoExpandTextView.java    From Trebuchet with GNU General Public License v3.0 6 votes vote down vote up
private static float getLetterSpacing(CharSequence text, TextPaint paint, float targetWidth,
                                      float low, float high, float precision) {
    float mid = (low + high) / 2.0f;
    paint.setLetterSpacing(mid);

    float measuredWidth = paint.measureText(text, 0, text.length());

    if (high - low < precision) {
        if (measuredWidth < targetWidth) {
            return mid;
        } else {
            return low;
        }
    } else if (measuredWidth > targetWidth) {
        return getLetterSpacing(text, paint, targetWidth, low, mid, precision);
    } else if (measuredWidth < targetWidth) {
        return getLetterSpacing(text, paint, targetWidth, mid, high, precision);
    } else {
        return mid;
    }
}
 
Example 2
Source File: CustomLetterSpacingSpan.java    From react-native-GPay with MIT License 5 votes vote down vote up
private void apply(TextPaint paint) {
  // mLetterSpacing and paint.getTextSize() are both in pixels,
  // yielding an accurate em value.
  if (!Float.isNaN(mLetterSpacing)) {
    paint.setLetterSpacing(mLetterSpacing / paint.getTextSize());
  }
}
 
Example 3
Source File: ReflowText.java    From android-proguards with Apache License 2.0 5 votes vote down vote up
private Layout createLayout(ReflowData data, Context context, boolean enforceMaxLines) {
    TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextSize(data.textSize);
    paint.setColor(data.textColor);
    paint.setLetterSpacing(data.letterSpacing);
    if (data.fontName != null) {
        paint.setTypeface(FontUtil.get(context, data.fontName));
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        StaticLayout.Builder builder =  StaticLayout.Builder.obtain(
                data.text, 0, data.text.length(), paint, data.textWidth)
                .setLineSpacing(data.lineSpacingAdd, data.lineSpacingMult)
                .setBreakStrategy(data.breakStrategy);
        if (enforceMaxLines && data.maxLines != -1) {
            builder.setMaxLines(data.maxLines);
            builder.setEllipsize(TextUtils.TruncateAt.END);
        }
        return builder.build();
    } else {
        return new StaticLayout(
                data.text,
                paint,
                data.textWidth,
                Layout.Alignment.ALIGN_NORMAL,
                data.lineSpacingMult,
                data.lineSpacingAdd,
                true);
    }
}
 
Example 4
Source File: RNTextSizeSpannedText.java    From react-native-text-size with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void apply(TextPaint paint) {
    paint.setLetterSpacing(mLetterSpacing / paint.getTextSize());
}
 
Example 5
Source File: PageLoader.java    From MyBookshelf with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 初始化画笔
 */
private void initPaint() {
    Typeface typeface;
    try {
        if (!TextUtils.isEmpty(readBookControl.getFontPath())) {
            typeface = Typeface.createFromFile(readBookControl.getFontPath());
        } else {
            typeface = Typeface.SANS_SERIF;
        }
    } catch (Exception e) {
        Toast.makeText(mContext, "字体文件未找,到恢复默认字体", Toast.LENGTH_SHORT).show();
        readBookControl.setReadBookFont(null);
        typeface = Typeface.SANS_SERIF;
    }
    // 绘制提示的画笔
    mTipPaint = new TextPaint();
    mTipPaint.setColor(readBookControl.getTextColor());
    mTipPaint.setTextAlign(Paint.Align.LEFT); // 绘制的起始点
    mTipPaint.setTextSize(ScreenUtils.spToPx(DEFAULT_TIP_SIZE)); // Tip默认的字体大小
    mTipPaint.setTypeface(Typeface.create(typeface, Typeface.NORMAL));
    mTipPaint.setAntiAlias(true);
    mTipPaint.setSubpixelText(true);

    // 绘制标题的画笔
    mTitlePaint = new TextPaint();
    mTitlePaint.setColor(readBookControl.getTextColor());
    mTitlePaint.setTextSize(mTitleSize);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mTitlePaint.setLetterSpacing(readBookControl.getTextLetterSpacing());
    }
    mTitlePaint.setStyle(Paint.Style.FILL_AND_STROKE);
    mTitlePaint.setTypeface(Typeface.create(typeface, Typeface.BOLD));
    mTitlePaint.setTextAlign(Paint.Align.CENTER);
    mTitlePaint.setAntiAlias(true);

    // 绘制页面内容的画笔
    mTextPaint = new TextPaint();
    mTextPaint.setColor(readBookControl.getTextColor());
    mTextPaint.setTextSize(mTextSize);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mTextPaint.setLetterSpacing(readBookControl.getTextLetterSpacing());
    }
    int bold = readBookControl.getTextBold() ? Typeface.BOLD : Typeface.NORMAL;
    mTextPaint.setTypeface(Typeface.create(typeface, bold));
    mTextPaint.setAntiAlias(true);

    // 绘制结束的画笔
    mTextEndPaint = new TextPaint();
    mTextEndPaint.setColor(readBookControl.getTextColor());
    mTextEndPaint.setTextSize(mTextEndSize);
    mTextEndPaint.setTypeface(Typeface.create(typeface, Typeface.NORMAL));
    mTextEndPaint.setAntiAlias(true);
    mTextEndPaint.setSubpixelText(true);
    mTextEndPaint.setTextAlign(Paint.Align.CENTER);

    // 绘制电池的画笔
    mBatteryPaint = new TextPaint();
    mBatteryPaint.setAntiAlias(true);
    mBatteryPaint.setDither(true);
    mBatteryPaint.setTextSize(ScreenUtils.spToPx(DEFAULT_TIP_SIZE - 3));
    mBatteryPaint.setTypeface(Typeface.createFromAsset(mContext.getAssets(), "number.ttf"));

    setupTextInterval();
    // 初始化页面样式
    initPageStyle();
}