Enrichman
Enrichman

Reputation: 11337

How to "collapse" a view's height with an animation?

I'm trying to make disappearing a simple View sliding behind another one, I've tried like any kind of animation but none of them succeded..

This is what I've tried by far:

public void slideToTop(View view){
        TranslateAnimation animate = new TranslateAnimation(0,0,0,-view.getHeight());
        animate.setDuration(1000);
        view.startAnimation(animate);
        view.setVisibility(View.GONE);
    }

with and without a ReverseInterpolator

public class ReverseInterpolator implements Interpolator {
        public float getInterpolation(float paramFloat) {
            return Math.abs(paramFloat -1f);
        }
    }

or with Scaling

public void scaling(View view) {
        ScaleAnimation animation = new ScaleAnimation(0,0,1,1.5f);
        animation.setDuration(1000);
        animation.setFillAfter(true);
        view.startAnimation(animation);
    }

or with the new api

editView.animate().translationY(-editView.getHeight()).withLayer();

Any help on how to achieve this?

My layout is something like:

<ScrollView>
  <LinearLayout>
    <LinearLayout>
      <TextView/>
      <View/> <!-- This should "disappear" sliding up behind the other one -->
    </LinearLayout>
    <!-- more of the previous -->
    <LinearLayout>
      <TextView/>
      <View/>
    </LinearLayout>
  </LinearLayout>
</ScrollView>

Any help? Thanks!

Upvotes: 0

Views: 1004

Answers (1)

Patrick
Patrick

Reputation: 35224

If you want the effect that View B disappears behind view A you need to draw B before A:

<RelativeLayout>
   <ViewB />
   <ViewA above="ViewB"/>
</RelativeLayout>

and then move ViewB around. I have done the same with the following code, that will move viewToMove up by it's own height:

public void collapse() {
        View viewToMove= mainView.findViewById(R.id.viewToMove);
        int pixelsToMove= viewToMove.getHeight() * -1;
        slots.animate().yBy(pixelsToMove).setDuration(ANIMATION_DURATION_MS);
    }

Take care when you call getHeight() you might need a ViewTreeObserver to get the correct value, because most times before the view is drawn getHeight() will just return 0

Upvotes: 3

Related Questions