View游戏框架--android开发

主要通过重写

@Override
    public void draw(Canvas canvas) {  }

然后通过invalidate();或者postInvalidate();重绘画布,不断更新页面

坦克图片材料

部分源码:

MyView

 1 package caicai.animation;
 2 
 3 import android.content.Context;
 4 import android.graphics.Bitmap;
 5 import android.graphics.BitmapFactory;
 6 import android.graphics.Canvas;
 7 import android.graphics.Color;
 8 import android.graphics.Paint;
 9 import android.view.MotionEvent;
10 import android.view.View;
11 
12 public class MyView extends View {
13  private Paint paint;
14     private int x = 0, y = 0;// 图片坐标
15     private Bitmap bitmap;// 图片资源
16     public MyView(Context context) {
17         super(context);
18         paint=new Paint();
19         paint.setColor(Color.WHITE);
20         bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.tanke);
21         setFocusable(true);
22         setFocusableInTouchMode(true);
23     }
24 
25     @Override
26     public void draw(Canvas canvas) {    
27         canvas.drawBitmap(bitmap, x, y, paint);
28         super.draw(canvas);
29     }
30     
31     /**
32 //     * 重写触屏事件函数
33 //     */
34     @Override
35     public boolean onTouchEvent(MotionEvent event) {
36         int x1 = (int)event.getX();
37         int y1 = (int)event.getY();
38         //玩家手指点击屏幕的动作
39         if (event.getAction() == MotionEvent.ACTION_DOWN) {
40             x = x1;
41             y = y1;
42             //玩家手指抬起离开屏幕的动作
43         } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
44             x = x1;
45             y = y1;
46             //玩家手指在屏幕上移动的动作
47         } else if (event.getAction() == MotionEvent.ACTION_UP) {
48             x = x1;
49             y = y1;
50         }
51         //重绘画布
52         invalidate();//或者用postInvalidate();刷新画布        
53         return true;
54     }
55 }
MyView

MainActivity

 1 package caicai.animation;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.Window;
 6 import android.view.WindowManager;
 7 
 8 public class MainActivity extends Activity {
 9     /** Called when the activity is first created. */
10     @Override
11     public void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         //设置全屏
14         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
15         requestWindowFeature(Window.FEATURE_NO_TITLE);
16         //显示自定义的SurfaceView视图
17         setContentView(new MyView(this));
18 
19     }
20 }
MainActivity

支持下趣淘鼓浪屿(www.qtgly.com)

 

 

  

posted on 2013-08-27 11:08  clarenceV1  阅读(283)  评论(0编辑  收藏  举报

导航