如何在幻灯片动画中隐藏片段

时间:2015-05-18 06:43:28

标签: android android-fragments android-animation

我正在研究android中的自动播放片段幻灯片动画。动画效果很好。我有5个滑动的碎片。我想在第二个片段完成滑动后隐藏第一个片段。我写了下面的代码,但第一个片段没有隐藏。如果我需要改变的话,请指导我。

我的第二个请求是我想要Decelerate_Accelerate_Interpolator。我见过几个像https://gist.github.com/NashLegend/5f51e5dc537f470f4384这样的java实现。但我想在xml文件中添加插值器引用。我不认为我可以以编程方式操纵我的ObjectAnimator。当我在Fragment中为setCustomAnimations方法传递此slide_in时。

Slide_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator

        android:valueFrom="1"
        android:valueTo="0"
        android:valueType="floatType"
        android:propertyName="XFraction"
        android:duration="1600"
         />
</set>

我的活动类

public class TestUIActivity extends FragmentActivity {
    private final String TAG ="TestUIActivity";

    Fragment fragment = new ClinicloudGreyFragment();

    int currentPage = 0, TIME_DELAY=1600, REPEAT =500;

    boolean stop = false;

    Timer swipeTimer = null;

    private final Handler mHandler = new Handler();
    //MyPagerAdapter mAdapter = new MyPagerAdapter(getSupportFragmentManager());
    //ViewPager mPager = null;

    Runnable update = new Runnable() {
        @Override
        public void run() {
            if(currentPage == getCount()) {
                stop = true;
                currentPage = getCount()-1;
                if(mHandler != null) {
                    mHandler.removeCallbacks(update);
                }
            }
            if(!stop) {
                if(currentPage == 1) {
                    TIME_DELAY=1600;
                    REPEAT=500;
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    ft.setCustomAnimations(R.anim.slideinwithhalfasecdelay, R.anim.slideout);
                    //ft.hide(fragment);
                    //fragment = new ClinicloudPurpleFragment();
                    ft.replace(R.id.topPanel, new ClinicloudPurpleFragment());
                    ft.commit();
                } else if (currentPage == 2) {
                    TIME_DELAY=1600;
                    REPEAT=500;

                    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    ft.hide(fragment);
                    //fragment = new ClinicloudOrangeFragment();
                    ft.setCustomAnimations(R.anim.slideinwithasecdelay,R.anim.slideout);
                    ft.replace(R.id.topPanel, new ClinicloudOrangeFragment());
                    ft.commit();
                } else if (currentPage == 3) {
                    TIME_DELAY=1400;
                    REPEAT=400;
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    ft.hide(fragment);
                    ft.setCustomAnimations(R.anim.slideinwithoneandhalfasecdelay, R.anim.slideout);
                    ft.replace(R.id.topPanel, new ClinicloudYellowFragment());
                    ft.commit();
                } else if (currentPage == 4) {
                    TIME_DELAY = 1200;
                    REPEAT=400;
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    ft.hide(fragment);
                    ft.setCustomAnimations(R.anim.slideinwithtwosecdelay,R.anim.slideout);
                    ft.replace(R.id.topPanel, new ClinicloudBlueFragment());
                    ft.commit();
                }

                currentPage++;

            }
        }
    };


    public int getCount() {
        return 5;
    }

我的活动XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/topPanel"
        android:name="com.clinicloud.app.ClinicloudGreyFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="76.4">
    </fragment>

    <fragment
        android:id="@+id/bottomPanel"
        android:name="com.clinicloud.app.ClinicloudBottonFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="23.6">
    </fragment>

</LinearLayout>

评论之后,我修改了我的Activity类,如下所示。

public class TestUIActivity extends FragmentActivity {
    private final String TAG ="TestUIActivity";



    Fragment fragment = new ClinicloudGreyFragment();

    int currentPage = 0, TIME_DELAY=1600, REPEAT =500;

    boolean stop = false;

    Timer swipeTimer = null;

    private final Handler mHandler = new Handler();
    //MyPagerAdapter mAdapter = new MyPagerAdapter(getSupportFragmentManager());
    //ViewPager mPager = null;

    Runnable update = new Runnable() {
        @Override
        public void run() {
            if(currentPage == getCount()) {
                stop = true;
                currentPage = getCount()-1;
                if(mHandler != null) {
                    mHandler.removeCallbacks(update);
                }
            }
            if(!stop) {
                if(currentPage == 1) {
                    TIME_DELAY=1600;
                    REPEAT=500;
                    FragmentTransaction ftTop = getFragmentManager().beginTransaction();
                    ftTop.setCustomAnimations(R.anim.slideinwithhalfasecdelay, R.anim.slideout);
                    //ft.hide(fragment);
                    //fragment = new ClinicloudPurpleFragment();
                    ClinicloudPurpleFragment clinicloudPurpleFragment = ClinicloudPurpleFragment.newInstance();
                    ftTop.replace(R.id.topPanel, clinicloudPurpleFragment, "Purple Fragment");
                    ftTop.commit();
                } else if (currentPage == 2) {
                    TIME_DELAY=1600;
                    REPEAT=500;
                    FragmentTransaction ftTop = getFragmentManager().beginTransaction();
                    ftTop.setCustomAnimations(R.anim.slideinwithasecdelay, R.anim.slideout);
                   // FragmentTransaction ft = getFragmentManager().beginTransaction();
                    ftTop.hide(getFragmentManager().findFragmentByTag("GreyFragment"));
                    //fragment = new ClinicloudOrangeFragment();
                    ClinicloudOrangeFragment clinicloudOrangeFragment = ClinicloudOrangeFragment.newInstance();
                    ftTop.replace(R.id.topPanel, clinicloudOrangeFragment, "Orange Fragment");
                    ftTop.commit();
                } else if (currentPage == 3) {
                    TIME_DELAY=1400;
                    REPEAT=400;
                    FragmentTransaction ftTop = getFragmentManager().beginTransaction();
                    ftTop.setCustomAnimations(R.anim.slideinwithoneandhalfasecdelay, R.anim.slideout);
                    ftTop.hide(getFragmentManager().findFragmentByTag("GreyFragment"));
                    ClinicloudYellowFragment clinicloudYellowFragment = ClinicloudYellowFragment.newInstance();
                    ftTop.replace(R.id.topPanel, clinicloudYellowFragment, "Yellow Fragment");
                    ftTop.commit();
                } else if (currentPage == 4) {
                    TIME_DELAY = 1200;
                    REPEAT=400;
                    FragmentTransaction ftTop = getFragmentManager().beginTransaction();
                    ftTop.setCustomAnimations(R.anim.slideinwithtwosecdelay, R.anim.slideout);
                    ftTop.hide(getFragmentManager().findFragmentByTag("GreyFragment"));

                    ClinicloudBlueFragment clinicloudBlueFragment = ClinicloudBlueFragment.newInstance();
                    ftTop.replace(R.id.topPanel, clinicloudBlueFragment,"Blue Fragment");
                    ftTop.commit();
                }

                currentPage++;

            }
        }
    };


    public int getCount() {
        return 5;
    }

我得到了nullpointer异常

Process: com.clinicloud.app, PID: 25721
java.lang.NullPointerException
        at android.app.BackStackRecord.run(BackStackRecord.java:658)
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
        at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码:

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);

DetailsFragment newFragmentobject = DetailsFragment.newInstance();

ft.replace(R.id.fragment_container, newFragmentobject, "Fragment");

// Start the animated transition.
ft.commit();