android viewPager滑動速度設定
阿新 • • 發佈:2019-02-20
ViewPager 滑動速度設定,並實現點選按鈕滑動
使用過ViewPager的童鞋,都會感覺到設定介面滑動挺簡單的。但是有時候卻滿足不了UI設計的要求。
在用這個ViewPager的時候我遇到兩個問題,不知道你們遇到沒有。這裡做個筆記,總結一下:
第一個問題是,ViewPager在我們滑動放手後,速度和動畫的變化率是固定的。
第二個問題的,我們再新增左右按鈕後,如點選滑動到前一頁面(通過mViewPager.setCurrentItem(viewID, true);),一閃就了,使用者感覺不到動畫效果。
其實這兩個問題的的根源都是一樣的。我們能都改變速度和動畫的變化率,那麼就可以解決了。
還是寫個簡單的demo,先看效果圖:
右邊這張是滑動中的。滑動放手後,動畫的變化率設為加速度,也就是先慢後快。這裡改變了Interpolator。
每按下向左鍵,滑動速度會快0.1秒。每按下向右鍵,滑動速度會慢0.1秒。 所以你不停的按向右鍵,那麼將會變得很慢很慢。
佈局main.xml:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width
- 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
- 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類:
- package blog.csdn.net.liyulei316686082;
- import java.lang.reflect.Field;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.os.Bundle;
- import android.support.v4.view.ViewPager;
- import android.support.v4.view.ViewPager.OnPageChangeListener;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.animation.AccelerateInterpolator;
- import android.widget.Button;
- public class ViewPagerDemoActivity extends Activity {
- /** Called when the activity is first created. */
- private ViewPager mViewPager = null;
- private Button mLeft = null;
- private Button mRight = null;
- private static final int PAGER_NUM = 10;//10個頁面
- private int mCurrentViewID = 0; //當前頁面
- private int mMyDuration = 100; //持續時間
- private FixedSpeedScroller mScroller;
- private YLeiPageAdapter mYLeiPageAdapter = null;
- private List<View> mListViews;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mViewPager = (ViewPager)findViewById(R.id.viewpager);
- mLeft = (Button)findViewById(R.id.left);
- mRight = (Button)findViewById(R.id.right);
- mLeft.setOnClickListener(mOnClickListener);
- mRight.setOnClickListener(mOnClickListener);
- mListViews = new ArrayList<View>();
- for(int i= 1; i<= PAGER_NUM; i++){
- MyPagerView view = new MyPagerView(this, i);
- mListViews.add(view);
- }
- mYLeiPageAdapter = new YLeiPageAdapter(this, mListViews);
- mViewPager.setAdapter(mYLeiPageAdapter);
- mViewPager.setOnPageChangeListener(mOnPageChangeListener);
- /*<spanstyle="color:#ff0000;">主要程式碼段</span>*/
- try {
- Field mField = ViewPager.class.getDeclaredField("mScroller");
- mField.setAccessible(true);
- //<spanstyle="color:#ff0000;">設定加速度 ,通過改變FixedSpeedScroller這個類中的mDuration來改變動畫時間(如mScroller.setmDuration(mMyDuration);)
- </span>mScroller = new FixedSpeedScroller(mViewPager.getContext(), new AccelerateInterpolator());
- mField.set(mViewPager, mScroller);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private OnClickListener mOnClickListener = new OnClickListener(){
- @Override
- public void onClick(View v) {
- switch (v.getId()){
- case R.id.left:
- if(mCurrentViewID != 0){
- mCurrentViewID--;
- mViewPager.setCurrentItem(mCurrentViewID, true);
- }
- mMyDuration -= 100;
- mScroller.setmDuration(mMyDuration);
- break;
- case R.id.right:
- if(mCurrentViewID != PAGER_NUM-1){
- mCurrentViewID++;
- mViewPager.setCurrentItem(mCurrentViewID, true);
- }
- mMyDuration += 100;
- mScroller.setmDuration(mMyDuration);
- break;
- }
- }};
- private OnPageChangeListener mOnPageChangeListener = new OnPageChangeListener(){
- @Override
- public void onPageScrollStateChanged(int arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onPageScrolled(int arg0, float arg1, int arg2) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onPageSelected(int currentID) {
- // TODO Auto-generated method stub
- mCurrentViewID = currentID;
- }};
- }
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;
- }
- }