对Hongyang大神文章《Andoird 自定义ViewGroup实现竖向引导界面》的阅读笔记

项目中有这种页面需求,正好hongyang大神的文章比较吻合,就仔细阅读了一下。其中做了一些自己的理解性注释:

1、原文链接:http://blog.csdn.net/lmj623565791/article/details/23692439


2、

效果:



3、布局类的源码及注释:


public class VerticalLinearLayout extends ViewGroup
{
    /**
     * 屏幕的高度
     */
    private int mScreenHeight;
    /**
     * 手指按下时的getScrollY
     */
    private int mScrollStart;
    /**
     * 手指抬起时的getScrollY
     */
    private int mScrollEnd;
    /**
     * 记录移动时的Y
     */
    private int mLastY;
    /**
     * 滚动的辅助类
     */
    private Scroller mScroller;
    /**
     * 是否正在滚动
     */
    private boolean isScrolling;
    /**
     * 加速度检测
     */
    private VelocityTracker mVelocityTracker;
    /**
     * 记录当前页
     */
    private int currentPage = 0;

    private OnPageChangeListener mOnPageChangeListener;

    public VerticalLinearLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);

        /**
         * 获得屏幕的高度
         */
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        mScreenHeight = outMetrics.heightPixels;
        // 初始化
        mScroller = new Scroller(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int count = getChildCount();
        for (int i = 0; i < count; ++i)
        {
            View childView = getChildAt(i);
            measureChild(childView, widthMeasureSpec,mScreenHeight);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        if (changed)
        {
            int childCount = getChildCount();
            // 设置主布局的高度
            MarginLayoutParams lp = (MarginLayoutParams) getLayoutParams();
            lp.height = mScreenHeight * childCount;
            setLayoutParams(lp);

            for (int i = 0; i < childCount; i++)
            {
                View child = getChildAt(i);
                if (child.getVisibility() != View.GONE)
                {
                    child.layout(l, i * mScreenHeight, r, (i + 1) * mScreenHeight);// 调用每个自布局的layout
                }
            }

        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        // 如果当前正在滚动,调用父类的onTouchEvent
        if (isScrolling)
            return super.onTouchEvent(event);

        int action = event.getAction();
        //每个event发生,都会重新获取这个y值
        int y = (int) event.getY();

        obtainVelocity(event);
        switch (action)
        {
            case MotionEvent.ACTION_DOWN:

                mScrollStart = getScrollY();
                mLastY = y;
                break;
            case MotionEvent.ACTION_MOVE:

                if (!mScroller.isFinished())
                {
                    mScroller.abortAnimation();
                }

                //从手指按下到当前event,移动的距离,dy>0 代表手指上滑,dy<0 代表手指下滑
                int dy = mLastY - y;
                // 边界值检查,scrollY是一个与该页面展示初始状态有关的值,手指上滑的时候scrollY会增大,而且连续下拉会累积增大,
                // 当scrollY==0时就已经到了顶部,当scrollY==getHeight()-mScreenHeight时就已经到了底部
                int scrollY = getScrollY();
                // 已经到达顶部,下拉多少,就往上滚动多少
                if (dy < 0 && scrollY + dy < 0)
                {
                    dy = -scrollY;
                }
                // 已经到达底部,上拉多少,就往下滚动多少
                if (dy > 0 && scrollY + dy > getHeight() - mScreenHeight)
                {
                    dy = getHeight() - mScreenHeight - scrollY;
                }

                scrollBy(0, dy);
                mLastY = y;
                break;
            case MotionEvent.ACTION_UP:

                mScrollEnd = getScrollY();

                //本次手指移动的距离
                int dScrollY = mScrollEnd - mScrollStart;

                if (wantScrollToNext())// 手指往上滑动
                {
                    if (shouldScrollToNext())
                    {
                        mScroller.startScroll(0, getScrollY(), 0, mScreenHeight - dScrollY);

                    } else
                    {
                        mScroller.startScroll(0, getScrollY(), 0, -dScrollY);
                    }

                }

                if (wantScrollToPre())// 往下滑动
                {
                    if (shouldScrollToPre())
                    {
                        mScroller.startScroll(0, getScrollY(), 0, -mScreenHeight - dScrollY);

                    } else
                    {
                        mScroller.startScroll(0, getScrollY(), 0, -dScrollY);
                    }
                }
                isScrolling = true;
                postInvalidate();
                recycleVelocity();
                break;
        }

        return true;
    }

    /**
     * 根据滚动距离判断是否能够滚动到下一页
     * 条件是:滑动的距离大于屏幕高度的一半,或者滑动的加速度绝对值大于600
     *
     * @return
     */
    private boolean shouldScrollToNext()
    {
        return mScrollEnd - mScrollStart > mScreenHeight / 2 || Math.abs(getVelocity()) > 600;
    }

    /**
     * 根据用户滑动,判断用户的意图是否是滚动到下一页
     *
     * @return
     */
    private boolean wantScrollToNext()
    {
        return mScrollEnd > mScrollStart;
    }

    /**
     * 根据滚动距离判断是否能够滚动到上一页
     *
     * @return
     */
    private boolean shouldScrollToPre()
    {
        return -mScrollEnd + mScrollStart > mScreenHeight / 2 || Math.abs(getVelocity()) > 600;
    }

    /**
     * 根据用户滑动,判断用户的意图是否是滚动到上一页
     *
     * @return
     */
    private boolean wantScrollToPre()
    {
        return mScrollEnd < mScrollStart;
    }

    @Override
    public void computeScroll()
    {
        super.computeScroll();
        if (mScroller.computeScrollOffset())
        {
            scrollTo(0, mScroller.getCurrY());
            postInvalidate();
        } else
        {//滑动结束,判断页面是否change

            int position = getScrollY() / mScreenHeight;

            Log.e("xxx", position + "," + currentPage);
            if (position != currentPage)
            {
                if (mOnPageChangeListener != null)
                {
                    currentPage = position;
                    mOnPageChangeListener.onPageChange(currentPage);
                }
            }

            isScrolling = false;
        }

    }

    /**
     * 获取y方向的加速度
     *
     * @return
     */
    private int getVelocity()
    {
        mVelocityTracker.computeCurrentVelocity(1000);
        return (int) mVelocityTracker.getYVelocity();
    }

    /**
     * 释放资源
     */
    private void recycleVelocity()
    {
        if (mVelocityTracker != null)
        {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
    }

    /**
     * 初始化加速度检测器
     *
     * @param event
     */
    private void obtainVelocity(MotionEvent event)
    {
        if (mVelocityTracker == null)
        {
            mVelocityTracker = VelocityTracker.obtain();
        }
        mVelocityTracker.addMovement(event);
    }

    /**
     * 设置回调接口
     *
     * @param onPageChangeListener
     */
    public void setOnPageChangeListener(OnPageChangeListener onPageChangeListener)
    {
        mOnPageChangeListener = onPageChangeListener;
    }

    /**
     * 回调接口
     *
     * @author zhy
     *
     */
    public interface OnPageChangeListener
    {
        void onPageChange(int currentPage);
    }
}


4、布局文件:

  1. <com.example.verticallinearlayout.VerticalLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/id_main_ly"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical"  
  7.     android:background="#fff" >  
  8.   
  9.     <RelativeLayout  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="fill_parent"  
  12.         android:background="@drawable/w02" >  
  13.   
  14.         <Button  
  15.             android:layout_width="wrap_content"  
  16.             android:layout_height="wrap_content"  
  17.             android:text="hello" />  
  18.     </RelativeLayout>  
  19.   
  20.     <RelativeLayout  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="fill_parent"  
  23.         android:background="@drawable/w03" >  
  24.   
  25.         <Button  
  26.             android:layout_width="wrap_content"  
  27.             android:layout_height="wrap_content"  
  28.             android:layout_centerInParent="true"  
  29.             android:background="#fff"  
  30.             android:text="hello" />  
  31.     </RelativeLayout>  
  32.   
  33.     <RelativeLayout  
  34.         android:layout_width="fill_parent"  
  35.         android:layout_height="fill_parent"  
  36.         android:background="@drawable/w04" >  
  37.   
  38.         <Button  
  39.             android:layout_width="wrap_content"  
  40.             android:layout_height="wrap_content"  
  41.             android:layout_centerInParent="true"  
  42.             android:text="hello" />  
  43.     </RelativeLayout>  
  44.   
  45.     <RelativeLayout  
  46.         android:layout_width="fill_parent"  
  47.         android:layout_height="fill_parent"  
  48.         android:background="@drawable/w05" >  
  49.   
  50.         <Button  
  51.             android:layout_width="wrap_content"  
  52.             android:layout_height="wrap_content"  
  53.             android:layout_centerInParent="true"  
  54.             android:text="hello" />  
  55.     </RelativeLayout>  
  56.   
  57. </com.example.verticallinearlayout.VerticalLinearLayout>  
这个布局文件其实很简单,只是要注意,在自定义的 VerticalLinearLayout 类中,使用onMeasure和onLayout函数将其每个child的大小都设置成屏幕大小,并布局中将各个child高度拼接起来。


要注意的几点:

1、view的大小并不局限于手机屏幕大小,而是可以无限大的

2、view的内部的坐标与手机屏幕内的坐标有一个相对性,可通过getscrollX()和getscrollY()获取到view在屏幕边缘的坐标值,而初始值(0,0)是界面初始化是view在屏幕左和上边缘的位置。具体内容可参阅:http://www.xuebuyuan.com/2013505.html

3、源码中主要的两个方法是:onTouchEvent()和computeScroll()        (关于scroller辅助类的使用方法,在网上有很多资料,我之前的文章中也有提及)





  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值