2013-06-23 87 views
-2

有一點問題。我的動畫不起作用。我沒有遇到任何錯誤。只是我的動畫不起作用。動畫無效

這是我的代碼。

public class MainActivity extends FragmentActivity { 

    private PagerAdapter mPagerAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     this.initialisePaging(); 
    } 

    private void initialisePaging() { 

     List<Fragment> fragments = new Vector<Fragment>(); 
     fragments.add(Fragment.instantiate(this, Tab1Fragment.class.getName())); 
     fragments.add(Fragment.instantiate(this, Tab2Fragment.class.getName())); 
     fragments.add(Fragment.instantiate(this, Tab3Fragment.class.getName())); 
     this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments); 

     ViewPager pager = (ViewPager)super.findViewById(R.id.pager); 
     pager.setAdapter(this.mPagerAdapter); 
     pager.setPageTransformer(true, new ZoomOutPageTransformer()); 
    } 

    public static class Tab1Fragment extends Fragment { 

     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      if (container == null) { 
       return null; 
      } 
      return (ImageView)inflater.inflate(R.layout.tab_frag1_layout, container, false); 
     } 
    } 

    public static class Tab2Fragment extends Fragment { 

     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      if (container == null) { 
       return null; 
      } 
      return (ImageView)inflater.inflate(R.layout.tab_frag2_layout, container, false); 
     } 
    } 

    public static class Tab3Fragment extends Fragment { 

     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      if (container == null) { 
       return null; 
      } 
      return (ImageView)inflater.inflate(R.layout.tab_frag3_layout, container, false); 
     } 
    } 

    public static class PagerAdapter extends FragmentPagerAdapter { 

     private List<Fragment> fragments; 

     public PagerAdapter(FragmentManager fm, List<Fragment> fragments) { 
      super(fm); 
      this.fragments = fragments; 
     } 

     @Override 
     public Fragment getItem(int position) { 
      return this.fragments.get(position); 
     } 

     @Override 
     public int getCount() { 
      return this.fragments.size(); 
     } 
    } 

    public static class ZoomOutPageTransformer implements ViewPager.PageTransformer { 
     private static final float MIN_SCALE = 0.85f; 
     private static final float MIN_ALPHA = 0.5f; 

     public void transformPage(View view, float position) { 
      int pageWidth = view.getWidth(); 
      int pageHeight = view.getHeight(); 

      if (position < -1) { // [-Infinity,-1) 
       // This page is way off-screen to the left. 
//    view.setAlpha(0); 
       ViewHelper.setAlpha(view, 0); 

      } else if (position <= 1) { // [-1,1] 
       // Modify the default slide transition to shrink the page as well 
       float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position)); 
       float vertMargin = pageHeight * (1 - scaleFactor)/2; 
       float horzMargin = pageWidth * (1 - scaleFactor)/2; 
       if (position < 0) { 
//     view.setTranslationX(horzMargin - vertMargin/2); 
        ViewHelper.setTranslationX(view, horzMargin - vertMargin/2); 
       } else { 
//     view.setTranslationX(-horzMargin + vertMargin/2); 
        ViewHelper.setTranslationX(view, -horzMargin + vertMargin/2); 
       } 

       // Scale the page down (between MIN_SCALE and 1) 
//    view.setScaleX(scaleFactor); 
//    view.setScaleY(scaleFactor); 
       ViewHelper.setScaleX(view, scaleFactor); 
       ViewHelper.setScaleY(view, scaleFactor); 

       // Fade the page relative to its size. 
//    view.setAlpha(MIN_ALPHA + 
//      (scaleFactor - MIN_SCALE)/
//      (1 - MIN_SCALE) * (1 - MIN_ALPHA)); 

       ViewHelper.setAlpha(view, MIN_ALPHA + 
         (scaleFactor - MIN_SCALE)/
         (1 - MIN_SCALE) * (1 - MIN_ALPHA)); 

      } else { // (1,+Infinity] 
       // This page is way off-screen to the right. 
//    view.setAlpha(0); 
       ViewHelper.setAlpha(view, 0); 
      } 
     } 
    } 
} 

其他的東西都正常工作。我想實施this.請幫幫我。

之前我沒有使用NineOldroids實施。但即使在實施這個之後,它也沒有奏效。

+0

動畫的代碼在哪裏? – Plato

+0

檢查此ZoomOutPageTransformer類。 –

+0

如果您認爲我的代碼存在任何技術缺陷而不是簡單地進行投票,請發表評論。這將幫助我糾正和改進我的編碼標準。無論如何。謝謝。 –

回答

0

它沒有在與Android 2.2平臺的模擬器工作,但當我檢查我的電話索尼Xperia Tipo雙有ICS作爲平臺,它的工作。

1

我也有這個問題檢查出this answer。使用nineoldandroid應該可以工作,但是在11之前(Heneycomb)忽略PageTransform,雖然它在支持庫中。我正在考慮移動來查看鰭狀肢只是爲了做動畫。但我仍在尋找另一種方式。

編輯:在這個問題中找出我的答案,看看我是如何解決這個問題的。我基本上覆制了viewpager類,並刪除了在setPageTransformer中檢查SDK版本的if語句。