简体   繁体   中英

Android slide down animation

I have a toolbar aligned at the bottom and a WebView above it, which fill remaining height. Now I want to slide down the toolbar in order to hide it, and while it's animating the webview height should expand. I've tried using TranslateAnimation but unfortunetely it's not adjusting toolbar's real position, only it's contents are moved. Here's what I tried:

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0%"
    android:toXDelta="0%"
    android:fromYDelta="0%"
    android:toYDelta="100%"
    android:duration="700"
    android:fillAfter="true" />

And the actual code:

final View v = findViewById(R.id.browseToolbar);
Animation animation = AnimationUtils.loadAnimation(someContext, R.anim.toolbar_hide);
v.startAnimation(animation);

How can I do it?

I wrote some code to do actual resize animations. Take a look:

http://www.touchlab.co/blog/resize-animation/

Also look at a presentation I did on Android animation

https://docs.google.com/present/view?id=djqv5kb_187c62jvbf7

Essentially, as mentioned above, the regular animation classes only affect what's in their parent view. The key to understanding them is they aren't "real". Think of the Android animation as a mirage. If you run the example app I created, say you do a scale animation and make a button smaller, if you click outside, where the button USED to be, it still registers as a button click. Android animations don't actually affect real boundaries and dimensions.

What the code in my blog post does, essentially, is implement a ViewGroup.OnHierarchyChangeListener. When stuff is added/removed from the hierarchy, the container animates a physical resize.

When you use android animation only the pixels of the view are shifted. To actually move the view, you will need to add an animation listener to the animation object and override the onAnimationEnd method. In that method you need to programatically move the view.

 AnimationListener animListener = new AnimationListener() {
     @Override
     public void onAnimationStart(Animation arg0) {}            
     @Override
     public void onAnimationRepeat(Animation arg0) {}                   
     @Override
     public void onAnimationEnd(Animation arg0) {
        Move the view here          
     }
 };

 animation.setAnimationListener(animListener);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM