【达内课程】手势滑动

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010356768/article/details/80691863
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
    从左至右滑动
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页