标签:
看了翔哥的blog,本篇blog来记录下github JazzyViewPager的使用。
直接将JazzyViewPager的代码copy进项目中。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="csdn.viewpagerexpand.MainActivity">
<csdn.viewpagerexpand.view.JazzyViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="Hello World!" />
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
private JazzyViewPager viewPager;
private int[] res=new int[]{R.drawable.banner_1 ,R.drawable.banner_2 ,R.drawable.banner_3};
private List<ImageView> imageViews=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
viewPager= (JazzyViewPager) findViewById(R.id.vp);
//初始化数据
initDate();
//@关键1 设置切换效果
viewPager.setTransitionEffect(JazzyViewPager.TransitionEffect.Accordion);
viewPager.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
return res.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(imageViews.get(position));
//@关键2
viewPager.setObjectForPosition(imageViews.get(position), position);
return imageViews.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager)container).removeView(imageViews.get(position));
}
});
}
private void initDate(){
for (int i=0;i<res.length;i++){
ImageView imageView=new ImageView(getApplicationContext());
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageResource(res[i]);
imageViews.add(imageView);
}
}
}
JazzyViewPager提供了如下多的动画切换方式:
public enum TransitionEffect {
Standard,
Tablet,
CubeIn,
CubeOut,
FlipVertical,
FlipHorizontal,
Stack,
ZoomIn,
ZoomOut,
RotateUp,
RotateDown,
Accordion
}
效果都很赞,如果是在产品允许的条件下,适当的使用一些动画相信会带来更好的体验效果。
标签:
原文地址:http://blog.csdn.net/u014733374/article/details/51353428