How to prepare curve translate animation for android?

后端 未结 3 839
半阙折子戏
半阙折子戏 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?

提交回复
热议问题