Action_up函数不适用于所有设备

时间:2019-02-06 09:12:25

标签: android touchscreen

private View.OnTouchListener onTouchListener() {
    return new View.OnTouchListener() {
        int lastAction;

        DisplayMetrics displaymetrics = getResources().getDisplayMetrics();
        int screenHight = displaymetrics.heightPixels;
        int screenWidth = displaymetrics.widthPixels;

        @SuppressLint("NewApi") @Override
        public boolean onTouch(View view, MotionEvent event) {

                view.setAlpha(1);
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:

                    dX = view.getX() - event.getRawX();
                    dY = view.getY() - event.getRawY();
                    transX=view.getX();
                    transY=view.getY();
                    lastAction = MotionEvent.ACTION_DOWN;

                  return true;


                  case MotionEvent.ACTION_MOVE:

                        newX = event.getRawX() + dX;
                        newY = event.getRawY() + dY;

                        // check if the view out of screen
                        if ((newX <= 0 || newX >= screenWidth - view.getWidth()) || (newY <= 0 || newY >= screenHight - view.getHeight())) {
                            lastAction = MotionEvent.ACTION_MOVE;
                            break;
                        }

                        view.setX(newX);
                        view.setY(newY);

                        if (newX >= 0 && newX <= (screenWidth / 2)) {
                            view.animate().translationX(-screenWidth + 200).setDuration(100);
                            view.setY(newY);
                            transX = 0;
                            transY = newY;
                            side = 0;
                        } else if (newX >= (screenWidth / 2) && newX <= screenWidth - view.getWidth()) {
                            //  view.setX(screenWidth - view.getWidth());
                            view.animate().translationX(0).setDuration(100);
                            view.setY(newY);
                            transX = screenWidth - view.getWidth();
                            transY = newY;
                            side = 1;
                        }
                        if(newY>=0 && newY<=200)
                        {
                            view.animate().translationY(-screenHight+700).setDuration(100);
                            view.setX(newX);
                        }
                        else if(newY>=screenHight-300 && newY<=screenHight-view.getHeight())
                        {
                            view.animate().translationY(0).setDuration(100);
                            view.setX(newX);
                        }


                    lastAction = MotionEvent.ACTION_MOVE;
                        break;

                case MotionEvent.ACTION_UP:
                    if (lastAction == MotionEvent.ACTION_DOWN) {
                        cvSpinner.setVisibility(View.VISIBLE);
                        spinner(view);

                    }

                    break;



                default:
                    return false;
            }
            return true;
        }
    };
}

0 个答案:

没有答案