How to execute TranslateAnimation sequentially without knowing how many translate you'll have to do (android)?

StackOverflow https://stackoverflow.com/questions/23086713

  •  04-07-2023
  •  | 
  •  

سؤال

The question is simple but i tried to search a solution in other similar questions, and i couldn't. I have a arraylist path containing objects of type Coordinata that is an object containing two float X and Y, that is a coordinate. In substance, it's a list of coordinate. I need my ImageView image to move on a map on my view doing several subsequential TranslateTrasformation between the coordinate contained in the list path.

This can be easily done in the case of a fixed number of translation, doing the first translate and attaching it a setAnimationListener which could catch the end of the first animation and start in that moment the second and so on. I tried to iterate the process on my list path, using a while cycle, for i cannot really know how many coordinate will be in the path when the animation will start. The result is the animation go on for the first couple of coordinates and then jump executing the animation for the last two coordinate in the path, ignoring all that is among these.

What is going on? I'm messing up with objects?

List<Coordinata> path; // properly filled with coordinates
Coordinata coordTempStart;
Coordinata coordTempArrival;
ImageView image;

ListIterator<Coordinata> pathIterator = path.listIterator();
    if(pathIterator.hasNext()){
        coordTempStart = (Coordinata) pathIterator.next();
    }
    if(pathIterator.hasNext()){
        coordTempArrival = (Coordinata) pathIterator.next();
        animation = new TranslateAnimation(coordTempStart.getX(), 
                   coordTempArrival.getX(),
                   coordTempStart.getY(), 
                   coordTempArrival.getY()); 
        animation.setDuration(3000);   
        image.startAnimation(animation);
    } 

    while(pathIterator.hasNext()){
        coordTempStart=coordTempArrival;
        coordTempArrival = (Coordinata) pathIterator.next();
            animation.setAnimationListener(new TranslateAnimation.AnimationListener(){
            public void onAnimationStart(Animation arg0) {}    
            public void onAnimationRepeat(Animation arg0) {}
            public void onAnimationEnd(Animation arg0) {
                animation = new TranslateAnimation(coordTempStart.getX(), 
                           coordTempArrival.getX(),
                           coordTempStart.getY(), 
                           coordTempArrival.getY()); 
                animation.setDuration(3000); 
                image.startAnimation(animation);
            }
        }); 

    }
هل كانت مفيدة؟

المحلول

I suggest to use ObjectAnimator and AnimatorSet to animate views. Object animator is available only for api > 11 but you can use NineOldAndroid if you have to keep the compatibility.

your code will be something like this (in pseudocode):

...
AnimatorSet animatorSet = new AnimatorSet();
while(pathIterator.hasNext()){
...
//Set _your_delta_x_ and _your_delta_y_ from iterator
...
    ObjectAnimator xTrasnlationObjectAnimator = ObjectAnimator.ofFloat(v, view_x, _your_delta_x_);
    ObjectAnimator yTrasnlationObjectAnimator = ObjectAnimator.ofFloat(v, view_y, _your_delta_y_);
    animatorSet.playSequentially(xTrasnlationObjectAnimator, yTrasnlationObjectAnimator);
}
...
animatorSet.start()

to set right parameters to object animator refer the official documentation http://developer.android.com/reference/android/animation/ObjectAnimator.html

نصائح أخرى

Just for the sake of completeness, i publish the actual code used to solve the problem:

// in this list you can insert as many animations you want
List<Animator> animations = new ArrayList<Animator>();

AnimatorSet animatorSet = new AnimatorSet();
ObjectAnimator xTrasnlationObjectAnimator = ObjectAnimator.ofFloat(image, View.TRANSLATION_X, (float)300);
xTrasnlationObjectAnimator.setRepeatCount(0);
ObjectAnimator yTrasnlationObjectAnimator = ObjectAnimator.ofFloat(image, View.TRANSLATION_Y, (float)300);
yTrasnlationObjectAnimator.setRepeatCount(0);
ObjectAnimator x2TrasnlationObjectAnimator = ObjectAnimator.ofFloat(image, View.TRANSLATION_X, (float)400);
x2TrasnlationObjectAnimator.setRepeatCount(0);

animations.add(xTrasnlationObjectAnimator);
animations.add(yTrasnlationObjectAnimator);
animations.add(x2TrasnlationObjectAnimator);

animatorSet.playSequentially(animations);

animatorSet.start();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top