ViewPager实现动画从入门到精通(二)---JazzyViewPager使用

来源:互联网 发布:沈阳纳森网络干什么的 编辑:程序博客网 时间:2024/06/18 14:22

今天我们一起学习GutHub上一个特别强大的开源库JazzyViewPager,顾名思义,首先它是由ViewPager 的功能,更重要的是它的动画功能。官网是这样解释的:

An easy to use ViewPager that adds an awesome set of custom swiping animations. Just change your ViewPagers toJazzyViewPagers, two more steps, and you're good to Go!

 即一个易用的ViewPager ,它有惊人的自定义切换动画。你只需将原来的ViewPager换成JazzyViewPagers,再做两部,就可以拥有,下面我们一起看看它的用法吧。

     有兴趣的可以去官网学习哦 https://github.com/jfeinstein10/JazzyViewPager


当然学习之前先看看效果:


JazzyViewPager的各类动画封装在TransitionEffect 

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public enum TransitionEffect {  
  2.         Standard,  
  3.         Tablet,  
  4.         CubeIn,  
  5.         CubeOut,  
  6.         Flip,  
  7.         Stack,  
  8.         ZoomIn,  
  9.         ZoomOut,  
  10.         RotateUp,  
  11.         RotateDown,  
  12.         Accordion  
  13.     }  
我们如果想要使用某个动画的话,只需要两部就可心想事成。

1.调用setTransitionEffect(TransitionEffect.*)选择动画类型 如下所示:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. private JazzyViewPager mJazzy;  
  2. /* ... */  
  3. mJazzy.setTransitionEffect(TransitionEffect.*);  
2.修改PagerAdapter,在addview后调用mJazzy.setObjectForPosition(obj, position);

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. private JazzyViewPager mJazzy;  
  2. /* ... */  
  3. @Override  
  4. public Object instantiateItem(ViewGroup container, final int position) {  
  5.     Object obj = super.instantiateItem(container, position);  
  6.     mJazzy.setObjectForPosition(obj, position);  
  7.     return obj;  
  8. }  

    代码附上:

MainActivity

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class MainActivity extends Activity {  
  2.   
  3.     private JazzyViewPager mJazzy;  
  4.   
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_main);  
  9.         setupJazziness(TransitionEffect.Tablet);  
  10.     }  
  11.   
  12.     @Override  
  13.     public boolean onCreateOptionsMenu(Menu menu) {  
  14.         menu.add("Toggle Fade");  
  15.         String[] effects = this.getResources().getStringArray(R.array.jazzy_effects);  
  16.         for (String effect : effects)  
  17.             menu.add(effect);  
  18.         return true;  
  19.     }  
  20.   
  21.     @Override  
  22.     public boolean onOptionsItemSelected(MenuItem item) {  
  23.         if (item.getTitle().toString().equals("Toggle Fade")) {  
  24.             mJazzy.setFadeEnabled(!mJazzy.getFadeEnabled());  
  25.         } else {  
  26.             TransitionEffect effect = TransitionEffect.valueOf(item.getTitle().toString());  
  27.             setupJazziness(effect);  
  28.         }  
  29.         return true;  
  30.     }  
  31.   
  32.     private void setupJazziness(TransitionEffect effect) {  
  33.         mJazzy = (JazzyViewPager) findViewById(R.id.jazzy_pager);  
  34.         mJazzy.setTransitionEffect(effect);//设置选择的动画效果  
  35.         mJazzy.setAdapter(new MainAdapter());  
  36.         mJazzy.setPageMargin(30);//设置页与页之间的间距  
  37.     }  
  38.   
  39.     private class MainAdapter extends PagerAdapter {  
  40.         @Override  
  41.         public Object instantiateItem(ViewGroup container, final int position) {  
  42.             TextView text = new TextView(MainActivity.this);  
  43.             text.setGravity(Gravity.CENTER);  
  44.             text.setTextSize(30);  
  45.             text.setTextColor(Color.WHITE);  
  46.             text.setText("Page " + position);  
  47.             text.setPadding(30, 30, 30, 30);  
  48.             int bg = Color.rgb((int) Math.floor(Math.random()*128)+64,   
  49.                     (int) Math.floor(Math.random()*128)+64,  
  50.                     (int) Math.floor(Math.random()*128)+64);  
  51.             text.setBackgroundColor(bg);  
  52.             container.addView(text, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);  
  53.               
  54.             mJazzy.setObjectForPosition(text, position);//保存view  
  55.             return text;  
  56.         }  
  57.         @Override  
  58.         public void destroyItem(ViewGroup container, int position, Object obj) {  
  59.             container.removeView(mJazzy.findViewFromObject(position));  
  60.         }  
  61.         @Override  
  62.         public int getCount() {  
  63.             return 10;  
  64.         }  
  65.         @Override  
  66.         public boolean isViewFromObject(View view, Object obj) {  
  67.             if (view instanceof OutlineContainer) {  
  68.                 return ((OutlineContainer) view).getChildAt(0) == obj;  
  69.             } else {  
  70.                 return view == obj;  
  71.             }  
  72.         }         
  73.     }  
  74. }  

 activity_main.xml

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <com.jfeinstein.jazzyviewpager.JazzyViewPager  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     android:id="@+id/jazzy_pager"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent" />  
0 0
原创粉丝点击