วันศุกร์ที่ 3 มกราคม พ.ศ. 2557

Android Graphics : Drawing Text


Android Graphics : Drawing Text

Loading Fonts
The Android API provides us with a class called Typeface that encapsulates a TrueType font. It provides a simple static method to load such a font file from the assets/

directory:
Typeface font = Typeface.createFromAsset(context.getAssets(), "font.ttf");

Interestingly enough, this method does not throw any kind of Exception if the font file can’t be loaded. Instead a RuntimeException is thrown. Why no explicit exception is thrown for this method is a bit of a mystery to me.

Drawing Text with a Font
Once we have our font, we set it as the Typeface of a Paint instance:
paint.setTypeFace(font);

Via the Paint instance, we also specify the size we want to render the font at:
paint.setTextSize(30);

The documentation of this method is again a little sparse. It doesn’t tell whether the text size is given in points or pixels. We just assume the latter.

Finally, we can draw text with this font via the following Canvas method:
canvas.drawText("This is a test!", 100, 100, paint);

MainActivity.java sourcecode


package android.graphics.drawText;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
           
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                        WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setContentView(new RenderView(this));
      }

      class RenderView extends View {
            Paint paint;
            //Typeface font;
            Bitmap icon;
            Rect bounds = new Rect();

            public RenderView(Context context) {
                  super(context);
                  paint = new Paint();
            }

            protected void onDraw(Canvas canvas) {
                             
                  paint.setColor(Color.BLUE);
                  paint.setTypeface(Typeface.SERIF);
                  paint.setTextSize(60);
                  paint.setTextAlign(Paint.Align.CENTER);              
                  canvas.drawText("Drawing Text Test", canvas.getWidth() / 2, 100,paint);
                  paint.setTextSize(42);
                  canvas.drawText("Paint.Align.CENTER", canvas.getWidth() / 2, 150,paint);
                 
                  String text = "Paint.Align.RIGHT.";
                  paint.setColor(Color.MAGENTA);
                  paint.setTextSize(42);
                  paint.setTextAlign(Paint.Align.RIGHT);               
                  paint.getTextBounds(text, 0, text.length(), bounds);
                  canvas.drawText(text, canvas.getWidth()/2, 250,paint);
                 
                  String text2 = "Paint.Align.LEFT.";
                  paint.setColor(Color.RED);
                  paint.setTextSize(42);
                  paint.setTextAlign(Paint.Align.LEFT);
                  paint.getTextBounds(text2, 0, text2.length(), bounds);
                  canvas.drawText(text2, canvas.getWidth() - bounds.width(), 350,paint);
                  invalidate();                
            }
      }
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น