How to animate a layout in android?

2020-07-25 08:56发布

In my Activity screen,half of the screen contain a layout.When Activity loaded it visible and after 10 seconds it will be get down slowly finally it will be not visible to user.But it get down slowly.How i can do it.Please can any one help me.

Thanking in Advance.

4条回答
啃猪蹄的小仙女
2楼-- · 2020-07-25 09:24

In your res\anim folder (create the folder if it's not there) create slide_out_down.xml and paste the following

<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"

android:fromYDelta="0%p"
android:toYDelta="100%p"
android:duration="@android:integer/config_longAnimTime" />

to start the animation and hide the view use this

 private void hideView(final View view){
    Animation animation = AnimationUtils.loadAnimation(this, R.anim.slide_out_down);
    //use this to make it longer:  animation.setDuration(1000);
    animation.setAnimationListener(new AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {}

        @Override
        public void onAnimationRepeat(Animation animation) {}

        @Override
        public void onAnimationEnd(Animation animation) {
             view.setVisibility(View.GONE);
        }
    });

    view.startAnimation(animation);
}
查看更多
聊天终结者
3楼-- · 2020-07-25 09:25
public void animateLayout(){
        LinearLayout layout = findViewById(R.id.layoutId);
        layout.animate().translationYBy(1000f).setDuration(50000);
    }

The above code will make the view go invisible very slowly.

setDuration(50000) //change the number according to your need. It varies the speed of the layout.

查看更多
虎瘦雄心在
4楼-- · 2020-07-25 09:31

You can use FragmentActivity and Fragment for this and add animation to the fragment

<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:interpolator="@android:anim/accelerate_interpolator">
 <scale
  android:fromXScale="1.0" android:toXScale="0.0"
  android:fromYScale="1.0" android:toYScale="0.0"
  android:pivotX="50%"
  android:pivotY="50%"
  android:duration="1000" 
 />

查看更多
神经病院院长
5楼-- · 2020-07-25 09:43
try this:

// gone layout
     collapse(recipientLayout);
//show layout
      expand(recipientLayout);

    public void expand(final LinearLayout v) {
    v.measure(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    final int targtetHeight = v.getMeasuredHeight();
    /*if (v.isShown()) {
        collapse(v);
    } else */{
        v.getLayoutParams().height = 0;
        v.setVisibility(View.VISIBLE);
        Animation a = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime,
                                               Transformation t) {
                v.getLayoutParams().height = interpolatedTime == 1 ? LinearLayout.LayoutParams.WRAP_CONTENT
                        : (int) (targtetHeight * interpolatedTime);
                v.requestLayout();
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
        a.setDuration((int) (targtetHeight + 600));
        v.startAnimation(a);
    }

}

public void collapse(final LinearLayout v) {
    final int initialHeight = v.getMeasuredHeight();
    Animation a = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime,
                                           Transformation t) {
            /*if (v.isShown()) {
                collapse(v);
            }*/
            if (interpolatedTime == 1) {
                v.setVisibility(View.GONE);
            } else {
                v.getLayoutParams().height = initialHeight
                        - (int) (initialHeight * interpolatedTime);
                v.requestLayout();
            }
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    a.setDuration((int) (v.getLayoutParams().height + 600));
    v.startAnimation(a);
}
查看更多
登录 后发表回答