Example usage for android.graphics Paint setTextSize

List of usage examples for android.graphics Paint setTextSize

Introduction

In this page you can find the example usage for android.graphics Paint setTextSize.

Prototype

public void setTextSize(float textSize) 

Source Link

Document

Set the paint's text size.

Usage

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  w w.j  a  v  a 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 Rect getTextRec(String text, Paint paint, float textSize) {
    Rect rect = new Rect();
    if (paint != null && text != null) {
        if (0 < textSize) {
            paint.setTextSize(textSize);
        }/*from  w  w w.  j av  a  2s . com*/
        paint.getTextBounds(text, 0, text.length(), rect);
    }
    return rect;
}

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)));
    }/*from w  w w  .ja v  a2 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

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 av  a  2  s. co  m
    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

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);/* ww  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:Main.java

/**
 * Create round, coloured bitmap with text embedded.
 * @param circleColor The color to use.//w ww  .  ja va2 s. c  o  m
 * @param diameterDP The diameter of the circle.
 * @param text The text to embed.
 * @return Bitmap showing a text.
 */
public static Bitmap generateCircleBitmap(int circleColor, float diameterDP, String text) {
    /**
     *
     * http://stackoverflow.com/questions/31168636/rounded-quickcontactbadge-with-text
     */
    final int textColor = 0xffffffff;

    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
    float diameterPixels = diameterDP * (metrics.densityDpi / 160f);
    float radiusPixels = diameterPixels / 2;

    // Create the bitmap
    Bitmap output = Bitmap.createBitmap((int) diameterPixels, (int) diameterPixels, Bitmap.Config.ARGB_8888);

    // Create the canvas to draw on
    Canvas canvas = new Canvas(output);
    canvas.drawARGB(0, 0, 0, 0);

    // Draw the circle
    final Paint paintC = new Paint();
    paintC.setAntiAlias(true);
    paintC.setColor(circleColor);
    canvas.drawCircle(radiusPixels, radiusPixels, radiusPixels, paintC);

    // Draw the text
    if (text != null && text.length() > 0) {
        final Paint paintT = new Paint();
        paintT.setColor(textColor);
        paintT.setAntiAlias(true);
        paintT.setTextSize(radiusPixels * 2);
        paintT.setTypeface(Typeface.SANS_SERIF);
        final Rect textBounds = new Rect();
        paintT.getTextBounds(text, 0, text.length(), textBounds);
        canvas.drawText(text, radiusPixels - textBounds.exactCenterX(),
                radiusPixels - textBounds.exactCenterY(), paintT);
    }

    return output;
}

From source file:Main.java

public static Bitmap drawTextCenterToBitmap(Bitmap bitmap, String text, int textSize, int textColor) {
    android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
    // set default bitmap config if none
    if (bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
    }//from  w w w. j  a va2 s  . c om
    // 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(textColor);
    // text size in pixels
    paint.setTextSize(textSize);
    // text shadow
    paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

    // draw text to the Canvas center
    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;
    //draw  text  to the bottom
    int x = (bitmap.getWidth() - bounds.width()) / 10 * 5;
    int y = (bitmap.getHeight() + bounds.height()) / 10 * 5;
    canvas.drawText(text, x, y, paint);

    return bitmap;
}

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;
    }/*ww  w  . ja v a2  s . com*/
    // 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;
}

From source file:Main.java

public static Bitmap drawTextToBitmap(Context gContext, int gResId, String 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;
    }/*from  ww  w  .j a va  2 s .  c om*/
    // 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 - #808080
    paint.setColor(Color.rgb(127, 127, 127));
    // text size in pixels
    paint.setTextSize((int) (14 * scale * 5));
    // 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() + bounds.height()) / 2;
    //draw  text  to the bottom
    int x = (bitmap.getWidth() - bounds.width()) / 10 * 9;
    int y = (bitmap.getHeight() + bounds.height()) / 10 * 9;
    canvas.drawText(gText, x, y, paint);

    return bitmap;
}

From source file:Main.java

private static Paint getTextPaint(float size) {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setShadowLayer(1, 1, 1, 0xFF000000);
    paint.setColor(0xFFFFFFFF);/*  w w w.  j a v  a  2 s. c  o m*/
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(size);
    paint.setTypeface(Typeface.DEFAULT);
    return paint;

}