List of usage examples for android.graphics Canvas drawText
public void drawText(@NonNull String text, float x, float y, @NonNull Paint paint)
From source file:Main.java
public static Bitmap text2Bitmap(String text, int color, float size) { if (TextUtils.isEmpty(text)) { return null; }/*from w w w. j av a2s . c om*/ Paint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG); paint.setTextSize(size); paint.setColor(color); paint.setTextAlign(Paint.Align.LEFT); float baseline = -paint.ascent(); int width = (int) (paint.measureText(text) + 0.5f); int height = (int) (baseline + paint.descent() + 0.5f); Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(image); canvas.drawText(text, 0, baseline, paint); return image; }
From source file:Main.java
private static Drawable getMoreSuggestionsHint(final Resources res, final float textSize, final int color) { final Paint paint = new Paint(); paint.setAntiAlias(true);/*from www.java 2 s.c o m*/ 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); return new BitmapDrawable(res, buffer); }
From source file:Main.java
public static void drawButtonText(Canvas canvas, Paint paint, Rect btnRect, String text) { int nLen = text.length(); canvas.drawText(text, btnRect.left + btnRect.width() / 2 - 4 * nLen, btnRect.top + btnRect.height() / 2 + nLen, paint); }
From source file:Main.java
private static Drawable getMoreSuggestionsHint(final Resources res, final float textSize, final int color) { final Paint paint = new Paint(); paint.setAntiAlias(true);/*from w ww. j a va 2 s . c o m*/ 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; }
From source file:Main.java
public static void drawMultiline(Canvas canvas, String str, int x, int y, Paint paint) { for (String line : str.split("\n")) { canvas.drawText(line, x, y, paint); y += -paint.ascent() + paint.descent(); }//from ww w . j a va2s . co m }
From source file:Main.java
public static void drawDebugOutput(Canvas c) { int vertical_spacing = 0; for (String key : debugOutput.keySet()) { c.drawText(key + ": " + debugOutput.get(key), 20, 200 + vertical_spacing, whitePaint); vertical_spacing += whitePaint.getTextSize(); }/*from w w w .j a va 2 s. c o m*/ }
From source file:Main.java
/** * The most useful answer about text drawing ever. http://stackoverflow.com/a/32081250 *///from w w w. jav a 2s . co m @Nullable public static Bitmap createTypefaceBitmap(final Context context, @NonNull final String text, final int color, final float textSizePx) { final Typeface robotoMedium = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf"); final Paint paint = new Paint(); paint.setAntiAlias(true); paint.setSubpixelText(true); paint.setTypeface(robotoMedium); paint.setStyle(Paint.Style.FILL); paint.setColor(color); paint.setTextSize(textSizePx); final Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); Bitmap bitmap = null; if (!bounds.isEmpty()) { final int width = bounds.width(); final int height = bounds.height(); bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444); final float x = bounds.left; final float y = height - bounds.bottom; final Canvas canvas = new Canvas(bitmap); canvas.drawText(text, x, y, paint); } return bitmap; }
From source file:Main.java
public static void drawText(Canvas canvas, String text, float x, float y, Paint paint, float angle) { if (angle != 0F) canvas.rotate(angle, x, y);/*from w w w .j a v a2s .com*/ canvas.drawText(text, x, y, paint); if (angle != 0F) canvas.rotate(-angle, x, y); }
From source file:Main.java
public static void drawTextCenter(Canvas canvas, RectF rectf, String s, Paint paint) { Rect rect = new Rect(); paint.getTextBounds(s, 0, s.length(), rect); canvas.drawText(s, rectf.left + (rectf.width() - (float) rect.width()) / 2.0F, rectf.top + (rectf.height() + (float) rect.height()) / 2.0F, paint); }
From source file:Main.java
public static void drawText(String text, float x, float y, Canvas canvas, Paint paint) { Rect rect = new Rect(); paint.getTextBounds(text, 0, 1, rect); float halfW = paint.measureText(text) / 2; canvas.drawText(text, x - halfW, y + halfW, paint); }