android - 刷卡动画

时间:2016-11-21 10:34:39

标签: java android animation swipe

在我的新程序中,我需要可以滑到一边的物体。我已经有了我的动画,它正在工作,我试图检测另一个类中用户的滑动。我遇到的问题是我不知道如何连接它们。当正确识别滑动手势时,应该开始特定的动画。

我的AnimatedViewClass:

private Runnable r = new Runnable() {
        @Override
        public void run() {
            if(continueAnimation) {
            invalidate(); 
            }
        }
    };


    protected void onDraw(Canvas c) {  

                if (x<0) {
                    x = this.getWidth()/2-100;
                    y = this.getHeight()/2-100;
                } 

                    else {
                        x += xVelocity;

                            if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
                                boolean continueAnimation = false;
                            }
                    }

                c.drawBitmap(ball.getBitmap(), x, y, null);  

                if(continueAnimation) 
                {
                    h.postDelayed(r, FRAME_RATE);
                }   

                else {
                        x = this.getWidth()-ball.getBitmap().getWidth();
                }

            }

我的SwipeTouchListener:

    public class OnSwipeTouchListener implements OnTouchListener {

    private final GestureDetector gestureDetector;

    public OnSwipeTouchListener (Context ctx){
        gestureDetector = new GestureDetector(ctx, new GestureListener());
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    private final class GestureListener extends SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            onSwipeRight();
                        } else {
                            onSwipeLeft();
                        }
                    }
                    result = true;
                } 
                else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffY > 0) {
                            onSwipeBottom();
                        } else {
                            onSwipeTop();
                        }
                    }
                    result = true;

            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以像这样添加GestureDetector到您的视图类,并替换onFling()中的代码

public class AnimatedViewClass extends View {

GestureDetector gestureDetector;

public AnimatedViewClass(Context context) {
    super(context);
    gestureDetector = new GestureDetector(getContext(), new GestureDetectorListener());
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    gestureDetector.onTouchEvent(event);
    return super.onTouchEvent(event);
}

private void onSwipeRight(){
    // swipe right detected
    // do stuff
    invalidate();
}


private void onSwipeLeft(){
    // swipe left detected
    // do stuff
    invalidate();
}

private class GestureDetectorListener extends
        GestureDetector.SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

        // onSwipeRight()
        // onSwipeLeft()

        return super.onFling(e1, e2, velocityX, velocityY);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return super.onDown(e);
    }
}


private Runnable r = new Runnable() {
    @Override
    public void run() {
        if(continueAnimation) {
            invalidate();
        }
    }
};


protected void onDraw(Canvas c) {

    if (x < 0) {
        x = this.getWidth() / 2 - 100;
        y = this.getHeight() / 2 - 100;
    } else {
        x += xVelocity;

        if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
            boolean continueAnimation = false;
        }
    }

    c.drawBitmap(ball.getBitmap(), x, y, null);

    if (continueAnimation) {
        h.postDelayed(r, FRAME_RATE);
    } else {
        x = this.getWidth() - ball.getBitmap().getWidth();
    }

}


}