首页 > 解决方案 > Activity transition black screen

问题描述

So I've got WelcomeActivity -> HomeActivity and closing WelcomeActivity with finish()/supportFinishAfterTransition(). I want to do either a slideTransition or a fadeTransition (open to other suggestions btw).

I've researched this and as it turns out there are 2+ ways of doing it: either with overridePendingTransition which uses anim.xml files or with Transitions (from the android docs) which use transition.xml files...

I've tried both and both give me unwanted results:

  1. for anims: I get this ugly mid transition black screen:

fade_in.xml:

    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
       android:interpolator="@android:anim/accelerate_interpolator"
       android:fromAlpha="0.0"
       android:toAlpha="1.0"
       android:duration="300" />

fade_out.xml:

    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
       android:interpolator="@android:anim/accelerate_interpolator"
       android:fromAlpha="1.0"
       android:toAlpha="0.0"
       android:zAdjustment="top"
       android:duration="300" />

WelcomeActivity: (I've tried having finish before the overridePendingTransaction)

    startActivity(Intent(this, HomeActivity::class.java))
    overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
    finish()
  1. for transitions: I can't make it so WelcomeActivity closes properly: It either closes before the animation starts or not closing at all. I'm following the android docs.. I've also tried this:

style.xml

    <item name="android:windowActivityTransitions">true</item>
    <item name="android:windowEnterTransition">@transition/enter_fade</item>
    <item name="android:windowExitTransition">@transition/exit_fade</item>

My other questions is which approach should I have? Is Google pushing the transitions over the anims for starting new activities?

标签: androidandroid-animation

解决方案


我总是做的是开始一项活动(任何你想要的方式,方式都在这里列出)。
我使用这两个文件使用幻灯片过渡:

slide_out_left.xml:

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

slide_in_right.xml:

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

然后我开始这样的活动(这是):

startActivity(MainActivity.this, SecondActivity.class);
overridePendingTransition(R.anim.slide_in_right.xml, R.anim.slide_in_left.xml);
finish();

使用它,活动退出让位于从右到左平滑的新活动。
对于黑屏,在AndroidManifest.xml文件中将该活动的主题设置为半透明

android:theme="@android:style/Theme.Translucent"

所以你的代码将是这样的

<activity android:name=".Activity"
        android:theme="@android:style/Theme.Translucent" />

黑屏答案取自:https ://stackoverflow.com/a/6468734/9819031


推荐阅读