ホームへ戻る

4.5 文字を描く


本節では文字を描画します。
今回もpaintを利用すると多種多様な書き方が出来るようになります。
本節では、中でもデフォルト表示と、パスに沿った描画を紹介します。


public class AndroidsCastleActivity extends Activity {

        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(new MyView(this));
        }

        class MyView extends View {

                public MyView(Context context) {
                        super(context);
                        
                }

                @Override
                protected void onDraw(Canvas canvas) {
                
                        Paint paint = new Paint();                      
                        paint.setColor(Color.RED);

                        canvas.drawText("ハローAndroid!!", 10, 20, paint);//テキスト描画

                        Path path = new Path();//パスを作って円を追加
                        path.addCircle(150, 150, 60, Direction.CCW);
                        
                        paint.setTextSize(30);  //テキストサイズセット

                        canvas.drawTextOnPath("Androidプログラミング", path, 10, 20, paint);//テキスト描画
                }

        }
}


drawTextが文字列を描画するメソッドです。

drawText( string, x, y, paint );

となります。
また、「パス」を利用することで複雑な軌跡上に描画することが出来ます。
「Path」「Paint」については別の章で詳しく紹介しますが、簡単に言えばPathは位置情報を記憶するクラスです。

pathのaddCircleで、(150,150)を中心とする、半径60の円をセットしています。
Direction.CCW というのは「反時計回り」を示し、時計回りは .CW です。

パスを使った描画は

drawTextOnPath で行っており、引数は

drawTextOnPath( string, path, x方向のオフセット, y方向のオフセット, paint );

となります。

詳細はリファレンスをどうぞ。

→分からないことがあれば掲示板で質問して下さい


Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

- Remical Soft -