How to prepare curve translate animation for android?

后端 未结 3 805
半阙折子戏
半阙折子戏 2021-01-04 09:46

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

Is it possible.?

相关标签:
3条回答
  • 2021-01-04 10:24

    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?

    0 讨论(0)
  • 2021-01-04 10:37

    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.

    0 讨论(0)
  • 2021-01-04 10:43

    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

    0 讨论(0)
提交回复
热议问题