如何设置ImageView的动画以移出屏幕并从另一个屏幕返回

时间:2019-05-03 22:50:49

标签: java android animation

我正在尝试制作一个应用程序,该应用程序可以让您推送图像,并且该图像从负侧重新出现(例如您将其向左推送-它将从右侧返回,反之亦然)。 我设法使您将其向右外侧推,但我无法将其x值设置为-120并开始播放新动画,从而将ImageView推回中心。

我的方法错误吗?也许有更好的方法可以做到这一点?

代码如下:

//on "onCreat":
imageView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent event) {
    switch (event.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
            xCoOrdinate = view.getX() - event.getRawX();
            break;
        case MotionEvent.ACTION_UP:
            nextPreviousNothing(view.getX(), view); //this is what makes the ImageView animate
            break;
        case MotionEvent.ACTION_MOVE:
            view.animate().x(event.getRawX() + xCoOrdinate).setDuration(0).start();
            break;
        default:
            return false;
        }
    return true;
    }
});

//this function is resposable to animate the imageView
void nextPreviousNothing(float x, View view){
    ObjectAnimator animatorX;
    final ObjectAnimator animatorX2;
    final ObjectAnimator animatorX3;
    if (x > 350) {
        //if the ImageView passed a sertin point on the right side (from the center
        animatorX = ObjectAnimator.ofFloat(view, "x", 1200f);
        animatorX2 = ObjectAnimator.ofFloat(view, "x", -300);
        animatorX3 = ObjectAnimator.ofFloat(view, "x", 120);
        animatorX.setDuration(300);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(animatorX);
        animatorSet.start();
        //Originaly here was an AnimationListener, and on the "onAnimationEnd void I tryed to call "view.setX(-300), but it didn't work at all.
    } 
    else if (x < - 100) {
        //if the ImageView passed a sertin point on the left side (from the center
    } 
    else {

    }
}

0 个答案:

没有答案