android ontouchevent

下午阅读onTouchEvent资料,发现方法调用有点混乱,没有完全按照api讲的执行,故挑了例子测试有MainActivity类和MySurfaceView类,

基本介绍见 http://blog.csdn.net/xiaominghimi/article/details/6127578

包括这个blog前面有关onTouchEvent介绍,

主要代码如下:

MainActivity
[java] view plaincopyprint?

    package com.s; 
     
    import android.app.Activity; 
    import android.os.Bundle; 
    import android.view.MotionEvent; 
     
    public class MainActivity extends Activity { 
     
        @Override 
        public void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            setContentView(new MySurfaceView(this)); 
             
        } 
         
        @Override 
        public boolean onTouchEvent(MotionEvent event) { 
            boolean result; 
            switch (event.getAction()) { 
                case MotionEvent.ACTION_UP: 
                    result = true; 
                    break; 
                case MotionEvent.ACTION_DOWN: 
                    result = true; 
                    break; 
                case MotionEvent.ACTION_CANCEL: 
                    result = true; 
                    break; 
                case MotionEvent.ACTION_MOVE: 
                    result = false; 
                    break; 
                default : 
                    result = true; 
                    break; 
            } 
            System.out.println("activity touch: " + result); 
            return result; 
        } 
         
    } 



MySurfaceView

[java] view plaincopyprint?

    package com.s; 
     
    import android.content.Context; 
    import android.graphics.Canvas; 
    import android.graphics.Color; 
    import android.graphics.Paint; 
    import android.view.KeyEvent; 
    import android.view.MotionEvent; 
    import android.view.SurfaceHolder; 
    import android.view.SurfaceHolder.Callback; 
    import android.view.SurfaceView; 
     
    /**
     * 
     * @author Himi
     * 
     */ 
    public class MySurfaceView extends SurfaceView implements Callback, Runnable { 
        public static MySurfaceView msrv; 
        private int move_x = 2, x = 20; 
        private Thread th; 
        private SurfaceHolder sfh; 
        private Canvas canvas; 
        private Paint p; 
     
        public MySurfaceView(Context context) { 
            super(context); 
            msrv = this; 
            p = new Paint(); 
            p.setAntiAlias(true); 
            sfh = this.getHolder(); 
            sfh.addCallback(this); 
            th = new Thread(this); 
            this.setKeepScreenOn(true); 
            this.setFocusable(false); 
            this.setFocusableInTouchMode(true); 
        } 
     
        int i = 0; 
        boolean result = false; 
        @Override 
        public boolean onTouchEvent(MotionEvent event) { 
            int key = event.getAction(); 
            switch (key) { 
                case MotionEvent.ACTION_UP: 
                    result = false; 
                    break; 
         
                case MotionEvent.ACTION_DOWN: 
                    result = true; 
                    break; 
                case MotionEvent.ACTION_CANCEL: 
                    result = true; 
                    break; 
         
                case MotionEvent.ACTION_MOVE: 
                     
                    if (i > 0) { 
                        result = false; 
                    } 
                    i++; 
                    break; 
                default : 
                    result = true; 
                    break; 
            } 
            System.out.println("surface  touch: " + result); 
            return result; 
        } 
         
        @Override 
        public boolean onKeyDown(int keyCode, KeyEvent event) { 
            boolean result = super.onKeyDown(keyCode, event); 
            System.out.println("surface  key  : " + result); 
            return true; 
        } 
     
        public void surfaceCreated(SurfaceHolder holder) { 
            th.start(); 
        } 
     
        public void draw() { 
            canvas = sfh.lockCanvas(); 
            if (canvas != null) { 
                canvas.drawColor(Color.WHITE); 
                canvas.drawText("Surfaceview", x + move_x, 280, p); 
                sfh.unlockCanvasAndPost(canvas); 
            } 
        } 
     
        private void logic() { 
            x += move_x; 
            if (x > 200 || x < 80) { 
                move_x = -move_x; 
            } 
        } 
     
        public void run() { 
            // TODO Auto-generated method stub 
            while (true) { 
                draw(); 
                logic(); 
                try { 
                    Thread.sleep(100); 
                } catch (Exception ex) { 
                } 
            } 
        } 
     
        public void surfaceChanged(SurfaceHolder holder, int format, int width, 
                int height) { 
        } 
     
        public void surfaceDestroyed(SurfaceHolder holder) { 
        } 
     
    } 


测试结果:

点击


touchevent-1


touchevent-2


touchevent-3


touchevent-4

MysurfaceView


false - down - 1


备注1







mainActivity


down - 2


up -3







点击













MysurfaceView


true - down - 1


false - up - 2







mainActivity





up - 3







点击













MysurfaceView


true - down - 1


true - up - 2







mainActivity



























滑动













MysurfaceView


false - down - 1










mainActivity


down - 2


move - 3


move - 4


up - 5

滑动













MysurfaceView


true - down - 1


false - move - 2


true - move - 4


false - up - 5

mainActivity





move - 3





up - 6

滑动





















猜你喜欢

转载自myhearsnow.iteye.com/blog/1607026