ViewPager 滑动速度设置,并实现点击按钮滑动
使用过ViewPager的童鞋,都会感觉到设置界面滑动挺简单的。但是有时候却满足不了UI设计的要求。
在用这个ViewPager的时候我遇到两个问题,不知道你们遇到没有。这里做个笔记,总结一下:
第一个问题是,ViewPager在我们滑动放手后,速度和动画的变化率是固定的。
第二个问题的,我们再添加左右按钮后,如点击滑动到前一页面(通过mViewPager.setCurrentItem(viewID, true);),一闪就了,用户感觉不到动画效果。
其实这两个问题的的根源都是一样的。我们能都改变速度和动画的变化率,那么就可以解决了。
还是写个简单的demo,先看效果图:

右边这张是滑动中的。滑动放手后,动画的变化率设为加速度,也就是先慢后快。这里改变了Interpolator。
每按下向左键,滑动速度会快0.1秒。每按下向右键,滑动速度会慢0.1秒。 所以你不停的按向右键,那么将会变得很慢很慢。
布局main.xml:
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
android:orientation="vertical" >
-
-
<android.support.v4.view.ViewPager
-
android:id="@+id/viewpager"
-
android:layout_width="fill_parent"
-
android:layout_height="400dip" />
-
-
<LinearLayout
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:orientation="horizontal" >
-
<Button
-
android:id="@+id/left"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="←"
-
android:textSize="25dip" />
-
<Button
-
android:id="@+id/right"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_marginRight="100dip"
-
android:text="→"
-
android:textSize="25dip" />
-
</LinearLayout>
-
-
</LinearLayout>
ViewPagerDemoActivity类:
FixedSpeedScroller类:
-
package blog.csdn.net.liyulei316686082;
-
-
import android.content.Context;
-
import android.view.animation.Interpolator;
-
import android.widget.Scroller;
-
-
public class FixedSpeedScroller extends Scroller {
-
private int mDuration = 1500;
-
public FixedSpeedScroller(Context context) {
-
super(context);
-
}
-
public FixedSpeedScroller(Context context, Interpolator interpolator) {
-
super(context, interpolator);
-
}
-
-
@Override
-
public void startScroll(int startX, int startY, int dx, int dy, int duration) {
-
// Ignore received duration, use fixed one instead
-
super.startScroll(startX, startY, dx, dy, mDuration);
-
}
-
@Override
-
public void startScroll(int startX, int startY, int dx, int dy) {
-
// Ignore received duration, use fixed one instead
-
super.startScroll(startX, startY, dx, dy, mDuration);
-
}
-
public void setmDuration(int time){
-
mDuration = time;
-
}
-
public int getmDuration(){
-
return mDuration;
-
}
-
-
}
android viewPager滑动速度设置
原文:http://blog.csdn.net/love_xsq/article/details/44703089