Can I change the Android startActivity() transition animation?

后端 未结 10 1629
臣服心动
臣服心动 2020-11-28 19:53

I am starting an activity and would rather have a alpha fade-in for startActivity(), and a fade-out for the finish(). How can I go about this in th

相关标签:
10条回答
  • 2020-11-28 20:16

    For fadeIn and fadeOut, only add this after super.onCreate(savedInstanceState) in your new Activity class. You don't need to create something else (No XML, no anim folder, no extra function).

    overridePendingTransition(R.anim.abc_fade_in,R.anim.abc_fade_out);
    
    0 讨论(0)
  • 2020-11-28 20:19

    In the same statement in which you execute finish(), execute your animation there too. Then, in the new activity, run another animation. See this code:

    fadein.xml

    <set xmlns:android="http://schemas.android.com/apk/res/android" 
         android:fillAfter="true">
         <alpha android:fromAlpha="1.0" 
                android:toAlpha="0.0"
                android:duration="500"/> //Time in milliseconds
    </set>
    

    In your finish-class

    private void finishTask() {
        if("blabbla".equals("blablabla"){
            finish();
            runFadeInAnimation();
        }
    }
    
    private void runFadeInAnimation() {
        Animation a = AnimationUtils.loadAnimation(this, R.anim.fadein);
        a.reset();
        LinearLayout ll = (LinearLayout) findViewById(R.id.yourviewhere);
        ll.clearAnimation();
        ll.startAnimation(a);   
    }
    

    fadeout.xml

    <set xmlns:android="http://schemas.android.com/apk/res/android"
               android:fillAfter="true">
      <alpha android:fromAlpha="0.0"
             android:toAlpha="1.0"
             android:duration="500"/>
    </set>
    

    In your new Activity-class you create a similiar method like the runFadeAnimation I wrote and then you run it in onCreate and don't forget to change the resources id to fadeout.

    0 讨论(0)
  • 2020-11-28 20:19

    You can simply create a context and do something like below:-

    private Context context = this;
    

    And your animation:-

    ((Activity) context).overridePendingTransition(R.anim.abc_slide_in_bottom,R.anim.abc_slide_out_bottom);
    

    You can use any animation you want.

    0 讨论(0)
  • 2020-11-28 20:33

    Most of the answers are pretty correct, but some of them are deprecated such as when using R.anim.hold and some of them are just elaboratig the process.

    So, you can use:

    startActivity(intent);
    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    
    0 讨论(0)
提交回复
热议问题