Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

android - Make activity animate from top to bottom

I am writing an Android app where I want the activity to appear by animating in from the bottom of the screen to the top. I am able to do this with code from here:

However, I am not able to do the vice-versa animation wherein the Activity would disappear by sliding from the top to the bottom of the screen.

I used the code in the above link; the activity appears by sliding up, but when disappearing, it fades out, instead of sliding to the bottom.

I even tried putting the code in onCreate() :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    overridePendingTransition(R.anim.appear_from_bottom, R.anim.disappear_to_bottom);
    setContentView(R.layout.activity_all_metadata_display);
    initializePage();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to define your "slide up" animations from the linked question, and some new "slide down" animations that reverse the process.

The important parts of the animations to look at are the fromYDelta and toYDelta values. These define the Y-positions (of the top of your view) at the start & end of the animations.

slide_in_up.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime"
    android:fromYDelta="100%p"
    android:toYDelta="0%p" />

slide_out_up.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime"
    android:fromYDelta="0%p"
    android:toYDelta="-100%p" />

slide_in_down.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime"
    android:fromYDelta="-100%p"
    android:toYDelta="0%p" />

slide_out_down.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime"
    android:fromYDelta="0%p"
    android:toYDelta="100%p" />

For the "slide up" animations, you should have overridden the pending transition in your onResume() method like this:

protected void onResume()
{
    super.onResume();
    overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up);
}

For the "slide down" animations, do something similar in your onPause() method:

protected void onPause()
{
    super.onPause();
    overridePendingTransition(R.anim.slide_in_down, R.anim.slide_out_down);
}

Some tutorials suggest using the wrong life-cycle methods:

  • onCreate() is not called every time the activity is shown
  • onDestroy() is not called every time the activity is taken away

Rather use methods that are called every time there is a screen transition:

  • onResume() is called when the activity is shown to the user
  • onPause() is called when the activity is going to be taken away

For more info on these methods specifically, check the Android developer site:


When your screen is displayed, it will slide in from the bottom.

When a new screen is displayed, your screen will slide back down.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...