Android Open Source - android_game_engine Draw Text Utils






From Project

Back to project page android_game_engine.

License

The source code is released under:

GNU General Public License

If you think the Android project android_game_engine listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.es.engine.graphics.utils;
/* w  w w. j av  a 2 s  .c o m*/
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;

/**
 * Created by Cyril Leroux on 27/02/14.
 */
public class DrawTextUtils {

    public static enum VerticalAlign {
        TOP,
        BOTTOM,
        CENTER
    }

    public static enum HorizontalAlign {
        RIGHT,
        LEFT,
        CENTER
    }

    public static RectF drawText(String text, Canvas canvas, float x, float y, HorizontalAlign hAlign, VerticalAlign vAlign, Paint paint) {

        // Save paint align.
        Paint.Align initialAlign = paint.getTextAlign();

        Rect textBounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), textBounds);

        RectF onScreenBounds = new RectF(textBounds);

        if (hAlign == HorizontalAlign.CENTER) {
            paint.setTextAlign(Paint.Align.CENTER);
            onScreenBounds.left = x - Math.abs(textBounds.width() / 2f);

        } else if (hAlign == HorizontalAlign.LEFT) {
            paint.setTextAlign(Paint.Align.RIGHT);
            onScreenBounds.left = x - Math.abs(textBounds.width());

        } else if (hAlign == HorizontalAlign.RIGHT) {
            paint.setTextAlign(Paint.Align.LEFT);
            onScreenBounds.left = x;
        }

        if (vAlign == VerticalAlign.CENTER) {
            onScreenBounds.top = y - Math.abs(textBounds.height() / 2f);

        } else if (vAlign == VerticalAlign.TOP) {
            onScreenBounds.top = y - Math.abs(textBounds.height());

        } else if (vAlign == VerticalAlign.BOTTOM) {
            onScreenBounds.top = y;
        }

        onScreenBounds.right = onScreenBounds.left + Math.abs(textBounds.width());
        onScreenBounds.bottom = onScreenBounds.top + Math.abs(textBounds.height());

        canvas.drawText(text, x, onScreenBounds.bottom, paint);

        // Restore paint align.
        paint.setTextAlign(initialAlign);

        return onScreenBounds;
    }
}




Java Source Code List

com.google.example.games.basegameutils.BaseGameActivity.java
com.google.example.games.basegameutils.GameHelperUtils.java
com.google.example.games.basegameutils.GameHelper.java
com.google.example.games.basegameutils.ScreenUtils.java
org.es.engine.audio.Sound.java
org.es.engine.game_mechanic.DrawingThread.java
org.es.engine.game_mechanic.DrawingView.java
org.es.engine.graphics.animation.AnimationCallback.java
org.es.engine.graphics.animation.Animation.java
org.es.engine.graphics.animation.BitmapAnimation.java
org.es.engine.graphics.animation.SpriteSheetAnimation.java
org.es.engine.graphics.drawable.DrawableElement.java
org.es.engine.graphics.sprite.GenericSprite.java
org.es.engine.graphics.sprite.SpriteSheet.java
org.es.engine.graphics.sprite.Sprite.java
org.es.engine.graphics.utils.DrawTextUtils.java
org.es.engine.graphics.utils.DrawingParam.java
org.es.engine.hud.Button.java
org.es.engine.hud.Control.java
org.es.engine.hud.HUD.java
org.es.engine.hud.Text.java
org.es.engine.hud.ToggleButton.java
org.es.engine.toolbox.pathfinding.Node.java
org.es.engine.toolbox.pathfinding.ShortestPath.java