旋转动画会中断其他动画

时间:2019-04-11 16:58:54

标签: java android

当我向ballImageView中添加旋转动画时,手势检测器开始错误地定义了velocityX和velocityY。球仅开始向下移动。 如果删除它,则移动将开始正常工作,并且球将向滑动方向移动。我试图取消动画,但没有任何效果。理想情况下,用户单击“开始”按钮,然后球旋转并且用户滑动以箍住。

public class MainActivity extends AppCompatActivity {

private static final String TAG = MainActivity.class.getSimpleName();
private final int MAX_VELOCITY = 600;

private ImageView ballImageView;
private ImageView playerImageView;
private ImageView hoopImageView;

private Button startButton;

FlingAnimation flingAnimationX;
FlingAnimation flingAnimationY;

FlingAnimation rotate;

VelocityTracker velocityTracker;

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

    ballImageView = (ImageView) findViewById(R.id.activity_main_ball);
    playerImageView = (ImageView)       findViewById(R.id.activity_main_player);
    hoopImageView = (ImageView) findViewById(R.id.activity_main_hoop);

    startButton = (Button) findViewById(R.id.activity_main_button_start);

    settingUpAnimation();

    rotate = new FlingAnimation(ballImageView, DynamicAnimation.ROTATION).setFriction(0.5f).setStartVelocity(2000f);


    startButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startButton.setVisibility(View.GONE);
            rotate.start();
            rotate.addEndListener(new DynamicAnimation.OnAnimationEndListener() {
                @Override
                public void onAnimationEnd(DynamicAnimation dynamicAnimation, boolean b, float v, float v1) {
                    rotate.cancel();
                }
            });
        }
    });
}



@Override
protected void onDestroy() {
    super.onDestroy();
    if(null != flingAnimationX) flingAnimationX.cancel();
    if(null != flingAnimationY) flingAnimationY.cancel();
    if(null != velocityTracker) velocityTracker.recycle();
}

@Override
protected void onStop() {
    super.onStop();
    if(null != flingAnimationX) flingAnimationX.cancel();
    if(null != flingAnimationY) flingAnimationY.cancel();
}


private void settingUpAnimation() {
    Display display = getWindowManager().getDefaultDisplay();
    final Point size = new Point();
    display.getSize(size);

    flingAnimationX = new FlingAnimation(ballImageView, DynamicAnimation.X).setFriction(4.0f);
    flingAnimationX.setMinValue(0f).setMaxValue((float) (size.x - ballImageView.getWidth()));
    flingAnimationY = new FlingAnimation(ballImageView, DynamicAnimation.Y).setFriction(4.0f);
    flingAnimationY.setMinValue(0f).setMaxValue((float) (size.y - ballImageView.getHeight()));


    flingAnimationX.addEndListener(new DynamicAnimation.OnAnimationEndListener() {
        @Override
        public void onAnimationEnd(DynamicAnimation dynamicAnimation, boolean b, float v, float v1) {
            if(checkGoal()) startButton.setVisibility(View.VISIBLE);
            flingAnimationY.cancel();
            ballImageView.setX(playerImageView.getX() + 0.5f * (playerImageView.getWidth() - ballImageView.getWidth()));
            ballImageView.setY(playerImageView.getY() - ballImageView.getHeight());
        }
    });

    final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.OnGestureListener() {
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public void onShowPress(MotionEvent e) {

        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return false;
        }

        @Override
        public void onLongPress(MotionEvent e) {

        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            float pixelPerSecond = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20.0f,
                    getResources().getDisplayMetrics());

            flingAnimationX.setStartVelocity(velocityX - pixelPerSecond);
            flingAnimationY.setStartVelocity(velocityY - pixelPerSecond);

            flingAnimationX.start();
            flingAnimationY.start();
            return true;
        }
    });

    ballImageView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            gestureDetector.onTouchEvent(event);
            return true;
        }
    });
}

private boolean checkGoal() {
    float ballX = ballImageView.getX() + ballImageView.getWidth() * 0.5f;
    float ballY = ballImageView.getY() + ballImageView.getHeight() * 0.5f;
    float hoopXL = hoopImageView.getX() + hoopImageView.getWidth() * 0.3f;
    float hoopXR = hoopImageView.getX() + hoopImageView.getWidth() * 0.6f;
    float hoopYT = hoopImageView.getY() + hoopImageView.getHeight() * 0.5f;
    float hoopYB = hoopImageView.getY() + hoopImageView.getHeight();
    return (ballX > hoopXR & ballX < hoopXL) & (ballY > hoopYT & ballY < hoopYB);
}

0 个答案:

没有答案