本文整理汇总了Java中android.text.TextPaint.getTextBounds方法的典型用法代码示例。如果您正苦于以下问题:Java TextPaint.getTextBounds方法的具体用法?Java TextPaint.getTextBounds怎么用?Java TextPaint.getTextBounds使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.text.TextPaint
的用法示例。
在下文中一共展示了TextPaint.getTextBounds方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: GifBadge
import android.text.TextPaint; //导入方法依赖的package包/类
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();
}
示例2: getStringWidth
import android.text.TextPaint; //导入方法依赖的package包/类
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;
}
示例3: CharView
import android.text.TextPaint; //导入方法依赖的package包/类
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);
}
示例4: drawBubble
import android.text.TextPaint; //导入方法依赖的package包/类
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);
}
示例5: buildSrcFromName
import android.text.TextPaint; //导入方法依赖的package包/类
@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;
}