我自己找到了。您必須覆蓋Fragment
中的onCreateAnimation()
方法。這是我做的:
@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
if (enter)
return mEnterAnimation;
else
return mExitAnimation;
}
private static final Animation mEnterAnimation = AnimationUtils
.loadAnimation(MyApplication.getInstance().getBaseContext(),
R.anim.frag_fade_in);
private static final Animation mExitAnimation = AnimationUtils
.loadAnimation(MyApplication.getInstance().getBaseContext(),
R.anim.frag_fade_out);
注意,mEnterAnimation
和mExitAnimation
字段被聲明爲static。要通過AnimationUtils.loadAnimation()
載入動畫,您需要一個Context
。 Context
是通過MyApplication
單身獲得的。要創建這樣一個單隻寫到您的AndroidManifest.xml
:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app"
android:versionCode="1"
android:versionName="1.0" >
<application
android:name=".MyApplication"
android:label="@string/app_name" >
<!-- Insert your activities here -->
</application>
</manifest>
然後創建該類com.example.app.MyApplication
作爲單:
public class MyApplication extends Application {
public static MyApplication getInstance() {
return mInstance;
}
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
private static MyApplication mInstance;
}
它不工作。我知道內置的'R.anim.fade_in'和'R.anim.fad_out',但我需要更快的動畫。不管怎樣,謝謝你。 – mneri