3

I searched all over Internet for this with no success. I need to Animate an ImageView along a Path. I have created a Path using:

Path path = new Path()    
path.moveTo(0, 0);
path.lineTo(184,776);
path.lineTo(184,780);
path.lineTo(184,790);
path.lineTo(184,810);
path.lineTo(230, 900);

ImageView img = new ImageView(this);

Is there any way I can move img along path? Thank you all

pochimen
  • 287
  • 3
  • 15

1 Answers1

2

Here are the animators I use:

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

v21+:

ValueAnimator pathAnimator = ObjectAnimator.ofFloat(view, "x", "y", path)

v11+:

ValueAnimator pathAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);

pathAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
float[] point = new float[2];

@Override
    public void onAnimationUpdate(ValueAnimator animation) {
        float val = animation.getAnimatedFraction();
        PathMeasure pathMeasure = new PathMeasure(path, true);
        pathMeasure.getPosTan(pathMeasure.getLength() * val, point, null);
        view.setX(point[0]);
        view.setY(point[1]);
    }
});

Same as: Android, move bitmap along a path?

Community
  • 1
  • 1
Dileep P G
  • 571
  • 5
  • 13