Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate a 'cloned', duplicate version of existing android View

I have an ImageView that sits in a page of a ViewPager and suppose I'd like to animate it to scale down + fly somewhere off-screen, while the original view is still present in pager, untouched.

What is the best way to create this type of animation on Android 4.0.3+ ? I would like to use the new animation framework, not the old one.

One way I figured is to use the activity's DecorView: create a clone of the ImageView there and do an animation, but I feel like this is a bit hacky - the decor view seems more like a hidden feature and an implementation detail.

Is there a better way maybe?

like image 650
dimsuz Avatar asked Aug 29 '14 13:08

dimsuz


People also ask

What is AnimatorSet Android?

android.animation.AnimatorSet. This class plays a set of Animator objects in the specified order. Animations can be set up to play together, in sequence, or after a specified delay.

What is fillAfter in Android?

android:fillAfterWhen set to true, the animation transformation is applied after the animation is over. The default value is false. If fillEnabled is not set to true and the animation is not set on a View, fillAfter is assumed to be true. May be a boolean value, such as " true " or " false ".

Can we animate in Android?

Animate between activitiesOn Android 5.0 (API level 21) and higher, you can also create animations that transition between your activities. This is based on the same transition framework described above to animate layout changes, but it allows you to create animations between layouts in separate activities.


1 Answers

I think you can get around using a DecorView by setting setClipChildren() to false on all the parents of the ImageView. This allows you to animate a View from your layout even outside of its parent (and even Activity). I mostly use a helper method to do this:

public static void setClipView(View view, boolean clip) {
    if (view != null) {
        ViewParent parent = view.getParent();
        if(parent instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view.getParent();
            viewGroup.setClipChildren(clip);
            setClipView(viewGroup, clip);
        }
    }
}

So just use this method on your ImageView like this:

LayoutHelper.setClipView(imageView, false);

To animate the View itself you should use the new animation API introduced with Android 3.0 (Honeycomb - API level 11).

This DevBytes video is also somewhat related to this. It deals with custom Activity animations and they create a duplicate ImageView to animate the transition from one Activity to the next. That is basically the same thing what you are trying to do, just in two different Activities.

like image 150
Xaver Kapeller Avatar answered Oct 06 '22 00:10

Xaver Kapeller