Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create move/resize animations in Android?

Does someone know about Android animations? I want to create something like following:

  • I have a big image in the center of my device screen;
  • this image becomes small (by animation) and goes to the corner of my device screen;

Its something like in this bellow sequence:

enter image description here

Any hints would be very appreciated! Thanks in advance!

like image 543
Marcelo Avatar asked Sep 10 '12 17:09

Marcelo


People also ask

What is twined animation in Android?

Tween animations are a specific type of animation related to rotation, sliding, and movement of an object.


2 Answers

Use ViewPropertyAnimator, with methods like scaleXBy() and translateYBy(). You get a ViewPropertyAnimator by calling animate() on a View, on API Level 11+. If you are supporting older devices, NineOldAndroids offers a near-workalike backport.

You might also wish to read:

  • http://android-developers.blogspot.com/2011/05/introducing-viewpropertyanimator.html
  • http://developer.android.com/guide/topics/graphics/prop-animation.html
like image 145
CommonsWare Avatar answered Oct 23 '22 01:10

CommonsWare


I have a class with the simultaneous rotation and movement. It's costly but it works on all API versions.

public class ResizeMoveAnimation extends Animation {
    View view; 
    int fromLeft; 
    int fromTop; 
    int fromRight;
    int fromBottom;
    int toLeft; 
    int toTop; 
    int toRight;
    int toBottom;

    public ResizeMoveAnimation(View v, int toLeft, int toTop, int toRight, int toBottom) {
        this.view = v;
        this.toLeft = toLeft;
        this.toTop = toTop;
        this.toRight = toRight;
        this.toBottom = toBottom;

        fromLeft = v.getLeft();
        fromTop = v.getTop();
        fromRight = v.getRight();
        fromBottom = v.getBottom();

        setDuration(500);
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {

        float left = fromLeft + (toLeft - fromLeft) * interpolatedTime;
        float top = fromTop + (toTop - fromTop) * interpolatedTime;
        float right = fromRight + (toRight - fromRight) * interpolatedTime;
        float bottom = fromBottom + (toBottom - fromBottom) * interpolatedTime;

        RelativeLayout.LayoutParams p = (LayoutParams) view.getLayoutParams();
        p.leftMargin = (int) left;
        p.topMargin = (int) top;
        p.width = (int) ((right - left) + 1);
        p.height = (int) ((bottom - top) + 1);

        view.requestLayout();
    }
}
like image 7
Adorjan Princz Avatar answered Oct 22 '22 23:10

Adorjan Princz