自定义ViewPager和弹性圆PagerIndicator

本文介绍了如何实现一个自定义的SlidingViewPager,包含手势探测和滑动效果,以及弹性圆的PagerIndicator。SlidingViewPager通过LinearLayout扩展,处理down、move、up事件,实现平滑拖动和回弹效果。弹性圆PagerIndicator利用二次贝塞尔曲线和平移动画,实现圆的弹性形变和移动。文章详细讲解了代码设计思路和实现过程。
摘要由CSDN通过智能技术生成

首先来一张效果图
效果图
上方是一个类似于ViewPager的滑动控件(SlidingViewPager),下方则是一个指示器(CustomPagerIndicator),指示器是会出现一个弹性圆的平移.这两2个都是自定义View或者ViewGroup来实现的.

布局文件如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.gaoql.customview.SlidingViewPager
        android:id="@+id/slidingViewGroup"
        android:layout_width="match_parent"
        android:background="#363636"
        android:layout_above="@+id/customRelativeLayout"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/iv1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/ic_camera_enhance_black_24dp"
            android:clickable="true"
            />
        <ImageView
            android:id="@+id/iv2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/ic_cloud_black_24dp"
            android:clickable="true"
            />
        <ImageView
            android:id="@+id/iv3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/ic_assignment_black_24dp"
            android:clickable="true"
            />

    </com.gaoql.customview.SlidingViewPager>

    <com.gaoql.customview.CustomRelativeLayout
        android:id="@+id/customRelativeLayout"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#363636">

        <com.gaoql.customview.CustomPagerIndicator
            xmlns:circleButton="http://schemas.android.com/apk/res-auto"
            android:id="@+id/customviewgroup"
            android:orientation="horizontal"
            android:gravity="center_horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.gaoql.customview.CircleButton
                android:id="@+id/btn1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                circleButton:backgroundDrawable="@mipmap/ic_camera"
                circleButton:radius="40dp" />

            <com.gaoql.customview.CircleButton
                android:id="@+id/btn2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                circleButton:backgroundDrawable="@mipmap/ic_cloud"
                circleButton:radius="40dp" />

            <com.gaoql.customview.CircleButton
                android:id="@+id/btn3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                circleButton:backgroundDrawable="@mipmap/ic_setting"
                circleButton:radius="40dp" />
        </com.gaoql.customview.CustomPagerIndicator>
    </com.gaoql.customview.CustomRelativeLayout>
</RelativeLayout>

布局就是一个SlidingViewPager,和一个CustomPagerIndicator,还有CustomRelativeLayout(是干什么用的呢?下面再说)

一. SlidingViewPager

带有手势探测器GestureDetector和滑动Sroller的自定义ViewGroup,继承自LinearLayout

大体实现的效果
- 1 布局只需简单地水平排列
- 2 页面需要跟随手指移动而产生被拖拽的效果
- 3 拖动距离不足某一个距离(最小滑动距离,比如屏幕的三分之一)回弹,大于某一个距离则前进或后退一页
当然手指在页面上抛掷也要做到滑动下一页面或者上一页面

1-比较简单,onLayout中横向排列子View即可
2-拖拽可以利用srcollBy来做
3-需要比较2个触摸事件间的偏移量的差值和你自定义的最小滑动距离(和系统的最小滑动距离)比较再做处理

代码设计思路

主要对down,move,up事件的处理
1. down事件,记录当前的偏移量和触摸点x
2. move事件,主要做拖拽,那么就是求2次触摸点的x坐标差值,然后scrollBy
3. up事件,手指松开,当手指松开不足最小滑动距离回弹,满足且大于则前进或者后退

代码实现

下面是具体的SlidingViewPager实现
- 布局和测量
scrollTo(currentPageIndex*mWidth, 0); 这里直接展示出第一页的内容

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureChildren(widthMeasureSpec,heightMeasureSpec);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
        scrollTo(currentPageIndex*mWidth, 0);
    }
  • 事件分发
@Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        float x = ev.getX();
        float y = ev.getY();
        View childView = getChildAt(0);
        childViewWidth = childView.getRight()-childView.getLeft();
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                offsetX=getScrollX();
                mDownX = x;
                mDownY = y;
                break;
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
                if (!isCanSliding) {
                    isCanSliding = isCanSliding(ev);
                }
                break;
            default:
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

dispatchTouchEvent 中主要判断isCanSliding 是否可以去滑动,

isCanSliding 的判断逻辑如下

private boolean isCanSliding(MotionEvent ev){
        float currentX = ev.getX();
        float currentY = ev.getY();
        if(Math.abs(currentX-mDownX) > Math.abs(currentY-mDownY) && Math.abs(currentX-mDownX)>minScrollDistance){
            //X方向的距离大于y的滑动距离 && x方向的滑动距离大于系统最短滑动距离,认为是水平滑动
            return true;
        }
        return false;
    }

然后在onInterceptTouchEvent中return isCanSliding ,想法是为了处理当在垂直滑动的控件,比如RecycleView,使用该组件的时候出现的滑动冲突,可以试试看.

 @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if(getIndicator()!=null&&getIndicator().getTranslateState()==CustomPagerIndicator.STATE_MOVING){
            //发现正在滑动的,调用父类的方法分发掉该事件,不再拦截
            return false;
        }
        return  isCanSliding;
    }

isCanSliding=true ,就被拦截掉啦,来到了onTouchEvent 执行滑动的代码.当false就把事件给childView

@Override
    public boolean onTouchEvent(MotionEvent event) {
        obtainVelocityTracker(event);
        float x = event.getX();
        int action = event.getAction();
        switch (action){
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                dx = x - mDownX;
                mDownX = x;
                if (currentPageIndex == 0 && dx > 0 || currentPageIndex == getChildCount() - 1 && dx < 0) {
                    break;
                }
                scrollBy((int) -dx, 0);
                break;
            case MotionEvent.ACTION_UP:
                if(isCanSliding) {
                    int scrollX = getScrollX();
                    int delta = scrollX - offsetX;
                    //计算出一秒移动1000像素的速率 1000 表示每秒多少像素(pix/second),1代表每微秒多少像素(pix/millisecond)
                    velocityTracker.computeCurrentVelocity(1000, configuration.getScaledMaximumFlingVelocity());
                    float velocityX = velocityTracker.getXVelocity();
                    float velocityY = velocityTracker.getYVelocity();
                    if (Math.abs(delta) < childViewWidth / 3) {
                        // 小于三分之一,弹回去
                        Log.e(TAG, "onTouchEvent ACTION_UP back 1  ");
                        state = State.None;
                        requestUpdateState(state,delta);
                    } else if (Math.abs(velocityX) <= configuration.getScaledMinimumFlingVelocity() && Math.abs(velocityY) <= configuration.getScaledMinimumFlingVelocity()) {
                        //当速度小于系统速度,但过了三分一的距离,此时应该滑动一页
                        Log.e(TAG, "onTouchEvent ACTION_UP back 2  ");
                        if (delta > 0) { //左滑趋势
                            Log.e(TAG,"onTouchEvent page index 2-1 -- "+currentPageIndex);
                            if (currentPageIndex >=0 ) {
                                Log.e(TAG, "onTouchEvent ACTION_UP back 2-1  ");
                                state = State.ToNext;
                            }
                        } else {
  //右滑趋势
                            Log.e(TAG,"onTouchEvent page index 2-2 -- "+currentPageIndex);
                            if (currentPageIndex < getChildCount()) {
                                Log.e(TAG, "onTouchEvent ACTION_UP back 2-2  ");
                                state = State.ToPre;
                            }
                        }
                        Log.e(TAG,"requestUpdateState 1 "+state);
                        requestUpdateState(state,delta);
                        Log.e(TAG,"requestUpdateState 1 addChildViewCenterPointToIndicator "+currentPageIndex);
                        addChildViewCenterPointToIndicator(currentPageIndex);
                        Log.i(TAG,"startIndicatorCircleMoving 1");
                        startIndicatorCircleMoving();
                    }

                }
                realseVelocityTracker();
                break;
            default:
                break;

        }
        return mGestureDetector.onTouchEvent(event) ;
    }

关于翻页的状态State,是个枚举类,ToPre–上一页,ToNext–下一页,None–无状态,用于滑动距离不足时回弹的

public  enum State {
        ToPre,ToNext,None;
    }
private voi
一个轻量级的viewpager指示器 ,类似于nexus5 启动器的效果。它可以自定义指示器上小点的样式和动画效果。可以用于引导页。项目地址:https://github.com/ongakuer/CircleIndicator 效果图:如何使用1. xml布局中创建CircleIndicator是配合ViewPager使用的,一般如下布局:<RelativeLayout         android:layout_width="match_parent"         android:layout_height="match_parent">         <android.support.v4.view.ViewPager             android:id="@ id/viewpager_default"             android:layout_width="match_parent"             android:layout_height="match_parent"/>         <me.relex.circleindicator.CircleIndicator             android:id="@ id/indicator_default"             android:layout_centerInParent="true"             android:layout_width="fill_parent"             android:layout_height="40dp"/>     </RelativeLayout>2. java代码中// DEFAULT ViewPager defaultViewpager = (ViewPager) findViewById(R.id.viewpager_default); CircleIndicator defaultIndicator = (CircleIndicator) findViewById(R.id.indicator_default); DemoPagerAdapter defaultPagerAdapter = new DemoPagerAdapter(getSupportFragmentManager()); defaultViewpager.setAdapter(defaultPagerAdapter);//为ViewPager设置适配器 defaultIndicator.setViewPager(defaultViewpager);//将ViewPager添加到CircleIndicator中,以便监听ViewPager的滚动等事件DemoPagerAdapter是ViewPager的适配器,这个需要你去实现。属性说明属性名称类型说明ci_widthdimension小点的宽度ci_heightdimension小点的高度ci_margindimension小点间的间距ci_animatorreferenceViewPager页面切换时,给小点设置动画。设置当前的小点恢复到未选中时的状态的动画。例如:当前正从a切换到b,那么该属性设置的是a的动画。ci_animator_reversereference和"ci_animator"属性一样,只不过该属性设置的是b的动画。ci_drawablereference设置当前或选中的小点的样式,如颜色、角等。ci_drawable_unselectedreference和"ci_drawable"一样,只是该属性设置的是未选中的小点的样式。点击上面的"下载源码"下载完整的demo工程。demo简要说明xml布局:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical">     <RelativeLayout         android:layout_width="match_parent"         android:layout_height="0dp"         android:layout_weight="1">         <android.support.v4.view.ViewPager             android:id="@ id/viewpager_default"             android:layout_width="match_parent"             android:layout_height="match_parent"/>         <me.relex.circleindicator.CircleIndicator             android:id="@ id/indicator_default"             android:layout_centerInParent="true"             android:layout_width="fill_parent"             android:layout_height="40dp"/>     </RelativeLayout>     <RelativeLayout         android:layout_width="match_parent"         android:layout_height="0dp"         android:layout_weight="1">         <android.support.v4.view.ViewPager             android:id="@ id/viewpager_custom"             android:layout_width="match_parent"             android:layout_height="match_parent"/>         <me.relex.circleindicator.CircleIndicator             android:id="@ id/indicator_custom"             app:ci_width="10dp"             app:ci_height="4dp"             app:ci_margin="6dp"             app:ci_animator="@animator/indicator_animator"             app:ci_animator_reverse="@animator/indicator_animator_reverse"             app:ci_drawable="@drawable/black_radius_square"             android:layout_centerInParent="true"             android:layout_width="fill_parent"             android:layout_height="40dp"/>     </RelativeLayout>     <RelativeLayout         android:layout_width="match_parent"         android:layout_height="0dp"         android:layout_weight="1">         <android.support.v4.view.ViewPager             android:id="@ id/viewpager_unselected_background"             android:layout_width="match_parent"             android:layout_height="match_parent"/>         <me.relex.circleindicator.CircleIndicator             android:id="@ id/indicator_unselected_background"             android:layout_centerInParent="true"             android:layout_width="fill_parent"             app:ci_width="6dp"             app:ci_height="6dp"             app:ci_animator="@animator/indicator_no_animator"             app:ci_drawable="@drawable/white_radius"             app:ci_drawable_unselected="@drawable/black_radius"             android:layout_height="40dp"             />     </RelativeLayout> </LinearLayout>定义了3组ViewPager和指示器。第一组是采用默认样式的,被选中的小点带有放大的动画。第2组比较全,既定义了动画又修改了样式。第3组是取消动画的同时使用了ci_drawable_unselected属性。适配器Adapter:public class DemoPagerAdapter extends FragmentPagerAdapter {         private int pagerCount = 5;         private Random random = new Random();         public DemoPagerAdapter(FragmentManager fm) {             super(fm);         }         @Override public Fragment getItem(int i) {             return ColorFragment.newInstance(0xff000000 | random.nextInt(0x00ffffff));         }         @Override public int getCount() {             return pagerCount;         }     }定义了5个页面或Fragment,通过ColorFragment生成Fragment。Fragment:public class ColorFragment extends Fragment {     private static final String ARG_COLOR = "color";     private int mColor;     public static ColorFragment newInstance(int param1) {         ColorFragment fragment = new ColorFragment();         Bundle args = new Bundle();         args.putInt(ARG_COLOR, param1);         fragment.setArguments(args);         return fragment;     }     public ColorFragment() {     }     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         if (getArguments() != null) {             mColor = getArguments().getInt(ARG_COLOR);         }     }     @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container,             Bundle savedInstanceState) {         View v = inflater.inflate(R.layout.color_fragment, container, false);         v.setBackgroundColor(mColor);         return v;     } }生成空的页面,没有任何控件,只是设置了页面的背景色。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值