Example usage for android.graphics Canvas drawText

List of usage examples for android.graphics Canvas drawText

Introduction

In this page you can find the example usage for android.graphics Canvas drawText.

Prototype

public void drawText(@NonNull String text, float x, float y, @NonNull Paint paint) 

Source Link

Document

Draw the text, with origin at (x,y), using the specified paint.

Usage

From source file:Main.java

public static BitmapDrawable writeOnDrawable(Activity actv, Resources res, int drawableId, String text,
        int textSize) {

    Bitmap bm = BitmapFactory.decodeResource(res, drawableId).copy(Bitmap.Config.ARGB_8888, true);

    DisplayMetrics dm = new DisplayMetrics();
    actv.getWindowManager().getDefaultDisplay().getMetrics(dm);

    int pixelSize = (int) ((textSize * dm.scaledDensity));

    if (text.length() > 2) {
        pixelSize = (int) ((textSize * dm.scaledDensity) * (0.5 - (text.length() / 10)));
    }/*w  w  w. j a  v a 2  s . c o  m*/

    Paint paint = new Paint();
    paint.setStyle(Style.FILL);
    paint.setColor(Color.WHITE);
    paint.setTextSize(pixelSize);
    paint.setTextAlign(Paint.Align.CENTER);

    // float adjust = paint.measureText(text);

    Canvas canvas = new Canvas(bm);
    int xPos = (int) ((bm.getWidth() / 2));
    int yPos = (int) ((bm.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));

    canvas.drawText(text, xPos, yPos, paint);

    return new BitmapDrawable(res, bm);
}

From source file:Main.java

/**
 * Draws a string that is aligned by one anchor point and rotated about
 * another anchor point.//w ww .  java 2  s. co m
 * 
 * @param text
 *            the text.
 * @param g2
 *            the graphics device.
 * @param x
 *            the location of the text anchor.
 * @param y
 *            the location of the text anchor.
 * @param textAnchor
 *            the text anchor.
 * @param rotationX
 *            the x-coordinate for the rotation anchor point.
 * @param rotationY
 *            the y-coordinate for the rotation anchor point.
 * @param angle
 *            the rotation angle.
 */
public static void drawRotatedString(final String text, final Canvas g2, final float x, final float y,
        final Paint paint, float angle) {

    //g2.drawCircle(x, y, 2, paint);
    g2.save();
    if (angle == 0.0)
        g2.drawText(text, x, y, paint);
    else {
        g2.rotate((float) Math.toDegrees(angle), x, y);
        g2.drawText(text, x, y, paint);
    }
    g2.restore();
}

From source file:Main.java

public static void drawCenterText(Canvas canvas, String text, Paint paint, Rect rect) {
    paint.getTextBounds(text, 0, text.length(), bounds);
    int x = (int) (rect.left + rect.width() / 2 - bounds.centerX());
    int y = (int) (rect.top + rect.height() / 2 - bounds.centerY());
    //Log.v(TAG," x="+x + " y="+y + " "+ text);
    canvas.drawText(text, x, y, paint);
}

From source file:Main.java

public static Bitmap getTextImage(String text, float size, int width, int height) {
    final Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    final Paint paint = new Paint();
    final Canvas canvas = new Canvas(bmp);

    canvas.drawColor(Color.WHITE);

    paint.setColor(Color.BLACK);/*from   w  w  w . j  av a  2 s . c  o  m*/
    paint.setStyle(Style.FILL);
    paint.setAntiAlias(true);
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(size);
    paint.setTypeface(Typeface.DEFAULT);
    canvas.drawText(text, width / 2, height / 2, paint);

    return bmp;
}

From source file:Main.java

public static void drawTextCenter(Canvas canvas, RectF rectf, String s, String s1, Paint paint, Paint paint1) {
    Rect rect = new Rect();
    paint.getTextBounds(s, 0, s.length(), rect);
    float f = rectf.left + (rectf.width() - (float) rect.width()) / 2.0F;
    float f1 = rectf.top + (rectf.height() + (float) rect.height()) / 2.0F;
    canvas.drawText(s, f, f1, paint);
    Rect rect1 = new Rect();
    paint1.getTextBounds(s, 0, s.length(), rect1);
    canvas.drawText(s1, 6F + (f + (float) rect.width()), (f1 - (float) rect.height()) + (float) rect1.height(),
            paint1);/*  w  w w. jav a  2  s .co m*/
}

From source file:Main.java

public static void drawFPS(Canvas canvas, String text) {
    if (PAINT_FPS == null) {
        PAINT_FPS = new Paint();
        PAINT_FPS.setColor(Color.RED);
        PAINT_FPS.setTextSize(30);/*from   ww w. j  ava2 s .com*/
    }
    int top = canvas.getHeight() - 50;

    clearCanvas(canvas, 10, top - 50, (int) (PAINT_FPS.measureText(text) + 20), canvas.getHeight());
    canvas.drawText(text, 10, top, PAINT_FPS);
}

From source file:Main.java

public static Bitmap int2Icon(Context context, int i) {
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), 0x7f020047);
    Bitmap bitmap1 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
            android.graphics.Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap1);
    Paint paint = new Paint();
    paint.setDither(true);/*from w w w . j a va2  s.  c o m*/
    paint.setFilterBitmap(true);
    canvas.drawBitmap(bitmap, 0.0F, 0.0F, paint);
    bitmap.recycle();
    Paint paint1 = new Paint(257);
    paint1.setColor(-1);
    paint1.setTypeface(Typeface.DEFAULT_BOLD);
    paint1.setTextAlign(android.graphics.Paint.Align.CENTER);
    canvas.drawText(String.valueOf(i), bitmap.getWidth() / 2, 3 + bitmap.getHeight() / 2, paint1);
    return bitmap1;
}

From source file:Main.java

public static Bitmap buildSequence(Context context, String text) {
    Bitmap myBitmap = Bitmap.createBitmap(160, 84, Bitmap.Config.ARGB_4444);
    Canvas myCanvas = new Canvas(myBitmap);
    Paint paint = new Paint();
    //Typeface font = Typeface.createFromAsset(context.getAssets(),"fonts/7LED.ttf");
    paint.setAntiAlias(true);//from   ww  w  .j  a  v  a  2  s .c  o m
    paint.setSubpixelText(true);
    //paint.setTypeface(font);
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.YELLOW);
    paint.setTextSize(65);
    paint.setTextAlign(Paint.Align.CENTER);
    myCanvas.drawText(text, 80, 60, paint);
    return myBitmap;
}

From source file:Main.java

public static Bitmap drawTextToBitmap(Bitmap bitmap, String gText) {
    OutputStream outStream = null;
    Bitmap.Config bitmapConfig = bitmap.getConfig();
    // set default bitmap config if none
    if (bitmapConfig == null) {
        bitmapConfig = Bitmap.Config.ARGB_8888;
    }/*ww w.j  a  va  2  s.c o m*/
    String dataPath = Environment.getExternalStorageDirectory().toString() + "/SignChat/Temp/temp" + "0"
            + pictureNum + ".jpg";

    try {

        FileOutputStream out = new FileOutputStream(dataPath);

        // NEWLY ADDED CODE STARTS HERE [
        Canvas canvas = new Canvas(bitmap);

        Paint paint = new Paint();
        paint.setColor(Color.WHITE); // Text Color
        paint.setStrokeWidth(12); // Text Size
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text
        // Overlapping
        // Pattern
        // some more settings...

        canvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.drawText("Testing...", 10, 10, paint);
        // NEWLY ADDED CODE ENDS HERE ]

        bitmap.compress(CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap drawTextToBitmap(Context context, int resId, String text) {

    Resources resources = context.getResources();
    float scale = resources.getDisplayMetrics().density;
    Bitmap bitmap = BitmapFactory.decodeResource(resources, resId);

    Bitmap.Config bitmapConfig = bitmap.getConfig();
    if (bitmapConfig == null)
        bitmapConfig = Bitmap.Config.ARGB_8888;
    bitmap = bitmap.copy(bitmapConfig, true);

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(context.getResources().getColor(android.R.color.white));
    paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    paint.setTextSize((int) (12 * scale));

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

    canvas.drawText(text, x * scale, y * scale, paint);

    return bitmap;
}