Loading

11_SurfaceView绘图

  学习SurfaceView绘图API的使用方法,为接下来使用SurfaceView开发游戏做准备。

 1 package com.example.surfaceview;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 
 6 public class MainActivity extends Activity {
 7 
 8     @Override
 9     protected void onCreate(Bundle savedInstanceState) {
10         super.onCreate(savedInstanceState);
11         setContentView(new MyView(this));
12 
13     }
14 
15 }
MainActivity
 1 package com.example.surfaceview;
 2 
 3 import android.content.Context;
 4 import android.graphics.Canvas;
 5 import android.graphics.Color;
 6 import android.graphics.Paint;
 7 import android.view.SurfaceHolder;
 8 import android.view.SurfaceView;
 9 
10 public class MyView extends SurfaceView implements SurfaceHolder.Callback {
11 
12     private Paint paint = null;
13 
14     public MyView(Context context) {
15         super(context);
16         paint = new Paint();
17         paint.setColor(Color.RED);
18         getHolder().addCallback(this);
19     }
20 
21     // 一定要锁定画布和解锁画布
22     public void draw() {
23         Canvas canvas = getHolder().lockCanvas();
24         canvas.drawColor(Color.WHITE);
25         canvas.drawRect(0, 0, 100, 100, paint);
26 
27         getHolder().unlockCanvasAndPost(canvas);
28 
29     }
30 
31     @Override
32     public void surfaceCreated(SurfaceHolder holder) {
33         draw();
34     }
35 
36     @Override
37     public void surfaceChanged(SurfaceHolder holder, int format, int width,
38             int height) {
39         // TODO Auto-generated method stub
40 
41     }
42 
43     @Override
44     public void surfaceDestroyed(SurfaceHolder holder) {
45         // TODO Auto-generated method stub
46 
47     }
48 
49 }
MyView
posted @ 2014-10-31 16:25  Yano_nankai  阅读(140)  评论(0编辑  收藏  举报