12

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

Is it possible.?

Julien Roncaglia
  • 17,397
  • 4
  • 57
  • 75
user884126
  • 185
  • 1
  • 3
  • 10

3 Answers3

10

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.

js-
  • 1,632
  • 1
  • 14
  • 18
4

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?

Community
  • 1
  • 1
Dileep P G
  • 571
  • 5
  • 13
  • 1
    It's helpful to spend more time explaining how your answer works, so the asker can follow your code easier. – SuperBiasedMan May 15 '15 at 08:51
  • 1
    @SuperBiasedMan Thanks for the feedback! Have added explanation in comments. – Dileep P G May 15 '15 at 10:06
  • I had to do: `pathMeasure.getLength()/2` because the view kept going back to it's original position. – Rick Jun 21 '16 at 14:47
  • 1
    The view probably kept going back to it's original position because you were passing true to PathMeasure(path, forceClose). This tells it to close the path, adding a line from the last point to the original position. – dephinera Dec 01 '16 at 15:43
-1

Consider the following web link. It is a game in C. You need to isolate the projectile() function and try to understand the variables defined within it. Once you get that try implementing it in your own code.

http://www.daniweb.com/software-development/c/code/216266

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Jay
  • 3
  • 5
  • 3
    While this may theoretically answer the question, it would be preferable (http://meta.stackexchange.com/q/8259) for you to edit the answer to include the essential parts of the solution, and provide the link for reference. – Peter O. Feb 10 '12 at 20:01