我有一个fragment我希望它在添加时淡入,在删除时淡出。我目前的做法如下:(不过,它没有任何动画效果)
添加后淡入:
FragmentManager manager =getFragmentManager();
manager.beginTransaction().setCustomAnimations(R.anim.fadein, R.anim.fadeout)
.add(R.id.loading_fragment_container, loadingFragment, "loadingFragment").commit();
移除时淡出:
manager.beginTransaction().setCustomAnimations(R.anim.fadeout, R.anim.fadein)
.remove(loadingFragment).commit();
淡入淡出.xml
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android">
android:interpolator="@android:anim/linear_interpolator"
android:propertyName="alpha"
android:valueFrom="0.0"
android:valueTo="1"
android:duration="1500"
</objectAnimator>
淡出.xml
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android">
android:interpolator="@android:anim/linear_interpolator"
android:propertyName="alpha"
android:valueFrom="1"
android:valueTo="0.0"
android:duration="1500"
</objectAnimator>
我的 fragment 类:
public class LoadingFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_loading_layout, container,false);
}
//I even tried to add this but still nothing happens
@Override
public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {
return super.onCreateAnimator(android.R.anim.fade_in, true, android.R.anim.fade_out);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//basically this code just makes the loading image (which is a circle) spinning on its center forever
//https://stackoverflow.com/questions/3137490/how-to-spin-an-android-icon-on-its-center-point
ImageView loadingCircle= (ImageView) getActivity().findViewById(R.id.loadingImage);
Log.d("loadingCircle","loadingCircle: "+loadingCircle);
RotateAnimation r = new RotateAnimation(0.0f, 10.0f * 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
r.setDuration(10000);
r.setInterpolator(new LinearInterpolator());
r.setRepeatCount(Animation.INFINITE);
r.setRepeatMode(Animation.INFINITE);
r.setFillEnabled(true);
r.setFillAfter(true);
loadingCircle.startAnimation(r);
}
}
最佳答案
对于淡入淡出 FragmentManager 管理器 =getFragmentManager(); manager.beginTransaction().setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out) .add(R.id.loading_fragment_container,loadingFragment,“loadingFragment”).commit();
淡出 manager.beginTransaction().setCustomAnimations(android.R.animator.fadeout, android.R.animator.fadein) .remove(loadingFragment).commit();
关于android - 加载 fragment 没有淡入或淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26460735/