如何淡出一个Activity的UI,并淡入另一个Activity的UI? [英] How to fade out an Activity's UI, and fade in another Activity's UI?

查看:239
本文介绍了如何淡出一个Activity的UI,并淡入另一个Activity的UI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有两个AppCompatActivity:

  1. 主菜单,仅显示启动器用户界面(即:只有我的应用徽标,以大尺寸显示)(ConstraintLayout)

家庭住所,既显示抽屉"导航菜单(DrawerLayout),又显示真实应用程序的UI(ConstraintLayout).

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

我想在2秒钟内淡出启动器用户界面.播放完该动画之后,我将开始第二个Activity,必须将其淡入.

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.

我应该怎么做?

我在ConstraintLayoutDrawerLayout中寻找了一些fading-infading-out,但没有找到它们.想法是在主体的onCreate事件处理程序和其他Activity的事件处理程序中调整它们的值(对于主体的ConstraintLayout,对于另一个主体的DrawerLayoutConstraintLayout).

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).

推荐答案

淡出当前活动:

在res/values中创建样式:

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>

然后按照您的活动风格,通常是MainActivity的Apptheme: 添加此行:

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>

要延迟启动下一个活动的UI,请执行以下操作: 对于您的情况,您需要使用处理程序来延迟转换:

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();
}

在oncreate上调用它:

Call this oncreate:

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

很显然,您要将初始字母设置为0. 希望这会有所帮助!

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

如果您想自定义持续时间,请定义自己的淡入淡出动画...

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}"/>

如果某些API的前一种方法失败,请在开始活动之前使用此方法:

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

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

这篇关于如何淡出一个Activity的UI,并淡入另一个Activity的UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆