1,设置页面切换动画
自定义类,并继承PageTransformer,下面举个例子,文章末有demo地址,该demo集成20余种动画供你选择
import android.support.v4.view.ViewPager;
import android.view.View;
public class DepthPageTransformer implements ViewPager.PageTransformer {
private static final float MIN_SCALE = 0.75f;
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
} else if (position <= 0) { // [-1,0]
// Use the default slide transition when moving to the left page
view.setAlpha(1);
view.setTranslationX(0);
view.setScaleX(1);
view.setScaleY(1);
} else if (position <= 1) { // (0,1]
// Fade the page out.
view.setAlpha(1 - position);
// Counteract the default slide transition
view.setTranslationX(pageWidth * -position);
// Scale the page down (between MIN_SCALE and 1)
float scaleFactor = MIN_SCALE
+ (1 - MIN_SCALE) * (1 - Math.abs(position));
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
}
viewpager.setPageTransformer(true,depthPageTransformer )就可以了
2,设置动画持续时长
viewpager的setCurrentItem 会调用默认的250毫秒,也就是 Scroller的默认时间
先自定义一个scroller类,在这里通过反射更改viewpager的Scroller对象,
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.view.animation.Interpolator;
import android.widget.Scroller;
import java.lang.reflect.Field;
public class ViewPagerScroller extends Scroller {
private int mDuration = 2000;/*default duration time*/
/**
* Set custom duration time.
*
* @param duration duration
*/
public void setScrollDuration(int duration) {
mDuration = duration;
}
/**
* Get duration time.
*
* @return duration
*/
public int getmDuration() {
return mDuration;
}
public ViewPagerScroller(Context context) {
super(context);
}
public ViewPagerScroller(Context context, Interpolator interpolator) {
super(context, interpolator);
}
public ViewPagerScroller(Context context, Interpolator interpolator, boolean flywheel) {
super(context, interpolator, flywheel);
}
@Override
public void startScroll(int startX, int startY, int dx, int dy) {
super.startScroll(startX, startY, dx, dy, mDuration);
}
@Override
public void startScroll(int startX, int startY, int dx, int dy, int duration) {
super.startScroll(startX, startY, dx, dy, mDuration);
}
public void initViewPagerScroll(ViewPager pager) {
try {
Field field = ViewPager.class.getDeclaredField("mScroller");
field.setAccessible(true);
field.set(pager, this);
} catch (Exception e) {
e.printStackTrace();
}
}
}
调用initViewPagerScroll(ViewPager pager)将设置ViewPager 的页面切换动画时长
3,设置自动播放间隔时常 利用 延时hander完成,isScroll作为开始和停止自动播放的控制标记位
int autoScrollTime=2000;//自动播放间隔时常
boolean isScroll=true;
Handler handler=new Handler(this.getContext().getMainLooper());
Runnable autoRun=new Runnable() {
@Override
public void run() {
if(isScroll) {
int position = viewPager.getCurrentItem() + 1;
viewPager.setCurrentItem(position, true);
handler.postDelayed(this, autoScrollTime);
}
}
};
public void startOrStopScroll(boolean isScroll){
this.isScroll=isScroll;
if(isScroll()){
handler.postDelayed(autoRun,autoScrollTime);
}
}
写了一个能无限往后播放的viewpager完整demo地址,不想看这零碎的文章,可以下载demo直接干代码,集成20余种动画供你选择
https://download.csdn.net/download/t7g367/12517959