libro
www.tuyano.com
Google androidプログラミング入門

グラフィックを描こう! (2/9)

作成:2009-12-28 13:28
更新:2010-05-11 14:26

■DrawViewを作る

では、ごく簡単な図形を描画する「DrawView」クラスを作成し、これを画面に表示させてみましょう。今回は、レイアウトのXMLは特に使いません。すべてJavaのソースコードのみで動きます。では、SampleApp.javaのソースコードを以下のリストのように修正しましょう。

プログラムを動かしてみると、画面には青い四角形と赤い円が表示されます。ごくごくシンプルですが、プログラムで図形を描画して表示しているのがわかるでしょう。

※プログラムリストが表示されない場合

AddBlockなどの広告ブロックツールがONになっていると、プログラムリスト等が表示されない場合があります。これらのツールをOFFにしてみてください。

●プログラム・リスト●

package jp.tuyano;


import android.app.Activity;
import android.content.Context;
import android.graphics.*;
import android.os.Bundle;
import android.view.View;


public class SampleApp extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DrawView view = new DrawView(getApplication());
        setContentView(view);
    }


    class DrawView extends View {


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


        public void onDraw(Canvas c) {
            c.drawColor(Color.BLACK);
            
            Paint fill_paint = new Paint();
            fill_paint.setStyle(Paint.Style.FILL);
            fill_paint.setColor(Color.BLUE);
            c.drawRect(100f, 100f, 200f, 200f, fill_paint);


            fill_paint.setColor(Color.RED);
            c.drawOval(new RectF(0f, 0f, 100f, 100f), fill_paint);
        }


    }
}

※関連コンテンツ

「Google androidプログラミング入門」に戻る