zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    generatorConfiguration详解
    fastjson java类、字符串、jsonObject之前的转换
    非CI执行Allure2 trends空白问题
    如何相互转换逗号分隔的字符串和List【转】
    搞服务器性能时有感
    EF实体实现链接字符串加密
    小程序接口测试之本地测试
    WebApi生成在线API文档--Swagger
    C# 调用IP库(QQWry.Dat)查询IP位置及自动升级IP库方法【转】
    懵逼的this指向
  • 原文地址:https://www.cnblogs.com/sode/p/2609255.html
Copyright © 2011-2022 走看看