Example usage for android.text TextPaint getTextBounds

List of usage examples for android.text TextPaint getTextBounds

Introduction

In this page you can find the example usage for android.text TextPaint getTextBounds.

Prototype

public void getTextBounds(String text, int start, int end, Rect bounds) 

Source Link

Document

Return in bounds (allocated by the caller) the smallest rectangle that encloses all of the characters, with an implied origin at (0,0).

Usage

From source file:Main.java

public static int getTextViewMeasureWidth(TextView textView, String text) {
    if (textView == null)
        return 0;
    TextPaint paint = textView.getPaint();
    Rect rect = new Rect();
    paint.getTextBounds(text, 0, text.length(), rect);
    return Math.abs(rect.right - rect.left);
}

From source file:enterprayz.megatools.Tools.java

public static int getTextHeight(String text, int maxWidth, float textSize, Typeface typeface) {
    TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
    paint.setTextSize(textSize);/* ww w . j av  a 2  s  . c o  m*/
    paint.setTypeface(typeface);

    int lineCount = 0;

    int index = 0;
    int length = text.length();

    while (index < length - 1) {
        index += paint.breakText(text, index, length, true, maxWidth, null);
        lineCount++;
    }

    Rect bounds = new Rect();
    paint.getTextBounds("Py", 0, 2, bounds);
    return (int) Math.floor(lineCount * bounds.height());
}

From source file:com.ecuamobi.deckwallet.util.Renderer.java

static void printWallet(final Activity context, final String label, final String addressUri,
        final String privateKey) {
    new AsyncTask<Void, Void, Bitmap>() {

        @Override// w w w  .j a va2  s.c o  m
        protected Bitmap doInBackground(Void... params) {

            TextPaint textPaint = new TextPaint();
            textPaint.setAntiAlias(true);
            textPaint.setColor(0xFF000000);
            final int bitmapMargin = 100;//big margin is to prevent possible clipping
            final int textHeight = 28;
            final int spaceBetweenQrCodes = 60;
            textPaint.setTextSize(textHeight);
            textPaint.setTextAlign(Paint.Align.CENTER);
            final int qrCodePadding = (int) (textPaint.descent() * 2);
            Rect bounds = new Rect();
            textPaint.getTextBounds(privateKey, 0, privateKey.length(), bounds);
            int textWidth = getTextWidth(privateKey, textPaint);
            ArrayList<String> labelLinesRelaxed = wrap(label, textWidth, false, textPaint);
            for (String titleLine : labelLinesRelaxed) {
                textWidth = Math.max(textWidth, getTextWidth(titleLine, textPaint));
            }
            textWidth = Math.max(textWidth, getTextWidth(addressUri, textPaint));
            QRCode privateKeyQrCode = QRCode.getMinimumQRCode(privateKey, ErrorCorrectLevel.M);
            Bitmap privateKeyQrCodeBitmap = privateKeyQrCode.createImage(textWidth);
            QRCode addressQrCode = QRCode.getMinimumQRCode(addressUri, ErrorCorrectLevel.M);
            Bitmap addressQrCodeBitmap = addressQrCode.createImage(textWidth);
            ArrayList<String> labelLines = wrap(label, textWidth, true, textPaint);
            Bitmap bmp = Bitmap.createBitmap(
                    textWidth * 2 + bitmapMargin * 2 + spaceBetweenQrCodes, privateKeyQrCodeBitmap.getHeight()
                            + textHeight * (labelLines.size() + 1) + qrCodePadding * 2 + bitmapMargin * 2,
                    Bitmap.Config.RGB_565);
            Canvas canvas = new Canvas(bmp);
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);
            paint.setARGB(0xFF, 0xFF, 0xFF, 0xFF);
            paint.setAntiAlias(false);
            canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);

            int centerXForAddress = bitmapMargin + textWidth / 2;
            int centerXForPrivateKey = bitmapMargin + textWidth + spaceBetweenQrCodes + textWidth / 2;
            int y = (int) (bitmapMargin - textPaint.ascent());
            for (int i = 0; i < labelLines.size(); i++) {
                canvas.drawText(labelLines.get(i), centerXForPrivateKey, y + i * textHeight, textPaint);
            }
            y = bitmapMargin + labelLines.size() * textHeight + qrCodePadding;
            Paint qrCodePaint = new Paint();
            qrCodePaint.setAntiAlias(false);
            qrCodePaint.setDither(false);
            canvas.drawBitmap(addressQrCodeBitmap, centerXForAddress - addressQrCodeBitmap.getWidth() / 2, y,
                    qrCodePaint);
            canvas.drawBitmap(privateKeyQrCodeBitmap,
                    centerXForPrivateKey - privateKeyQrCodeBitmap.getWidth() / 2, y, qrCodePaint);
            y += qrCodePadding - textPaint.ascent();
            canvas.drawText(addressUri, centerXForAddress, y + addressQrCodeBitmap.getHeight(), textPaint);
            canvas.drawText(privateKey, centerXForPrivateKey, y + privateKeyQrCodeBitmap.getHeight(),
                    textPaint);
            return bmp;
        }

        @Override
        protected void onPostExecute(final Bitmap bitmap) {
            if (bitmap != null) {
                //DEBUG
                //                    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context);
                //                    android.widget.ImageView view = new android.widget.ImageView(context);
                //                    view.setImageBitmap(bitmap);
                //                    builder.setView(view);
                //                    builder.setPositiveButton(android.R.string.ok, null);
                //                    builder.show();

                PrintHelper printHelper = new PrintHelper(context);
                printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT);
                printHelper.printBitmap(label, bitmap);
            }

        }
    }.execute();
}

From source file:com.busdrone.android.ui.VehicleMarkerRenderer.java

private Bitmap render(int color, String text) {
    TextPaint textPaint = new TextPaint();
    textPaint.setColor(Color.WHITE);
    textPaint.setStyle(Paint.Style.FILL);
    textPaint.setAntiAlias(true);//from   www .j av  a2  s .  c o  m
    textPaint.setTextSize(mTextSize);

    Rect textBounds = new Rect();
    textPaint.getTextBounds(text, 0, text.length(), textBounds);

    int width = mPadding + textBounds.width() + mPadding;
    int height = mPadding + textBounds.height() + mPadding;

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);

    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(color);
    canvas.drawRoundRect(new RectF(0, 0, width, height), mCornerRadius, mCornerRadius, paint);

    canvas.drawText(text, (width / 2f) - (textBounds.width() / 2f), (height / 2f) + (textBounds.height() / 2f),
            textPaint);

    return bitmap;
}

From source file:co.ceryle.segmentedbutton.SegmentedButton.java

private void drawButton() {

    final CharSequence text = getText();
    if (!TextUtils.isEmpty(text)) {
        TextPaint textPaint = getPaint();
        textPaint.getTextBounds(text.toString(), 0, text.length(), textBounds);
    } else {/*from  w  w w . j  a va2  s .  co  m*/
        textBounds.setEmpty();
    }

    final int width = getWidth() - (getPaddingLeft() + getPaddingRight());
    final int height = getHeight() - (getPaddingTop() + getPaddingBottom());

    final Drawable[] drawables = getCompoundDrawables();

    int offSet = 0;
    for (int i = 0; i < drawables.length; i++) {
        if (drawables[i] == null)
            continue;

        drawables[i].copyBounds(drawableBounds);
        switch (i) {
        case 0:
            offSet = (width - (textBounds.width() + drawableBounds.width()) + getRightPaddingOffset()) / 2
                    - getCompoundDrawablePadding();
            break;
        case 1:
            offSet = (height - (textBounds.height() + drawableBounds.height()) + getBottomPaddingOffset()) / 2
                    - getCompoundDrawablePadding();
            break;
        case 2:
            offSet = ((textBounds.width() + drawableBounds.width()) - width + getLeftPaddingOffset()) / 2
                    + getCompoundDrawablePadding();
            break;
        case 3:
            offSet = ((textBounds.height() + drawableBounds.height()) - height + getTopPaddingOffset()) / 2
                    + getCompoundDrawablePadding();
            break;
        }
        if (i % 2 == 0)
            drawableBounds.offset(offSet, 0);
        else
            drawableBounds.offset(0, offSet);

        drawables[i].setBounds(drawableBounds);
    }
}

From source file:org.ciasaboark.tacere.manager.NotificationManagerWrapper.java

private Bitmap createMarkerIcon(Drawable backgroundImage, String text, int width, int height) {

    Bitmap canvasBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    // Create a canvas, that will draw on to canvasBitmap.
    Canvas imageCanvas = new Canvas(canvasBitmap);

    // Draw the image to our canvas
    backgroundImage.draw(imageCanvas);/*  w  w w .ja v a 2  s .c om*/

    // Set up the paint for use with our Canvas
    TextPaint textPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | TextPaint.LINEAR_TEXT_FLAG);
    textPaint.setTextAlign(TextPaint.Align.CENTER);
    textPaint.setTypeface(Typeface.DEFAULT);
    textPaint.setTextSize(100f);
    textPaint.setColor(context.getResources().getColor(android.R.color.white));

    int xPos = (imageCanvas.getWidth() / 2);
    int yPos = (int) ((imageCanvas.getHeight() / 2) - ((textPaint.descent() + textPaint.ascent()) / 2));
    Rect r = new Rect();
    textPaint.getTextBounds(text, 0, text.length(), r);
    //        yPos += (Math.abs(r.height()))/2;

    // Draw the text on top of our image
    imageCanvas.drawText(text, xPos, yPos, textPaint);

    // Combine background and text to a LayerDrawable
    LayerDrawable layerDrawable = new LayerDrawable(
            new Drawable[] { backgroundImage, new BitmapDrawable(canvasBitmap) });
    Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    layerDrawable.setBounds(0, 0, width, height);
    layerDrawable.draw(new Canvas(newBitmap));
    return newBitmap;
}

From source file:com.android.ex.chips.RecipientEditTextView.java

private static float getTextYOffset(final String text, final TextPaint paint, final int height) {
    final Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    final int textHeight = bounds.bottom - bounds.top;
    return height - (height - textHeight) / 2 - (int) paint.descent();
}