jle
jle

Reputation: 696

Android - withdraw an animation

I want my random chosen image to reset after it has been animated, so another randomly choosen image is at the origin point and the old one is deleted. How do I get back to the part where the image is set from the animation?

Random rand = new Random();
                    int imag = rand.nextInt(4) + 1;

                    switch (imag)
                        {
                        case 1:
                            imageView.setImageResource(R.drawable.a);

                            break;

                        case 2:
                            imageView.setImageResource(R.drawable.b);

                            break;

                        case 3:
                            imageView.setImageResource(R.drawable.c);

                            break;

                        case 4:
                            imageView.setImageResource(R.drawable.d);

                            break;
                            }                   

        }        


        public boolean onTouchEvent(MotionEvent touchevent)
        {

            final ViewPropertyAnimator animator = imageView.animate();
            switch (touchevent.getAction())
            {

                case MotionEvent.ACTION_DOWN:
                {
                    x1 = touchevent.getX();
                    y1 = touchevent.getY();
                    break;
                }
                case MotionEvent.ACTION_UP:
                {
                    x2 = touchevent.getX();
                    y2 = touchevent.getY();



                    if (x1 < x2 && (x2-x1)>=(y1-y2) && (x2-x1)>=(y2-y1))
                    {

                        animator.translationX(imageView.getWidth()*2) 
                                .setDuration(180) //it's optional                               
                                .setListener(new AnimatorListenerAdapter() {
                                    @Override
                                    public void onAnimationEnd(Animator animation) {
                                        super.onAnimationEnd(animation);                                             
                                    }
                                })

                                .start();
                    }
                   } 

Upvotes: 0

Views: 54

Answers (1)

Doron Yakovlev Golani
Doron Yakovlev Golani

Reputation: 5490

Maybe you can try something like this:

int getMyRandomResId() {
    Random rand = new Random();
    int imag = rand.nextInt(4);

    switch (imag) {
        case 0:
            return R.drawable.a;
        break;
        case 1:
            return R.drawable.b;
        break;
        case 2:
            return R.drawable.c;
        break;
        default:
            return R.drawable.d;
    }
}

private boolean animationRunning = false;

public boolean onTouchEvent(MotionEvent touchevent)
{
    final ViewPropertyAnimator animator = imageView.animate();
    switch (touchevent.getAction()) {

        case MotionEvent.ACTION_DOWN: {
            x1 = touchevent.getX();
            y1 = touchevent.getY();
            break;
        }
        case MotionEvent.ACTION_UP: {
            x2 = touchevent.getX();
            y2 = touchevent.getY();

            if (!animationRunning) {
                if (x1 > x2 && (x1 - x2) >= (y1 - y2) && (x1 - x2) >= (y2 - y1)) {
                    animationRunning = true;
                    animator.translationX(-imageView.getWidth() * 2)
                            .setDuration(180)
                            .setListener(new AnimatorListenerAdapter() {
                                @Override
                                public void onAnimationEnd(Animator animation) {
                                    imageView.setTranslationX(0);
                                    imageView.setImageResource(getMyRandomResId());
                                    animationRunning = false;
                                }
                            })
                            .start();
                }
            }
        }
    }
}

Upvotes: 2

Related Questions