上一篇viewpager的切换动画中我们看到需要修改viewpager的源码,这里我们为了方便,并且不会出现版本不兼容的问题,所以自定义一个viewpager
MyViewPager.class(这里是用了缩放和移动的动画,通过map存储梯度值)
/**
*
*/
package com.test.sildingmenu.view;
import java.util.HashMap;
import java.util.Map;
import com.nineoldandroids.view.ViewHelper;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
/**
* @author wanggenping
*
*/
public class MyViewPager extends ViewPager{
private View mRight;
private View mLeft;
private float mTrans;
private float mScale;
private static final float MIN_SCALE = 0.5f;
private Map<Integer, View> mChildren = new HashMap<Integer, View>();
/**
* @param context
* @param attrs
*/
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onPageScrolled(int position, float offset, int offsetPixels) {
mLeft = mChildren.get(position);
mRight = mChildren.get(position + 1);
animStack(mLeft,mRight,offset,offsetPixels);
super.onPageScrolled(position, offset, offsetPixels);
}
/**
* @param left
* @param right
* @param offset
* @param offsetPixels
*/
private void animStack(View left, View right, float offset,
int offsetPixels) {
if(right != null){
//offset 0 ~ 1
mScale = (1-MIN_SCALE)*offset + MIN_SCALE;
mTrans = -getWidth() - getPageMargin() + offsetPixels;
ViewHelper.setScaleX(right, mScale);
ViewHelper.setScaleY(right, mScale);
ViewHelper.setTranslationX(right, mTrans);
}
if(left != null){
left.bringToFront();
}
}
public void setViewForPosition(View view,int position){
mChildren.put(position, view);
}
public void removeViewFromPosition(Integer position){
mChildren.remove(position);
}
}
ViewPagerActivity.class
/**
*
*/
package com.test.sildingmenu;
import java.util.ArrayList;
import java.util.List;
import com.test.sildingmenu.view.CustomViewPager;
import com.test.sildingmenu.view.DepthPageTransformer;
import com.test.sildingmenu.view.MyViewPager;
import com.test.sildingmenu.view.RotateDownPagerTransformer;
import com.test.sildingmenu.view.ZoomOutPageTransformer;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
/**
* @author wanggenping
*
*/
public class ViewPagerActivity extends Activity{
private MyViewPager viewPager;
private int[] images = new int[]{R.drawable.guide_image1,R.drawable.guide_image2,R.drawable.guide_image3};
private List<ImageView> imageList = new ArrayList<ImageView>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager2);
viewPager = (MyViewPager) findViewById(R.id.activity_view_pager_viewpager);
//为viewpager添加动画效果
// viewPager.setPageTransformer(true, new DepthPageTransformer());
// viewPager.setPageTransformer(true, new ZoomOutPageTransformer());
// viewPager.setPageTransformer(true, new RotateDownPagerTransformer());
viewPager.setAdapter(new PagerAdapter() {
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0.equals(arg1);
}
@Override
public int getCount() {
return images.length;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(ViewPagerActivity.this);
imageView.setImageResource(images[position]);
imageView.setScaleType(ScaleType.CENTER_CROP);//防止变形
container.addView(imageView);
imageList.add(imageView);
viewPager.setViewForPosition(imageView, position);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
container.removeView(imageList.get(position));
viewPager.removeViewFromPosition(position);
}
});
}
}
activity_view_pager2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.test.sildingmenu.view.MyViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_view_pager_viewpager"
/>
</LinearLayout>