android滑动值的计算

来源:互联网 发布:ubuntu 安装 kde桌面 编辑:程序博客网 时间:2024/06/13 06:07

重写Activity的onTouchEvent方法:

    float x_temp01 = 0.0f;
    float y_temp01 = 0.0f;
    float x_temp02 = 0.0f;
    float y_temp02 = 0.0f; 

    @Override  
    public boolean onTouchEvent(MotionEvent event)
    {
        //获得当前坐标
            float x = event.getX();
            float y = event.getY();
            
            switch(event.getAction())
            {
                    case MotionEvent.ACTION_DOWN: 
                    {
                        x_temp01 = x;
                        y_temp01 = y;
                    }
                    break;
                    case MotionEvent.ACTION_UP: 
                    {
                        x_temp02 = x;
                        y_temp02 = y;
                        
                        if(x_temp01!=0 && y_temp01!=0)//
                        {
                                // 比较x_temp01和x_temp02
                                // x_temp01>x_temp02         //向左
                                // x_temp01==x_temp02         //竖直方向或者没动
                                // x_temp01<x_temp02         //向右
                                
                                if(x_temp01>x_temp02)//向左
                                {
                                        //移动了x_temp01-x_temp02
                                }
                                
                                if(x_temp01<x_temp02)//向右
                                {
                                        //移动了x_temp02-x_temp01
                                }
                        }
                    }
                    break;
                    case MotionEvent.ACTION_MOVE:
                    {
                            
                    }
                    break;

            }
            return super.onTouchEvent(event);
    }

原创粉丝点击