android surfaceview 事件

引用:http://blog.sina.com.cn/s/blog_75e063c1010131zf.html

ackage cn.touchSur.rxm;
 
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.FillType;
import android.graphics.Point;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
 
public class TouchAndSurActivity extends Activity {
   
GameView myView;
 
Point point=new Point();
Paint paint = new Paint();
Path path = new Path();
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏显示
        myView = new GameView(this);    
        setContentView(myView);//使其显示视图
    }
    public boolean onTouchEvent(MotionEvent event) {
 
        // TODO Auto-generated method stub
 
        myView.OnTouch(event);
        myView.postInvalidate();
        return super.onTouchEvent(event);
 
}
    
    
public class GameView extends SurfaceView implements SurfaceHolder.Callback {
 
 
        SurfaceHolder surfaceHolder;
 
        
 
        public GameView(Context context) {
 
                super(context);
 
                // TODO Auto-generated constructor stub
 
                surfaceHolder = this.getHolder();  
 
                surfaceHolder.addCallback(this);       
 
                this.setFocusable(true);  
 
        }
 
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
 
        }
 
        public void surfaceCreated(SurfaceHolder holder) {
 
        }
 
        public void surfaceDestroyed(SurfaceHolder holder) {
 
        }
 
        
 
        public void OnTouch(MotionEvent event){
   
                paint.setColor(Color.RED);
          paint.setStyle(Paint.Style.STROKE);   //关键在于设置这个   ,默认情况下为实心!!
    
          paint.setStrokeWidth(3);//设置线条粗细
            switch (event.getAction()) 
            {
             case MotionEvent.ACTION_DOWN:
             path.moveTo(event.getX(), event.getY()-40);//设置起点
              break;
              
             case MotionEvent.ACTION_MOVE:
             path.lineTo(event.getX(), event.getY()-40);      
             Canvas canvas = surfaceHolder.lockCanvas();
             canvas.drawPath(path, paint);//指定轨迹画图       由于path是多个轮廓叠加在一起,所以可以绘制在一张图上面。越往后,path中包含的轮廓越多!   
             surfaceHolder.unlockCanvasAndPost(canvas);
              break;
              
             case MotionEvent.ACTION_UP:
              break;
              
             default:            
              break;
            }//switch
 
           
        }//Ontouch
 
}  //GameView
    
}//TouchAndSurActivity
posted @ 2012-07-26 01:06  镇水古月  阅读(569)  评论(0编辑  收藏  举报