How to prepare curve translate animation for android?

后端 未结 3 826
半阙折子戏
半阙折子戏 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: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.

提交回复
热议问题