Example usage for android.graphics Paint ascent

List of usage examples for android.graphics Paint ascent

Introduction

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

Prototype

public float ascent() 

Source Link

Document

Return the distance above (negative) the baseline (ascent) based on the current typeface and text size.

Usage

From source file:Main.java

public static float getTextHeight(Paint textPaint) {
    return -textPaint.ascent() - textPaint.descent();
}

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();
    }//www  .j  ava  2 s  . co m
}

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 a v  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

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  2s  .co  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: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);/*from  w w w.ja  v a  2  s .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:com.metinkale.prayerapp.vakit.WidgetService.java

private static Bitmap getIconFromMinutes(long left) {
    String text = left + "";
    Resources r = App.getContext().getResources();
    int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 32, r.getDisplayMetrics());
    Bitmap b = Bitmap.createBitmap(px, px, Bitmap.Config.ARGB_4444);
    Canvas c = new Canvas(b);
    Paint paint = new Paint();
    final float testTextSize = 48f;
    paint.setTextSize(testTextSize);/*  ww  w  .  j  a v  a 2s. c o m*/
    Rect bounds = new Rect();
    paint.getTextBounds(text.length() == 1 ? "0" + text : text, 0, text.length() == 1 ? 2 : text.length(),
            bounds);
    float desiredTextSize = testTextSize * (px * 0.9f) / bounds.width();
    paint.setTextSize(desiredTextSize);
    paint.setColor(0xFFFFFFFF);
    paint.setTextAlign(Paint.Align.CENTER);
    int yPos = (int) ((c.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));
    c.drawText(text, px / 2, yPos, paint);
    c.drawText(text, px / 2, yPos, paint);
    return b;
}

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);/*w w w. j  ava  2  s . 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 - ((paint.descent() + paint.ascent()) / 2);

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

    return bitmap;
}

From source file:de.mrapp.android.util.BitmapUtil.java

/**
 * Creates and returns a bitmap from a specific text. The text is centered.
 *
 * @param context// ww w .j a v  a 2 s  .  c om
 *         The context, which should be used, as an instance of the class {@link Context}. The
 *         context may not be null
 * @param width
 *         The width of the bitmap, which should be created, in pixels as an {@link Integer}
 *         value
 * @param height
 *         The height of the bitmap, which should be created, in pixels as an {@link Integer}
 *         value
 * @param backgroundColor
 *         The background color of the bitmap, which should be created, as an {@link Integer}
 *         value
 * @param text
 *         The text, the bitmap should be created from, as an instance of the type {@link
 *         CharSequence}. The text may neither be null, nor empty
 * @param textSize
 *         The text size, which should be used, in sp as an {@link Integer} value
 * @param textColor
 *         The text color, which should be used, as an {@link Integer} value The color of the
 *         text
 * @param typeface
 *         The typeface, which should be used, as a value of the enum {@link Typeface} or null,
 *         if the default typeface should be used
 * @return The bitmap, which has been created, as an instance of the class {@link Bitmap}
 */
public static Bitmap textToBitmap(@NonNull final Context context, final int width, final int height,
        @ColorInt final int backgroundColor, @NonNull final CharSequence text, final float textSize,
        @ColorInt final int textColor, @Nullable final Typeface typeface) {
    ensureNotNull(context, "The context may not be null");
    ensureNotNull(text, "The text may not be null");
    ensureNotEmpty(text, "The text may not be empty");
    ensureAtLeast(textSize, 1, "The text size must be at least 1");
    Bitmap bitmap = colorToBitmap(width, height, backgroundColor);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(textColor);
    paint.setTextSize(textSize * getDensity(context));
    paint.setTextAlign(Align.CENTER);

    if (typeface != null) {
        paint.setTypeface(typeface);
    }

    int x = bitmap.getWidth() / 2;
    int y = (int) ((bitmap.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));
    canvas.drawText(text.toString(), x, y, paint);
    return bitmap;
}

From source file:org.cicadasong.samples.nextbuses.NextBuses.java

protected void onDraw(Canvas canvas) {
    // Set font for heading
    Paint paint = isWidget() ? metawatch5px : metawatch7px;

    int x = 2;/*  w  ww . ja v  a 2 s.  co  m*/
    int y = 2 + (int) -paint.ascent();

    canvas.drawText("Next buses at...", x, y, paint);

    y += (int) paint.descent() + 2;

    // Set font for stop name
    paint = isWidget() ? metawatch7px : metawatch11px;
    y += (int) -paint.ascent();

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

    y += (int) paint.descent() + 2;

    // Set font for "body"
    paint = isWidget() ? metawatch5px : default10pt;
    y += (int) -paint.ascent();

    if (inInitialFetch) {
        canvas.drawText("Fetching...", x, y, paint);
    } else if (predictions == null) {
        canvas.drawText("Network Error", x, y, paint);
    } else if (isWidget()) {
        String singleLineResult = "";
        List<Prediction> allPredictions = predictions.getAllPredictions();
        StringBuilder resultBuilder = new StringBuilder();
        for (Prediction prediction : allPredictions) {
            if (resultBuilder.length() > 0) {
                resultBuilder.append(" ");
            }
            resultBuilder.append(prediction.route);
            resultBuilder.append("~");
            resultBuilder.append(prediction.minutes);
            resultBuilder.append("m");
        }
        singleLineResult = resultBuilder.toString();
        canvas.drawText(singleLineResult, x, y, paint);
    } else {
        // We're in app mode, so we have more screen space to work with
        for (String route : predictions.getRoutes()) {
            StringBuilder resultBuilder = new StringBuilder();
            resultBuilder.append(route);
            resultBuilder.append(":");
            for (Prediction prediction : predictions.getPredictionsForRoute(route)) {
                resultBuilder.append(" ");
                resultBuilder.append(prediction.minutes);
                resultBuilder.append("m");
            }
            canvas.drawText(resultBuilder.toString(), x, y, paint);
            y += paint.getFontSpacing();
        }
    }
}

From source file:com.github.kubatatami.RoundedView.java

private void drawText(Canvas canvas) {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextSize(getResources().getDimension(R.dimen.item_circle_text_size));

    Rect areaRect = new Rect(0, 0, canvas.getWidth(), canvas.getHeight());
    RectF bounds = new RectF(areaRect);

    bounds.right = paint.measureText(text, 0, text.length());
    bounds.bottom = paint.descent() - paint.ascent();
    bounds.left += (areaRect.width() - bounds.right) / 2.0f;
    bounds.top += (areaRect.height() - bounds.bottom) / 2.0f;

    paint.setColor(Color.WHITE);//  w  ww.ja  va 2 s.c o  m

    canvas.drawText(text, bounds.left, bounds.top - paint.ascent(), paint);
}