欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【达内课程】手势滑动

程序员文章站 2022-03-16 16:41:59
...
public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    }

    private float downX;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                Log.d("Motion","ACTION_DOWN->"+event.getX()+","+event.getY());
                downX = event.getX();
                break;
            case MotionEvent.ACTION_UP:
                Log.d("Motion","ACTION_UP->"+event.getX()+","+event.getY());
                if(event.getX()-downX>60){
                    Log.d("Motion","从左至右滑动");
                }
                if(downX - event.getX()>60){
                    Log.d("Motion","从右至左滑动");
                }
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d("Motion","ACTION_MOVE->"+event.getX()+","+event.getY());
                break;
        }
        return false;
    }
}

观察日志

ACTION_DOWN->180.15381,301.11328
ACTION_MOVE->212.78,313.125
ACTION_MOVE->231.20656,313.125
......
ACTION_UP->445.00327,313.125
    从左至右滑动
相关标签: 手势滑动