facebook twitter hatena line google mixi email

Android/図形view

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

独自View

onDrawが自動で実行され描画される。

  • src/TestActivity.java
public class TestActivity extends Activity {
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       MyCircleView view = new MyCircleView(getApplicationContext());
       setContentView(view);
   }
}
  • src/MyCircleView.java
class MyCircleView extends View {
   public MyCircleView(Context context) {
       super(context);
       setFocusable(true);
   }
   protected void onDraw(Canvas canvas) {
       super.onDraw(canvas);
       canvas.drawColor(Color.WHITE);
       Paint paint = new Paint();
       paint.setAntiAlias(true);
       paint.setStyle(Paint.Style.FILL);
       paint.setColor(Color.BLUE);
       canvas.drawCircle(150, 150, 100, paint);
   }
}

viewのonDrawはview.invalidate()で呼び出せる。

参照

http://www.hakkaku.net/articles/20090206-341