I followed this documentation: https://developer.android.com/training/transitions/start-activity#java .
First, I will show you my implementation. You will find my question at the end of this post.
Thus, I modified 4 files:
- fade.xml, which defines the duration of the fade in transition.
<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<fade android:fadingMode="fade_in" android:duration="3000" />
</transitionSet>
- values/styles.xml, which defines the
windowEnterTransition
thanks to fade.xml. Note that MainActivity's parent, AppTheme, has Material as parent.
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Material">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowActivityTransitions">true</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="MainActivityTheme" parent="AppTheme">
<item name="android:windowEnterTransition">@transition/fade</item>
</style>
- Since I created this new theme "MainActivityTheme", I also modified the Android Manifest to specify this new theme to this activity:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:theme="@style/MainActivityTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
- Finally, I have set the enter transition in
MainActivity.java
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setEnterTransition(new Fade(Fade.IN));
setContentView(R.layout.activity_main);
}
Emulator and Gradle build
build.gradle (Module: app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.example.."
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:design:27.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Emulator
The version of Android installed within the emulator is: 5.1.1 (cf. "About phone" in the settings of Android, in the emulator).
Question
However, when I launch my application, the main activity's UI doesn't fade in. Do you know why ? In other words: Why the XML specified transition isn't executed? (or why its specified duration isn't taken account?)
I also tried to move <item name="android:windowActivityTransitions">true</item>
from the theme MainActivityTheme
(file: styles.xml) to the theme AppTheme
(file: the same, i.e.: styles.xml). But it didn't fix this bug.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…