我自己找到了。onCreateAnimation()
您必须覆盖Fragment
. 我是这样做的:
@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
andmExitAnimation
字段被声明为静态的。要通过加载动画,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;
}