Java Code Examples for android.graphics.Paint.getTextBounds()

The following are Jave code examples for showing how to use getTextBounds() of the android.graphics.Paint class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: greendao_expand   File: CircleProgressView.java   View Source Code Vote up 6 votes
/**
 * Returns the bounding rectangle of the given _text, with the size and style defined in the _textPaint centered in the middle of the _textBounds
 *
 * @param _text       The text.
 * @param _textPaint  The paint defining the text size and style.
 * @param _textBounds The rect where the text will be centered.
 * @return The bounding box of the text centered in the _textBounds.
 */
private RectF calcTextBounds(String _text, Paint _textPaint, RectF _textBounds) {

    Rect textBoundsTmp = new Rect();

    //get current text bounds
    _textPaint.getTextBounds(_text, 0, _text.length(), textBoundsTmp);
    float width = textBoundsTmp.left + textBoundsTmp.width();
    float height = textBoundsTmp.bottom + textBoundsTmp.height() * 0.93f; // the height of calcTextBounds is a bit to high, therefore  * 0.93
    //center in circle
    RectF textRect = new RectF();
    textRect.left = (_textBounds.left + ((_textBounds.width() - width) / 2));
    textRect.top = _textBounds.top + ((_textBounds.height() - height) / 2);
    textRect.right = textRect.left + width;
    textRect.bottom = textRect.top + height;


    return textRect;
}
 
Example 2
Project: decoy   File: AitHelper.java   View Source Code Vote up 6 votes
public static ImageSpan getInputAitSpan(String name, float textsize) {
    if (TextUtils.isEmpty(name)) {
        return null;
    }
    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    paint.setAntiAlias(true);
    paint.setTextSize(textsize);
    Rect rect = new Rect();

    paint.getTextBounds(name, 0, name.length(), rect);

    // 获取字符串在屏幕上的长度
    int width = (int) (paint.measureText(name));

    final Bitmap bmp = Bitmap.createBitmap(width, rect.height(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bmp);

    canvas.drawText(name, rect.left, rect.height() - rect.bottom, paint);

    return new ImageSpan(NimUIKit.getContext(), bmp, ImageSpan.ALIGN_BOTTOM);
}
 
Example 3
Project: memetastic   File: ContextUtils.java   View Source Code Vote up 6 votes
public Bitmap drawTextToDrawable(@DrawableRes int resId, String text, int textSize) {
    Resources resources = _context.getResources();
    float scale = resources.getDisplayMetrics().density;
    Bitmap bitmap = getBitmapFromDrawable(resId);

    bitmap = bitmap.copy(bitmap.getConfig(), true);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.rgb(61, 61, 61));
    paint.setTextSize((int) (textSize * scale));
    paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    int x = (bitmap.getWidth() - bounds.width()) / 2;
    int y = (bitmap.getHeight() + bounds.height()) / 2;
    canvas.drawText(text, x, y, paint);

    return bitmap;
}
 
Example 4
Project: AOSP-Kayboard-7.1.2   File: SuggestionStripLayoutHelper.java   View Source Code Vote up 6 votes
private static Drawable getMoreSuggestionsHint(final Resources res, final float textSize,
        final int color) {
    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(textSize);
    paint.setColor(color);
    final Rect bounds = new Rect();
    paint.getTextBounds(MORE_SUGGESTIONS_HINT, 0, MORE_SUGGESTIONS_HINT.length(), bounds);
    final int width = Math.round(bounds.width() + 0.5f);
    final int height = Math.round(bounds.height() + 0.5f);
    final Bitmap buffer = Bitmap.createBitmap(width, (height * 3 / 2), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(buffer);
    canvas.drawText(MORE_SUGGESTIONS_HINT, width / 2, height, paint);
    BitmapDrawable bitmapDrawable = new BitmapDrawable(res, buffer);
    bitmapDrawable.setTargetDensity(canvas);
    return bitmapDrawable;
}
 
Example 5
Project: jackknife   File: ShadeView.java   View Source Code Vote up 5 votes
private void initPaints() {
    mTextPaint = new Paint();
    mTextPaint.setTextSize(mTextSize);
    mTextPaint.setColor(mTextColor);
    mTextPaint.setAntiAlias(true);
    mTextPaint.setDither(true);
    mTextPaint.getTextBounds(mText, 0, mText.length(), mTextRect);
    mIconPaint = new Paint();
}
 
Example 6
Project: BaseCore   File: ImageUtils.java   View Source Code Vote up 5 votes
/**
 * 添加文字水印
 *
 * @param src      源图片
 * @param content  水印文本
 * @param textSize 水印字体大小
 * @param color    水印字体颜色
 * @param x        起始坐标x
 * @param y        起始坐标y
 * @param recycle  是否回收
 * @return 带有文字水印的图片
 */
public static Bitmap addTextWatermark(Bitmap src, String content, float textSize, int color, float x,
                                      float y, boolean recycle) {
    if (isEmptyBitmap(src) || content == null) return null;
    Bitmap ret = src.copy(src.getConfig(), true);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Canvas canvas = new Canvas(ret);
    paint.setColor(color);
    paint.setTextSize(textSize);
    Rect bounds = new Rect();
    paint.getTextBounds(content, 0, content.length(), bounds);
    canvas.drawText(content, x, y + textSize, paint);
    if (recycle && !src.isRecycled()) src.recycle();
    return ret;
}
 
Example 7
Project: aos-MediaLib   File: ActionBarSubmenu.java   View Source Code Vote up 5 votes
public void addSubmenuItem(int iconId, int titleId, long itemId) {
    // Add the item to the internal list
    mItemList.add(new SubmenuItemData(iconId, titleId, itemId));

    // Compute the width of the title 
    Paint paint = new Paint();
    Rect bounds = new Rect();
    int textWidth;

    paint.setTypeface(Typeface.DEFAULT);
    paint.setTextSize(mSubmenuFontSize);

    String title = mContext.getString(titleId);
    paint.getTextBounds(title, 0, title.length(), bounds);
    textWidth =  bounds.width();

    // Check if this is the longuest title of the submenu
    if (textWidth > mSubmenuItemTitleMaxWidth) {
        mSubmenuItemTitleMaxWidth = textWidth;
    }

    // Check if this is the widest icon of the submenu
    if (iconId>0) {
        int iconWidth = getBitmapWidth(iconId);
        if (iconWidth > mIconMaxWidth) {
            mIconMaxWidth = iconWidth;
        }
    }
}
 
Example 8
Project: financisto1-holo   File: GraphStyle.java   View Source Code Vote up 5 votes
public GraphStyle build() {
          float density = context.getResources().getDisplayMetrics().density;
	Rect rect = new Rect();
	Paint namePaint = new Paint();
	Paint amountPaint = new Paint();
	Paint linePaint = new Paint();
	namePaint.setColor(Color.WHITE);
	namePaint.setAntiAlias(true);
	namePaint.setTextAlign(Align.LEFT);
	namePaint.setTextSize(spToPx(nameTextSize, density));
	namePaint.setTypeface(Typeface.DEFAULT_BOLD);
	namePaint.getTextBounds("A", 0, 1, rect);		
	int nameHeight = rect.height();
	amountPaint.setColor(Color.WHITE);
	amountPaint.setAntiAlias(true);
	amountPaint.setTextSize(spToPx(amountTextSize, density));
	amountPaint.setTextAlign(Align.CENTER);
	amountPaint.getTextBounds("8", 0, 1, rect);		
	int amountHeight = rect.height();
	linePaint.setStyle(Style.FILL);
	return new GraphStyle(
			spToPx(dy, density),
                  spToPx(textDy, density),
                  spToPx(indent, density),
			spToPx(lineHeight, density),
                  nameHeight,
                  amountHeight,
			namePaint,
                  amountPaint,
                  linePaint);
}
 
Example 9
Project: Loyalty   File: BarcodeGenerator.java   View Source Code Vote up 5 votes
/**
 * Renders an arbitrary string (centered) at the bottom of the given input Bitmap. The given
 * input Bitmap will be altered and must thus be mutable.
 *
 * @param input The Bitmap on which a String should be rendered (This Bitmap must be mutable)
 * @param text The text that should be rendered.
 */
private void renderTextOnBitmap(Bitmap input, String text) {

    Canvas canvas = new Canvas(input);

    Paint paint = getBarcodePaint();

    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    int x = (input.getWidth() - bounds.width())/2;
    int y = input.getHeight() - 25;

    canvas.drawText(text, x, y, paint);
}
 
Example 10
Project: CodeWatch   File: PerformanceBarView.java   View Source Code Vote up 5 votes
private void setupPaint() {
    progressBarPaint = new Paint();
    progressBarPaint.setColor((change < 100) ? barNegativeColor : barPositiveColor);

    barBorderPaint = new Paint();
    barBorderPaint.setColor(barBorderColor);
    barBorderPaint.setStrokeWidth(barBorderThickness);
    if (shadowEnabled) {
        setLayerType(LAYER_TYPE_SOFTWARE, barBorderPaint);
        barBorderPaint.setShadowLayer(6, 2, 2, Color.parseColor("#424242"));
    }

    whitePaint = new Paint();
    whitePaint.setColor(Color.WHITE);

    textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
    textPaint.setColor(textColor);
    textPaint.setTypeface(Typeface.create("sans-serif-condensed", Typeface.ITALIC));
    textPaint.setTextSize(UiUtils.dpToPx(14));
    textPaint.setStyle(Paint.Style.FILL);

    markerLinePaint = new Paint();
    markerLinePaint.setColor(markerLineColor);
    markerLinePaint.setStrokeWidth(UiUtils.dpToPx(2));

    Rect rect = new Rect();
    textPaint.getTextBounds("23 Mar", 0, 6, rect);
    estimatedTextWidth = rect.width();
    estimatedTextHeight = UiUtils.dpToPx(rect.height());

    extraDateHeight = estimatedTextHeight + UiUtils.dpToPx(8);
}
 
Example 11
Project: react-native-sunmi-inner-printer   File: BitmapUtils.java   View Source Code Vote up 5 votes
public static Bitmap drawTextToBitmap(Context gContext, int gResId, String gText) {  
    	  Log.i(TAG, "drawTextToBitmap = " + gText);
		  Resources resources = gContext.getResources();  
		  float scale = resources.getDisplayMetrics().density;  
		  Bitmap bitmap =   
		     BitmapFactory.decodeResource(resources, gResId);  
		   
		  Bitmap.Config bitmapConfig =
		      bitmap.getConfig();  
		  // set default bitmap config if none   
		 if(bitmapConfig == null) {  
		    bitmapConfig = Bitmap.Config.ARGB_8888;
		  }  
		  // resource bitmaps are imutable,    
		  // so we need to convert it to mutable one   
		  bitmap = bitmap.copy(bitmapConfig, true);  
		   
		  Canvas canvas = new Canvas(bitmap);  
		  // new antialised Paint   
		  Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);  
		  // text color - #3D3D3D   
		  paint.setColor(Color.WHITE);  
		  // text size in pixels   
		  paint.setTextSize((int) (12 * scale));  
		  // text shadow   
//		  paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);  
		   
		  // draw text to the Canvas center   
		  Rect bounds = new Rect();  
		  paint.getTextBounds(gText, 0, gText.length(), bounds);  
		  int x = (bitmap.getWidth() - bounds.width())/2;  
		  int y = (bitmap.getHeight())/2 + (int)scale*2;  
		   
		  canvas.drawText(gText,  x, y, paint);  
	      
		  canvas.save(Canvas.ALL_SAVE_FLAG); 
	      canvas.restore();
		   
		  return bitmap;  
	}
 
Example 12
Project: beyondwords   File: ChangeColorIconWithText.java   View Source Code Vote up 5 votes
/**
 * 获取自定义属性的值
 *
 * @param context
 * @param attrs
 * @param defStyleAttr
 */

public ChangeColorIconWithText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.ChangeColorIconWithText);

    int n = a.getIndexCount();

    for (int i = 0; i < n; i++) {
        int attr = a.getIndex(i);
        switch (attr) {
            case R.styleable.ChangeColorIconWithText_icon:
                BitmapDrawable drawable = (BitmapDrawable) a.getDrawable(attr);
                mIconBitmap = drawable.getBitmap();
                break;
            case R.styleable.ChangeColorIconWithText_color:
                mColor = a.getColor(attr, 0x03a9f4);
                break;
            case R.styleable.ChangeColorIconWithText_text:
                mText = a.getString(attr);
                break;
            case R.styleable.ChangeColorIconWithText_text_size:
                mTextSize = (int) a.getDimension(attr, TypedValue
                        .applyDimension(TypedValue.COMPLEX_UNIT_SP, 12,
                                getResources().getDisplayMetrics()));
                break;
        }
    }

    a.recycle();

    mTextBound = new Rect();
    mTextPaint = new Paint();
    mTextPaint.setTextSize(mTextSize);
    mTextPaint.setColor(0Xff555555);
    mTextPaint.getTextBounds(mText, 0, mText.length(), mTextBound);
}
 
Example 13
Project: lqrwechatrongcloud   File: RongGenerate.java   View Source Code Vote up 5 votes
public static String generateDefaultAvatar(String username, String userid) {

        String s = null;
        if (!TextUtils.isEmpty(username)) {
            s = String.valueOf(username.charAt(0));
        }
        if (s == null) {
            s = "A";
        }
        String color = getColorRGB(userid);
        String string = getAllFirstLetter(username);
        createDir(SAVEADDRESS);
        File f = new File(SAVEADDRESS, string + "_" + userid);
        if (f.exists()) {
            return SCHEMA + f.getPath();
        }
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setTextSize(220);
        paint.setAntiAlias(true);
        int width = 480;
        int height = 480;
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.parseColor(color));
        Rect rect = new Rect();
        paint.getTextBounds(s, 0, s.length(), rect);
        Paint.FontMetrics fm = paint.getFontMetrics();
        int textLeft = (int) ((width - paint.measureText(s)) / 2);
        int textTop = (int) (height - width / 2 + Math.abs(fm.ascent) / 2 - 25);
        canvas.drawText(s, textLeft, textTop, paint);
        return saveBitmap(bitmap, string + "_" + userid);
    }
 
Example 14
Project: LQRWeChat-master   File: RongGenerate.java   View Source Code Vote up 5 votes
public static String generateDefaultAvatar(String username, String userid) {

        String s = null;
        if (!TextUtils.isEmpty(username)) {
            s = String.valueOf(username.charAt(0));
        }
        if (s == null) {
            s = "A";
        }
        String color = getColorRGB(userid);
        String string = getAllFirstLetter(username);
        createDir(SAVEADDRESS);
        File f = new File(SAVEADDRESS, string + "_" + userid);
        if (f.exists()) {
            return SCHEMA + f.getPath();
        }
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setTextSize(220);
        paint.setAntiAlias(true);
        int width = 480;
        int height = 480;
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.parseColor(color));
        Rect rect = new Rect();
        paint.getTextBounds(s, 0, s.length(), rect);
        Paint.FontMetrics fm = paint.getFontMetrics();
        int textLeft = (int) ((width - paint.measureText(s)) / 2);
        int textTop = (int) (height - width / 2 + Math.abs(fm.ascent) / 2 - 25);
        canvas.drawText(s, textLeft, textTop, paint);
        return saveBitmap(bitmap, string + "_" + userid);
    }
 
Example 15
Project: simple-keyboard   File: TypefaceUtils.java   View Source Code Vote up 4 votes
public static float getStringWidth(final String string, final Paint paint) {
    synchronized (sStringWidthBounds) {
        paint.getTextBounds(string, 0, string.length(), sStringWidthBounds);
        return sStringWidthBounds.width();
    }
}
 
Example 16
Project: LotteryView   File: LotteryView.java   View Source Code Vote up 4 votes
private int getTextWidth(String str, Paint paint) {
    paint.getTextBounds(str, 0, str.length(), mBounds);
    return mBounds.width();
}
 
Example 17
Project: GitHub   File: Utils.java   View Source Code Vote up 4 votes
public static void drawXAxisValue(Canvas c, String text, float x, float y,
                                  Paint paint,
                                  MPPointF anchor, float angleDegrees) {

    float drawOffsetX = 0.f;
    float drawOffsetY = 0.f;

    final float lineHeight = paint.getFontMetrics(mFontMetricsBuffer);
    paint.getTextBounds(text, 0, text.length(), mDrawTextRectBuffer);

    // Android sometimes has pre-padding
    drawOffsetX -= mDrawTextRectBuffer.left;

    // Android does not snap the bounds to line boundaries,
    //  and draws from bottom to top.
    // And we want to normalize it.
    drawOffsetY += -mFontMetricsBuffer.ascent;

    // To have a consistent point of reference, we always draw left-aligned
    Paint.Align originalTextAlign = paint.getTextAlign();
    paint.setTextAlign(Paint.Align.LEFT);

    if (angleDegrees != 0.f) {

        // Move the text drawing rect in a way that it always rotates around its center
        drawOffsetX -= mDrawTextRectBuffer.width() * 0.5f;
        drawOffsetY -= lineHeight * 0.5f;

        float translateX = x;
        float translateY = y;

        // Move the "outer" rect relative to the anchor, assuming its centered
        if (anchor.x != 0.5f || anchor.y != 0.5f) {
            final FSize rotatedSize = getSizeOfRotatedRectangleByDegrees(
                    mDrawTextRectBuffer.width(),
                    lineHeight,
                    angleDegrees);

            translateX -= rotatedSize.width * (anchor.x - 0.5f);
            translateY -= rotatedSize.height * (anchor.y - 0.5f);
            FSize.recycleInstance(rotatedSize);
        }

        c.save();
        c.translate(translateX, translateY);
        c.rotate(angleDegrees);

        c.drawText(text, drawOffsetX, drawOffsetY, paint);

        c.restore();
    } else {
        if (anchor.x != 0.f || anchor.y != 0.f) {

            drawOffsetX -= mDrawTextRectBuffer.width() * anchor.x;
            drawOffsetY -= lineHeight * anchor.y;
        }

        drawOffsetX += x;
        drawOffsetY += y;

        c.drawText(text, drawOffsetX, drawOffsetY, paint);
    }

    paint.setTextAlign(originalTextAlign);
}
 
Example 18
Project: Huahui-Android   File: ProgressButton.java   View Source Code Vote up 4 votes
public float getFontlength(Paint paint, String str) {
    Rect rect = new Rect();
    paint.getTextBounds(str, 0, str.length(), rect);
    return rect.width();
}
 
Example 19
Project: fastindex-master   File: FastIndexBar.java   View Source Code Vote up 4 votes
private int getTextHeight(String mText, Paint mPaint) {
    Rect mBounds = new Rect();
    mPaint.getTextBounds(mText,0,mText.length(),mBounds);// 方法执行结束后 mBounds 会被赋值
    return mBounds.height();
}
 
Example 20
Project: VirtualHook   File: BaseView.java   View Source Code Vote up 4 votes
public float getFontlength(Paint paint, String str) {
    Rect rect = new Rect();
    paint.getTextBounds(str, 0, str.length(), rect);
    return rect.width();
}