本文整理汇总了Java中android.graphics.Paint.getTextBounds方法的典型用法代码示例。如果您正苦于以下问题:Java Paint.getTextBounds方法的具体用法?Java Paint.getTextBounds怎么用?Java Paint.getTextBounds使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.graphics.Paint
的用法示例。
在下文中一共展示了Paint.getTextBounds方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: calcTextBounds
import android.graphics.Paint; //导入方法依赖的package包/类
/**
* 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;
}
示例2: getInputAitSpan
import android.graphics.Paint; //导入方法依赖的package包/类
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);
}
示例3: drawTextToDrawable
import android.graphics.Paint; //导入方法依赖的package包/类
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;
}
示例4: getMoreSuggestionsHint
import android.graphics.Paint; //导入方法依赖的package包/类
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;
}
示例5: initPaints
import android.graphics.Paint; //导入方法依赖的package包/类
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();
}
示例6: addTextWatermark
import android.graphics.Paint; //导入方法依赖的package包/类
/**
* 添加文字水印
*
* @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;
}
示例7: addSubmenuItem
import android.graphics.Paint; //导入方法依赖的package包/类
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;
}
}
}
示例8: build
import android.graphics.Paint; //导入方法依赖的package包/类
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);
}
示例9: renderTextOnBitmap
import android.graphics.Paint; //导入方法依赖的package包/类
/**
* 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);
}
示例10: setupPaint
import android.graphics.Paint; //导入方法依赖的package包/类
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);
}
示例11: drawTextToBitmap
import android.graphics.Paint; //导入方法依赖的package包/类
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;
}
示例12: ChangeColorIconWithText
import android.graphics.Paint; //导入方法依赖的package包/类
/**
* 获取自定义属性的值
*
* @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);
}
示例13: generateDefaultAvatar
import android.graphics.Paint; //导入方法依赖的package包/类
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);
}
示例14: getStringWidth
import android.graphics.Paint; //导入方法依赖的package包/类
public static float getStringWidth(final String string, final Paint paint) {
synchronized (sStringWidthBounds) {
paint.getTextBounds(string, 0, string.length(), sStringWidthBounds);
return sStringWidthBounds.width();
}
}
示例15: getTextWidth
import android.graphics.Paint; //导入方法依赖的package包/类
private int getTextWidth(String str, Paint paint) {
paint.getTextBounds(str, 0, str.length(), mBounds);
return mBounds.width();
}