当我的启动画面完成(2秒)并且移动到我的主要活动时,我想要使用alpha动画进行此操作。一切都有效,除了动画......
这是我的代码。它有什么问题吗? (我有一个文字和一个闪屏图像)
MainActivity.java (这里真的没有触及任何内容)
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
SplashActivity.java
public class SplashActivity extends Activity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
int delay = 2000;
new Handler().postDelayed(new Runnable() {
public void run() {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
SplashActivity.this.finish();
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
}, delay);
}
public void onBackPressed(){}
}
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="2000" />
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:duration="2000" />
AndroidMenifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.id11201478.WhatIf" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name=".SplashActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activty_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#FFFFFF"
android:id="@+id/splashLayout"
android:layout_height="match_parent">
<TextView
android:text="What If?"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:textSize="50dp"
android:id="@+id/appName" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/appImage"
android:src="@drawable/image"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</LinearLayout>
谢谢!
答案 0 :(得分:0)
您可以尝试移动代码
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
到MainActivity
。所以它变成了这样的东西。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
}