android - 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:
- 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()
- 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?
解决方案
我总是做的是开始一项活动(任何你想要的方式,方式都在这里列出)。
我使用这两个文件使用幻灯片过渡:
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>
然后我开始这样的活动(这是java):
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" />
推荐阅读
- android - 如何解决任务':app:compileFlutterBuildDebug'的执行失败
- c# - 如何在输出旁边添加附加文本?
- rust - 动态货物功能标志
- ldap - CLFRN1254E 在针对 OpenLDAP 服务器同步 HCL 连接的 TDI 时出现异常
- mysql - 比较 Rails 活动记录中日期时间列上的日期
- php - 获取用户自定义元数据值并在 WooCommerce 订单中更新它
- powershell - 获取所有 windows xp 和 server 2003 机器代码失败
- tensorflow-lite - 如何使用 c++ api 在 tflite 中获取权重?
- java - java中如何访问其他类的方法中的变量
- sql - 如何在条件相同的查询中获取相同查询的结果