加载片段不会淡入或淡出

时间:2014-10-20 07:47:29

标签: android android-fragments android-animation

我有一个fragment,我希望它在添加时淡入淡出,在删除时淡出淡出。我现在这样做的方式如下:(但,它没有任何动画效果)

加入Fadein:

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();

fadein.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>

fadeout.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>

我的片段类:

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);
    }
}

3 个答案:

答案 0 :(得分:1)

试试这个:

manager.beginTransaction().setCustomAnimations(R.anim.fadein, R.anim.fadeout, R.anim.fadeout, R.anim.fadein)
                .add(R.id.loading_fragment_container, loadingFragment, "loadingFragment").commit();

顺便说一句:动画周期为1.5秒很长

答案 1 :(得分:1)

对于Fadein FragmentManager manager = getFragmentManager(); manager.beginTransaction()。setCustomAnimations(android.R.animator.fade_in,android.R.animator.fade_out)                 .add(R.id.loading_fragment_container,loadingFragment,“loadingFragment”)。commit();

对于Fadeout manager.beginTransaction()。setCustomAnimations(android.R.animator.fadeout,android.R.animator.fadein)             卸下摆臂(loadingFragment).commit();

答案 2 :(得分:0)

在片段中覆盖此

@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
    return AnimationUtils.loadAnimation(getActivity(),
            enter ? android.R.anim.fade_in : android.R.anim.fade_out);
}