Search code examples
androidandroid-layoutanimationandroid-animationandroid-view

Android: How to animation move the View by using a matrix?


How to move the View with the animation from the current position (translate, rotate & scale) to a new state of the matrix?

Current values (...) ==> animate ==> New values (scale: 1, transX: 20, transY: 700, angle: 0)

I use the following code, the move occurs without animation.

Matrix matrix = new Matrix();
matrix.setTranslate(20, 700);

MyAnimation animation = new MyAnimation(matrix);
animation.setDuration(1000);
animation.setFillAfter(true);
text_object.setAnimation(animation);

MyAnimation.java

public class MyAnimation extends Animation {
    private Matrix matrix;

    public MyAnimation(Matrix matrix) {
        this.matrix = matrix;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);
        t.getMatrix().set(matrix);
    }
}

How to make the animation work? What alternatives can be used to achieve this objective?


Solution

  • Why go all the trouble to modify the Matrix of the View to animate it's scale and traslation. Just use Property Animation. You can read more about it here: http://developer.android.com/guide/topics/graphics/prop-animation.html

    You can use an ObjectAnimator to perform the animation you require.