Java Code Examples for android.text.TextPaint.getTextBounds()

The following are Jave code examples for showing how to use getTextBounds() of the android.text.TextPaint class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: Protein   File: BadgedFourThreeImageView.java   Source Code and License Vote up 5 votes
GifBadge(Context context) {
    if (bitmap == null) {
        final DisplayMetrics dm = context.getResources().getDisplayMetrics();
        final float density = dm.density;
        final float scaledDensity = dm.scaledDensity;
        final TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint
                .SUBPIXEL_TEXT_FLAG);
        textPaint.setTypeface(Typeface.create(TYPEFACE, TYPEFACE_STYLE));
        textPaint.setTextSize(TEXT_SIZE * scaledDensity);

        final float padding = PADDING * density;
        final float cornerRadius = CORNER_RADIUS * density;
        final Rect textBounds = new Rect();
        textPaint.getTextBounds(GIF, 0, GIF.length(), textBounds);
        height = (int) (padding + textBounds.height() + padding);
        width = (int) (padding + textBounds.width() + padding);
        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setHasAlpha(true);
        final Canvas canvas = new Canvas(bitmap);
        final Paint backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        backgroundPaint.setColor(BACKGROUND_COLOR);
        canvas.drawRoundRect(0, 0, width, height, cornerRadius, cornerRadius,
                backgroundPaint);
        // punch out the word 'GIF', leaving transparency
        textPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        canvas.drawText(GIF, padding, height - padding, textPaint);
    }
    paint = new Paint();
}
 
Example 2
Project: Hotspot-master-devp   File: StringUtils.java   Source Code and License Vote up 5 votes
public static int getStringWidth(TextView tv, String text) {
    Rect bounds = new Rect();
    TextPaint paint;

    paint = tv.getPaint();
    paint.getTextBounds(text, 0, text.length(), bounds);
    int width = bounds.width();

    return width;
}
 
Example 3
Project: cute-currency-view   File: CharView.java   Source Code and License Vote up 5 votes
public CharView(Context context, TextPaint originalPaint, char letter) {
    super(context);
    this.letter = letter;
    this.originalPaint = originalPaint;
    layout = new StaticLayout(String.valueOf(letter),
            0, 1, originalPaint, 0, Layout.Alignment.ALIGN_NORMAL, 0f, 0f, false);
    originalPaint.getTextBounds(String.valueOf(letter), 0, 1, textBounds);
}
 
Example 4
Project: UsoppBubble   File: UsoppFrameLayout.java   Source Code and License Vote up 5 votes
private void drawBubble(Canvas canvas) {
    float left = 0;
    float top = 0;
    float right = mDataBean.mClickViewRect.width();
    float bottom = mDataBean.mClickViewRect.height();
    float midHeight = bottom / 2.0f;
    float midWidth = right / 2.0f;

    mDataBean.mPaint.setMaskFilter(null);
    mDataBean.mPaint.setColor(mDataBean.mClickView.getDragBackgroundColor());
    float offset = 0;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        offset = mDataBean.mClickViewRect.width() - ((float) mDataBean.mClickViewRect.width()) / 1.845f;
        canvas.drawArc(left + offset, top, right, bottom, -90, 180, true, mDataBean.mPaint);
        canvas.drawArc(left, top, right - offset, bottom, 90, 180, true, mDataBean.mPaint);
    } else {
        offset = mDataBean.mClickViewRect.width() - ((float) mDataBean.mClickViewRect.width()) / 1.35f;
        canvas.drawCircle(left + offset, midHeight, midHeight, mDataBean.mPaint);
        canvas.drawCircle(right - offset, midHeight, midHeight, mDataBean.mPaint);
    }
    float halfDrawWidth = (right - (left + offset)) / 2.0f;
    canvas.drawRect(halfDrawWidth, 0, right - halfDrawWidth, bottom, mDataBean.mPaint);
    TextPaint paint = mDataBean.mClickView.getPaint();
    String text = (String) mDataBean.mClickView.getText();
    float halfMeasureTextWidth = paint.measureText(text, 0, text.length()) / 2.0f;
    paint.getTextBounds(text, 0, text.length(), mTextBound);
    canvas.drawText(text, 0, text.length(), midWidth - halfMeasureTextWidth, midHeight + mTextBound.height() / 2.0f, paint);
}
 
Example 5
Project: OSchina_resources_android   File: PortraitView.java   Source Code and License Vote up 4 votes
@SuppressWarnings("ResourceAsColor")
private Bitmap buildSrcFromName(final String firstChar, int w, int h) {
    if (w == Target.SIZE_ORIGINAL || w <= 0)
        w = 80;
    if (h == Target.SIZE_ORIGINAL || h <= 0)
        h = 80;

    final int size = Math.max(Math.min(Math.min(w, h), 220), 64);
    final float fontSize = size * 0.4f;
    log("firstChar:" + firstChar + " size:" + size + " fontSize:" + fontSize);

    Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.RGB_565);

    TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    paint.setAntiAlias(true);
    paint.setDither(true);
    paint.setColor(Color.WHITE);
    paint.setTextAlign(Paint.Align.CENTER);
    paint.setTextSize(fontSize);
    paint.setTypeface(Typeface.SANS_SERIF);

    // check ASCII
    final int charNum = Character.getNumericValue(firstChar.charAt(0));
    if (charNum > 0 && charNum < 177) {
        Typeface typeface = getFont(getContext(), "Numans-Regular.otf");
        if (typeface != null)
            paint.setTypeface(typeface);
    }

    Rect rect = new Rect();
    paint.getTextBounds(firstChar, 0, 1, rect);
    int fontHeight = rect.height();
    log(rect.toString());

    int fontHalfH = fontHeight >> 1;
    int centerX = bitmap.getWidth() >> 1;
    int centerY = bitmap.getHeight() >> 1;

    Canvas canvas = new Canvas(bitmap);
    canvas.drawColor(getBackgroundColor(firstChar));
    canvas.drawText(firstChar, centerX, centerY + fontHalfH, paint);

    return bitmap;
}