Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value Animator android

I am Trying to animate 3 images one after the other using value animator but i am not able to decide how to call the three handlers after regular intervals..Suppose there are 3 images and i m making three handlers for animating them.But i am able to bring the three images at a time but not one after the other at regular intervals.Please Help

This is my UpdateListener which i am calling from my handler

public void startAnimation_image(final ImageView aniView) {

    animator = ValueAnimator.ofFloat(0, .8f);
    animator.setDuration(Constants.ANIM_DURATION);

    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

        int Low = 10;
        int High = width-150;
        int R = (int) ((Math.random() * (High - Low)) + Low);

        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            float value = ((Float) (animation.getAnimatedValue())).floatValue();
            aniView.setTranslationX(R);

             Log.e("mscale",150*mScale +"");
             Log.e("value is", value+"");

            aniView.setTranslationY((mDisplaySize.bottom + (150*mScale))*value);

            x_point = aniView.getTranslationX();
            y_point = aniView.getTranslationY();
        }
    });

    animator.addListener(new AnimatorListener() {

        @Override
        public void onAnimationStart(Animator arg0) {

        }
        @Override
        public void onAnimationRepeat(Animator arg0) {

        }
        @Override
        public void onAnimationEnd(Animator arg0) {

            startAnimation();
        }
        @Override
        public void onAnimationCancel(Animator arg0) {

        }
    });

    animator.start();
}

This is one of my handler

@Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);

        //int viewId = new Random().nextInt(STARS.length);

         id =   getApplicationContext().getResources().getIdentifier(STARS[0], "drawable", 
          getApplicationContext().getPackageName());

         inflate = LayoutInflater.from(StarsActivity2.this);


        img[0].setVisibility(ImageView.VISIBLE);
        img[0].setImageResource(id);

        mAllImageViews.add(img[0]);         

        LayoutParams animationLayout = (LayoutParams) img[0].getLayoutParams();
        img[0].setLayoutParams(animationLayout);
        Log.e("mHandler",img[0]+ "");
        startAnimation_image(img[0]);           
    }
};

There are similaarly three handlers and three update listeners.. Please help...

like image 457
XylemRaj Avatar asked Nov 28 '13 13:11

XylemRaj


People also ask

What is Android ValueAnimator?

This class provides a simple timing engine for running animations which calculate animated values and set them on target objects. There is a single timing pulse that all animations use. It runs in a custom handler to ensure that property changes happen on the UI thread.

Is animation possible on Android?

On Android 4.4 (API level 19) and higher, you can use the transition framework to create animations when you swap the layout within the current activity or fragment. All you need to do is specify the starting and ending layout, and what type of animation you want to use.

How do I set up animator on Android?

You have to create a new folder called anim under res directory and make an xml file under anim folder. This method starts the animation. This method sets the duration of an animation.

What is object animator in Android?

ObjectAnimator is a Subclass of ValueAnimator, which allows us to set a target object and object property to animate. It is an easy way to change the properties of a view with a specified duration. We can provide the end position and duration of the animation.


1 Answers

You can delay an animation by offset ms by calling

animation.setStartOffset(offset);

So for three images with duration ANIM_DURATION, you can use the following values to start them sequentially (probably by passing them as a parameter to startAnimation_image())

// note: this is for illustrative purposes. You should put this in a loop
int firstOffset = 0 * ANIM_DURATION; // starts immediately
int secondOffset = 1 * ANIM_DURATION; // starts after the first animation is finished
int thirdOffset = 2 * ANIM_DURATION; // starts after the second animation is finished
// ... and so on.
like image 110
Nachi Avatar answered Oct 07 '22 13:10

Nachi