Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translate (curve path) + Scale animation Android

Tags:

android

enter image description here

I want to translate a imageview in a curve while scaling it as it translates.

Ive gone through Multiple posts like https://stackoverflow.com/a/30254927/3518278 where i cant figure out how to compute my path variable

also its mentioned here that android 5 provides basic curves in interpolars https://stackoverflow.com/a/26591870/3518278 but they seem to have no effect.

My current code is

View view;
        ValueAnimator animator = ValueAnimator.ofFloat(0, 1); // values from 0 to 1
        animator.setDuration(5000); // 5 seconds duration from 0 to 1
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
        {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = ((Float) (animation.getAnimatedValue()))
                        .floatValue();
                // Set translation of your view here. Position can be calculated
                // out of value. This code should move the view in a half circle.
                img_fullscreen_drone.setTranslationX((float)(150.0 * Math.sin(value*Math.PI)));
                img_fullscreen_drone.setTranslationY((float)(150.0 * Math.cos(value*Math.PI)));
                img_fullscreen_drone.setScaleType(ImageView.ScaleType.CENTER);
                img_fullscreen_drone.animate().scaleX(0.5f).scaleY(0.5f).setDuration(5000).start();

            }
        });

        animator.start();

this translates my view in a arc but after translation is complete scale starts,i want scale and translate along curve to happen toghether.

Any help will be appreicated

Thanks in Advance

like image 449
user2229100 Avatar asked May 11 '16 20:05

user2229100


People also ask

What is translate in Android animation?

Translate Animation can change the visual appearance of an object, but they cannot change the objects themselves. That is, if you apply a translate animation to a view, it would move to a new position, but its click events would not get fired; the click events would still get fired at its previous position.

What is Alpha animation in Android?

An Alpha Animation is animation that controls the alpha level of an object, i.e. fading it in and out.


1 Answers

Your original code was restarting the scale animation every frame of the animation that translated the image. You just need to move the scale animation code out of the update block, as below.

Note that your using two slightly different methods of animation, for scale vs. translate, which is perhaps what confused you.

    View view;
    ValueAnimator animator = ValueAnimator.ofFloat(0, 1); // values from 0 to 1
    animator.setDuration(5000); // 5 seconds duration from 0 to 1
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
    {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            float value = ((Float) (animation.getAnimatedValue()))
                    .floatValue();
            // Set translation of your view here. Position can be calculated
            // out of value. This code should move the view in a half circle.
            img_fullscreen_drone.setTranslationX((float)(150.0 * Math.sin(value*Math.PI)));
            img_fullscreen_drone.setTranslationY((float)(150.0 * Math.cos(value*Math.PI)));
        }
    });

    animator.start();

    img_fullscreen_drone.setScaleType(ImageView.ScaleType.CENTER);
    img_fullscreen_drone.animate().scaleX(0.5f).scaleY(0.5f).setDuration(5000).start();
like image 147
Joel Duggan Avatar answered Oct 06 '22 20:10

Joel Duggan