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

private static Bitmap decorate(Bitmap bitmap, char label, int color, boolean glossy) {

    Canvas canvas = new Canvas(bitmap);

    canvas.drawColor(color);/*from   w w  w  .j  a v a2s. com*/

    String labelString = String.valueOf(label);
    Paint textPaint = getTextPaint(bitmap.getHeight() * 0.75f);
    float cx = bitmap.getWidth() / 2;
    float cy = bitmap.getHeight() / 2;
    float oy = 0;

    synchronized (junkRectangle) {
        textPaint.getTextBounds(labelString, 0, 1, junkRectangle);
        oy = (junkRectangle.top + junkRectangle.bottom) / 2;
    }

    canvas.drawText(labelString, cx, cy - oy, textPaint);

    if (glossy) {
        canvas.drawPath(gloss(bitmap.getWidth()), getGlossPaint());
    }

    return bitmap;

}

From source file:Main.java

public static Bitmap drawTextToBitmap(Bitmap bitmap, String gText) {
    android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
    // set default bitmap config if none
    if (bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
    }/*from   ww w .  j  a v  a  2 s .c om*/
    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.rgb(61, 61, 61));
    // text size in pixels
    paint.setTextSize((int) (21)); //* scale));
    // text shadow
    paint.setShadowLayer(2f, 1f, 1f, Color.WHITE);
    int x = bitmap.getWidth() - 150;//bounds.width()) - 150;
    int y = bitmap.getHeight() - 27;//bounds.height()) - 30;
    canvas.drawRect(x, y, x + 150, y + 27, paint);
    canvas.drawText(gText, x, y + 20, paint);
    return bitmap;
}

From source file:Main.java

static Bitmap generatorContactCountIcon(Context context, Bitmap icon) {
    int iconSize = (int) context.getResources().getDimension(android.R.dimen.app_icon_size);
    Bitmap contactIcon = Bitmap.createBitmap(iconSize, iconSize, Config.ARGB_8888);
    Canvas canvas = new Canvas(contactIcon);

    Paint iconPaint = new Paint();
    iconPaint.setDither(true);/*from  w  w  w. ja  v a2 s  .c  o  m*/
    iconPaint.setFilterBitmap(true);
    Rect src = new Rect(0, 0, icon.getWidth(), icon.getHeight());
    Rect dst = new Rect(0, 0, iconSize, iconSize);
    canvas.drawBitmap(icon, src, dst, iconPaint);
    int contacyCount = 11;
    Paint countPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
    countPaint.setColor(Color.RED);
    countPaint.setTextSize(20f);
    countPaint.setTypeface(Typeface.DEFAULT_BOLD);
    canvas.drawText(String.valueOf(contacyCount), iconSize - 18, 25, countPaint);
    return contactIcon;
}

From source file:cc.softwarefactory.lokki.android.utilities.Utils.java

public static Bitmap getDefaultAvatarInitials(Context context, String text) {

    Log.e(TAG, "getDefaultAvatarInitials");

    String initials = getInitials(text);

    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);/*  www  . j  a v  a2s  .  c o m*/
    paint.setTextSize(36);
    paint.setStrokeWidth(4);
    paint.setTextAlign(Paint.Align.CENTER);

    Bitmap bm = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bm);
    canvas.drawColor(context.getResources().getColor(R.color.material_blue_300));

    int distanceFromBaseline = (int) ((paint.descent() + paint.ascent()) / 2);
    int xPos = (canvas.getWidth() / 2);
    int yPos = (canvas.getHeight() / 2) - distanceFromBaseline;
    canvas.drawText(initials, xPos, yPos, paint);

    return bm;
}

From source file:eu.iescities.pilot.rovereto.roveretoexplorer.map.MapManager.java

private static Bitmap writeOnMarker(Context mContext, int drawableId, String text) {
    float scale = mContext.getResources().getDisplayMetrics().density;

    Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), drawableId)
            .copy(Bitmap.Config.ARGB_8888, true);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextAlign(Align.CENTER);/*from w ww. j a  v a 2  s .  co  m*/
    paint.setTextSize(scale * 14);
    paint.setAntiAlias(true);
    paint.setARGB(255, 255, 255, 255);

    Canvas canvas = new Canvas(bitmap);
    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    float x = bitmap.getWidth() / 2;
    float y = bitmap.getHeight() / 2;
    canvas.drawText(text, x, y, paint);

    return bitmap;
}

From source file:Main.java

public static Bitmap drawTextToBitmap(Context gContext, String gText, int frontColor, int backColor) {
    Resources resources = gContext.getResources();
    float scale = resources.getDisplayMetrics().density;
    int w = 1536, h = 1280;
    Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
    Bitmap bmp = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap
    android.graphics.Bitmap.Config bitmapConfig = bmp.getConfig();
    Canvas canvas = new Canvas(bmp);

    // new antialised Paint
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // text color - #3D3D3D
    paint.setColor(frontColor);//from w  w  w.java2  s  . c o  m
    // text size in pixels
    paint.setTextSize((int) (400 * scale));
    // text shadow
    //paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

    // draw text to the Canvas center
    if (backColor != -1) {
        canvas.drawColor(backColor);
    }
    Rect bounds = new Rect();
    paint.getTextBounds(gText, 0, gText.length(), bounds);
    int x = (bmp.getWidth() - bounds.width()) / 2;
    int y = (bmp.getHeight() + bounds.height()) / 2;
    canvas.drawText(gText, x, y, paint);
    return bmp;
}

From source file:eu.trentorise.smartcampus.trentinofamiglia.map.MapManager.java

private static Bitmap writeOnMarker(Context mContext, int drawableId, String text) {
    float scale = mContext.getResources().getDisplayMetrics().density;

    Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), drawableId)
            .copy(Bitmap.Config.ARGB_8888, true);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextAlign(Align.CENTER);//from ww w .  j  a  v a 2s. c o m
    paint.setTextSize(scale * 14);
    paint.setAntiAlias(true);
    paint.setARGB(255, 255, 255, 255);

    Canvas canvas = new Canvas(bitmap);
    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    float x = bitmap.getWidth() / 2;
    float y = bitmap.getHeight() / 2 - 5;
    canvas.drawText(text, x, y, paint);

    return bitmap;
}

From source file:eu.iescities.pilot.rovereto.roveretoexplorer.map.MapManager.java

private static Bitmap writeOnStoryMarker(Context mContext, int drawableId, String text) {
    float scale = mContext.getResources().getDisplayMetrics().density;

    Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), drawableId)
            .copy(Bitmap.Config.ARGB_8888, true);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextAlign(Align.CENTER);//from  w  ww  .j av a 2 s .  co  m
    paint.setTextSize(scale * 14);
    paint.setAntiAlias(true);
    paint.setARGB(255, 255, 255, 255);

    Canvas canvas = new Canvas(bitmap);
    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    float x = bitmap.getWidth() / 2;
    float y = bitmap.getHeight() / 2 - ((paint.descent() + paint.ascent()) / 2);

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

    return bitmap;
}

From source file:Main.java

public static Bitmap drawTextToBitmap(Context mContext, int resourceId, String mText) {
    try {/*from w  w  w . j av  a 2 s . co  m*/
        Resources resources = mContext.getResources();
        float scale = resources.getDisplayMetrics().density;
        Bitmap bitmap = BitmapFactory.decodeResource(resources, resourceId);

        android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
        // set default bitmap config if none
        if (bitmapConfig == null) {
            bitmapConfig = android.graphics.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.rgb(77, 77, 77));
        // text size in pixels
        paint.setTextSize((int) (13 * scale));
        // text shadow
        paint.setShadowLayer(1f, 0f, 1f, Color.DKGRAY);

        // draw text to the Canvas center
        Rect bounds = new Rect();
        paint.getTextBounds(mText, 0, mText.length(), bounds);
        int x = (int) ((bitmap.getWidth() - bounds.width()) / 4);
        int y = (int) ((bitmap.getHeight() + bounds.height()) / 4);

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

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

}

From source file:Main.java

public static Bitmap drawTextToBitmap(Bitmap bitmap, String gText) {
    //Resources resources = gContext.getResources();
    //float scale = resources.getDisplayMetrics().density;

    android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
    // set default bitmap config if none
    if (bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
    }/* www.  j  a  v a 2s  .co  m*/
    // 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.rgb(61, 61, 61));
    // text size in pixels
    paint.setTextSize((int) (21)); //* scale));
    // text shadow
    paint.setShadowLayer(2f, 1f, 1f, Color.WHITE);
    // draw text to the Canvas center
    //Rect bounds = new Rect();
    //paint.getTextBounds(gText, 0, gText.length(), bounds);
    int x = bitmap.getWidth() - 150;//bounds.width()) - 150;
    int y = bitmap.getHeight() - 27;//bounds.height()) - 30;
    // fill
    canvas.drawRect(x, y, x + 150, y + 27, paint);
    canvas.drawText(gText, x, y + 20, paint);
    return bitmap;
}