java – Simultaneous animation of multiple Views

Question:

Each time you click on the screen, a new View appears, each View must constantly move to the left, now when I add a new View , the animation for the previous View stops. How to make all animations work at the same time?

 private class MyGestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onDown(MotionEvent e) {
            switch (e.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    int x = (int) e.getX();
                    int y = (int) e.getY();

                    draw = new DrawView(getActivity(), 1, String.valueOf(xcoordList.size()));
                    drawViewList.add(draw);
                    progressBar.addView(draw);
                    progressBar.invalidate();

                    drawViewList.get(drawViewList.size()-1).animate().translationXBy(-360f).setDuration(1000);

                    Runnable runnable = new Runnable() {
                        @Override
                        public void run() {
                            CountDownTimer timer = new CountDownTimer(System.currentTimeMillis() + 2000 - timeList.get(0), 1000) {
                                int j = 3;
                                @Override
                                public void onTick(long millisUntilFinished) {
                                    final DrawView drawView = drawViewList.get(drawViewList.size()-1);

                            drawView.animate().translationXBy((-720f)/j).setDuration(1000);
                                    j++;

                                @Override
                                public void onFinish() {

                                }
                            }.start();
                        }
                    };

                    getActivity().runOnUiThread(runnable);
} 

Answer:

The solution I applied replaced the given line:

drawView.animate().translationXBy((-720f)/j).setDuration(1000);

on:

 drawView.animate().translationXBy((-720f)/j)
                                        .setDuration(1000).setListener(new Animator.AnimatorListener() {
                                    @Override
                                    public void onAnimationStart(Animator animation) {
                                        j++;
                                    }

                                    @Override
                                    public void onAnimationEnd(Animator animation) {
                                        drawView.animate().translationXBy((-720f) / j).setDuration(1000);
                                    }

                                    @Override
                                    public void onAnimationCancel(Animator animation) {

                                    }

                                    @Override
                                    public void onAnimationRepeat(Animator animation) {

                                    }
                                });
Scroll to Top