2015-05-18 94 views
0

我正在使用android中的自動播放片段幻燈片動畫。動畫效果很好。我有5個滑動的碎片。一旦第二個片段完成滑動,我想隱藏第一個片段。我寫了下面的代碼,但第一個片段沒有隱藏。請指導我,如果有什麼我必須改變。如何在幻燈片動畫中隱藏片段

我的第二個要求是我想擁有Decelerate_Accelerate_Interpolator。我見過幾個java實現,如https://gist.github.com/NashLegend/5f51e5dc537f470f4384。但我想在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; 
    } 

我得到空指針異常

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) 
+0

檢查這個帖子http://stackoverflow.com/questions/4817900/android-fragments -and-animation http://stackoverflow.com/questions/13005 961/fragmenttransaction-animation-to-slide-in-over-top http://stackoverflow.com/questions/24179899/fragments-and-slide-over-animation –

回答

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

Nope沒有工作。 – geetha