Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

android - How to fade out an Activity's UI, and fade in another Activity's UI?

For the moment I have two AppCompatActivity:

  1. The main one, which only displays a launcher UI (i.e. : there is only my app's logo, shown in large size) (ConstraintLayout)

  2. The home one, which displays both a Drawer navigation menu (DrawerLayout) and the real app's UI (ConstraintLayout).

I want to fade out my launcher UI during 2 seconds. After this animation, I will start the second Activity, which must be faded in.

How should I do this?

I looked for some fading-in and fading-out in the in ConstraintLayout and DrawerLayout but I didn't find them. The idea was to adjust their value within the onCreate event handler of the main and other Activity (for the ConstraintLayout of the main, and also for the DrawerLayout and ConstraintLayout of the other).

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

To fade out current activity:

Create a style in res/values:

<style name="PageTransition.Activity" parent="@android:style/Animation.Activity">
    <item name="android:activityOpenEnterAnimation">@android:anim/fade_in</item>
    <item name="android:activityOpenExitAnimation">@android:anim/fade_out</item>
    <item name="android:activityCloseEnterAnimation">@android:anim/fade_out</item>
    <item name="android:activityCloseExitAnimation">@android:anim/fade_in</item>
</style>

Then in your activiy's style which is usually Apptheme for MainActivity: add this line :

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/one</item>
        <item name="colorPrimaryDark">@color/one</item>
        <item name="colorAccent">@color/colorAccent</item>
        name="android:windowAnimationStyle">@style/PageTransition.Activity</item>
    </style>

To start next activity's UI delayed: For your case, you'll need to use handler to delay transitions:

 private final int interval = 1000; // 1 Second
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        public void run() {

        ValueAnimator animator1 = ValueAnimator.ofInt(10, 0);

        animator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {


                float x = (Integer) valueAnimator.getAnimatedValue() / (float) 10;
                yourview.setAlpha(x);
            }
        });

        animator1.setDuration(500);
        animator1.start();
}

Call this oncreate:

handler.postAtTime(runnable, System.currentTimeMillis() + interval);
handler.postDelayed(runnable, 2000); //two seconds

Obviously, you wanna set your initial alphas to 0. Hope this helped!

EDIT:

Define your own fadein-fadeout anims if your want to customize duration...

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:interpolator/decelerate_quad"
    android:fromAlpha="startalpha" android:toAlpha="endalpha"
    android:duration="{YourDuration in ms}"/>

If the previous way fails for some APIs, use this before starting your activity:

...
overridePendingTransition(int enterAnim, int exitAnim);
startActivity(intent);
....

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...