Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prepare curve translate animation for android?

There are 4 types of animations in android - rotate, alpha,scale and translate. I want to prepare curved translate animation.

Is it possible.?

like image 674
user884126 Avatar asked Dec 02 '11 09:12

user884126


People also ask

What is translate animation in Android?

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.

Is animation possible on Android?

On Android 5.0 (API level 21) and higher, you can also create animations that transition between your activities. This is based on the same transition framework described above to animate layout changes, but it allows you to create animations between layouts in separate activities.

What is animation translate?

Animation is the process of making films in which drawings appear to move. ...

What is Android TranslationZ?

TranslationZ is a dynamic property used for animation. Basically it's needed to nicely handle elevation changes.


2 Answers

What Android version do you use? Since API level 11 you can use custom Animators which can easily implement your curve translation.

If you use a version below that there is afaik only the possibility to manually concatenate multiple linear translations using the translate animation and setting animation listeners

EDIT:

Example:

View view;
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.
        view.setTranslationX((float)(200.0 * Math.sin(value*Math.PI)));
        view.setTranslationY((float)(200.0 * Math.cos(value*Math.PI)));
    }
});

I hope it works. Just copied & pasted (and shortened and changed) the code from one of my apps.

like image 73
js- Avatar answered Oct 01 '22 22:10

js-


Here are the animators I use:

Purpose: Move View "view" along Path "path"

Android v21+:

// Animates view changing x, y along path co-ordinates
ValueAnimator pathAnimator = ObjectAnimator.ofFloat(view, "x", "y", path)

Android v11+:

// Animates a float value from 0 to 1 
ValueAnimator pathAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);

// This listener onAnimationUpdate will be called during every step in the animation
// Gets called every millisecond in my observation  
pathAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

float[] point = new float[2];

@Override
    public void onAnimationUpdate(ValueAnimator animation) {
        // Gets the animated float fraction
        float val = animation.getAnimatedFraction();

        // Gets the point at the fractional path length  
        PathMeasure pathMeasure = new PathMeasure(path, true);
        pathMeasure.getPosTan(pathMeasure.getLength() * val, point, null);

        // Sets view location to the above point
        view.setX(point[0]);
        view.setY(point[1]);
    }
});

Similar to: Android, move bitmap along a path?

like image 35
Dileep P G Avatar answered Oct 01 '22 22:10

Dileep P G