ViewPager looping sliding + scaling gradient over-animation for multi-Image Browsing,
As shown above.
First, implement the loop:
Public class MyAdapter extends PagerAdapter {/*** install ImageView array */private ImageView [] mImageViews;/*** image resource id */private HashMap <Integer, view> mChildrenViews = new javashashmap <Integer, View> (); private int [] imgIdArray; private Context context; public MyAdapter (Context context, int [] id) {this. context = context; this. imgIdArray = id; initImageView ();} private void initImageView () {mImageViews = new ImageView [imgIdArray. length]; for (int I = 0; I <mImageViews. length; I ++) {ImageView imageView = new ImageView (context); imageView. setBackgroundResource (imgIdArray [I]); mImageViews [I] = imageView; }}@ Overridepublic int getCount () {return Integer. MAX_VALUE;} public int getItemCount () {return mImageViews. length ;}@ Overridepublic boolean isViewFromObject (View arg0, Object arg1) {return arg0 = arg1 ;}@ Overridepublic void destroyItem (View container, int position, Object object) {(ViewPager) container ). removeView (mImageViews [position % imgIdArray. length]) ;}@ Overridepublic Object instantiateItem (View container, int position) {(ViewPager) container ). addView (mImageViews [position % imgIdArray. length], 0); setObjectForPosition (mImageViews [position % imgIdArray. length], position); return mImageViews [position % imgIdArray. length];} private void setObjectForPosition (View view, int position) {mChildrenViews. put (position, view);} public HashMap <Integer, View> getViewMap () {return mChildrenViews ;}}
1. Processing PagerAdapter
To achieve the cyclic effect, we return the getCount () method to a maximum integer number.
public int getCount() {return Integer.MAX_VALUE;}Since the number of items cannot reach the number of getCount (), we can process the position when instantiateItem is used.
mImageViews[position% imgIdArray.length]
After processing, the VIEW returned by instantiateItem is always between mImageViews [0]-mImageViews [imgIdArray. length.
2. ViewPager Processing
If the processing of pagerAdapter is not enough, we will find that it is no problem to slide to the left, but it cannot slide to the right because the position is 0 and it cannot slide to the right.
Therefore, set the initial position to 100 times the number of images.
Public void setAdapter (PagerAdapter arg0) {// TODO Auto-generated method stubsuper. setAdapter (arg0); // In the initial state, you can slide to the right. // set the current position to 100 times the number of resources setCurrentItem (MyAdapter) getAdapter ()). getItemCount () * 100 );}
After this processing, the two sides can slide.
Animation:
Listen to the split page
Private OnPageChangeListener PageChangeListener = new OnPageChangeListener () {@ Overridepublic void onPageSelected (int arg0) {if (pageIndicator! = Null & pageIndicator. length> 1) setSelectBackground (arg0% pageIndicator. length) ;}@ Overridepublic void onPageScrolled (int arg0, float arg1, int arg2) {// TODO Auto-generated method stubfloat implements toffset = isSmall (arg1 )? 0: arg1; // obtain ViewmLeft = findViewFromObject (arg0) on the left; // obtain ViewmRight = findViewFromObject (arg0 + 1) on the Right; // Add the animation effect animateAlpha (mLeft, mRight, writable toffset); animateXY (mLeft, mRight, writable toffset);} @ Overridepublic void onPageScrollStateChanged (int arg0) {// TODO Auto-generated method stub }};
There are two pages involved in each page cutting. We want to take the two views out and use the property animation for processing.
protected void animateAlpha(View left, View right, float positionOffset) {if (left != null) {ViewHelper.setAlpha(left, 1-positionOffset);}if (right != null) {ViewHelper.setAlpha(right, positionOffset);}}private void animateXY(View left, View right, float positionOffset) {if (left != null) {ViewHelper.setPivotX(left, left.getMeasuredWidth());ViewHelper.setPivotY(left, 0);ViewHelper.setScaleX(left, 1-positionOffset);}if (right != null) {ViewHelper.setPivotX(right, 0);ViewHelper.setPivotY(right, 0);ViewHelper.setScaleX(right, positionOffset);}}
Source code download