Android 过渡动画不工作

Android transition animation is not working

Android 过渡与爆炸相同,slide.Actually 我不认为它是动画。持续时间也不是 6 秒。我该如何解决?

代码取自here

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);

        Transition ts = new Slide();  //Slide(); //Explode();

        ts.setDuration(6000);
        getWindow().setEnterTransition( ts );
        getWindow().setExitTransition( ts );
        setContentView(R.layout.activity_main_activity);



    }




    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finishAfterTransition();
    }

风格-v21.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="BaseAppTheme" parent="android:Theme.Material.Light">
        <item name="android:colorControlHighlight">#0000AA</item>
        <item name="android:navigationBarColor">#0000AA</item>
        <item name="android:colorPrimaryDark">#0000AA</item>
        <item name="android:colorPrimary">#0000FF</item>
        <item name="android:colorAccent">#00FF00</item>
        <item name="android:windowBackground">@android:color/black</item>
        <item name="android:textColorPrimary">@android:color/white</item>

        <item name="android:windowContentTransitions">true</item>
        <item name="android:windowAllowEnterTransitionOverlap">true</item>

        <!-- specify enter and exit transitions -->
        <item name="android:windowEnterTransition">@transition/explode</item>
        <item name="android:windowExitTransition">@transition/explode</item>
    </style>
</resources>

您可以使用它来开始新的 activity 转换

startActivity(new Intent(this, NewActivity.class));
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);

创建文件res/anim/slide_in_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false" >
     <translate android:duration="1000" android:fromXDelta="100%" android:toXDelta="0%" />
    <alpha android:duration="1000" android:fromAlpha="0.0" android:toAlpha="1.0" />
</set>

创建文件res/anim/slide_out_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false" >
     <translate android:duration="2000" android:fromXDelta="0%" android:toXDelta="-100%"/>
     <alpha android:duration="2000" android:fromAlpha="1.0" android:toAlpha="0.0" />
</set>

不要忘记为 activity 或 AndroidManifest.xml 中的整个包应用主题。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.package"
android:theme="@style/BaseAppTheme">
    ....
    ....
</manifest>

就我自己而言,出于某种原因,过渡动画在我的 android 设备的开发人员选项中处于关闭状态。

Just go to Settings -> Developer Options -> Transition animation scale: set to Animation scale 1x

我发现我真的需要这样做

ActivityOptionsCompat activityOptionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, test, "transition_name");
ActivityCompat.startActivity(TourAndLoginActivity.this, new Intent(TourAndLoginActivity.this, LoginActivity.class), activityOptionsCompat.toBundle());

还是很费解,但我不知道为什么我们需要一个共享对象来获取进入/退出转换。如果我提供 null 而不是 ActivityOptionsCompat,则没有转换