安卓学习笔记之自定义ViewGroup

4 篇文章 0 订阅
3 篇文章 0 订阅

涉及的知识点

 - ViewGroup的测量与布局
 - View的测量与布局
 -  滑动冲突的处理
 - VelocityTracker滑动速率跟踪
 - Scroller实现弹性滑动
 - 屏幕宽高的获取等

实现步骤

 1. 创建MyScrollView继承ViewGroup,实现构造与方法
 2.  在onMeasure方法中对子View进行测量,同时计算出ViewGroup的宽高,并通过setMeasuredDimension设置
 3. 在onLayout方法中进行布局
 4. 在onInterceptTouchEvent方法中进行滑动冲突的处理
 5. 在onTouchEvent方法中进行滑动事件的处理
 6. 实现弹性滑动

具体实现

继承ViewGroup
public class MyScrollView extends ViewGroup {
    /**
     * 可视为点击事件的距离,视为滑动的临界值
     */
    private int touchSlop;
    private Scroller mScroller;
    /**
     * 屏幕宽度
     */
    private int screenHeight;
    /**
     * 滑动速度跟踪类
     */
    private VelocityTracker velocityTracker;
    /**
     * 滑动的起始坐标
     */
    private int startX, startY;
    /**
     * getScrollY的起始与结束值
     */
    private int startScrollY, endScrollY;


    public MyScrollView(Context context) {
        this(context, null);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        mScroller = new Scroller(context, new OvershootInterpolator());  // 添加Interpolator
        screenHeight = getScreenHeight(context);
    }
 /**
     * 获取屏幕高
     */
    private int getScreenHeight(Context context) {
        Display d = ((Activity) context).getWindowManager().getDefaultDisplay();
        Point outSize = new Point();
        d.getSize(outSize);  // outSize保存着屏幕的宽高
        return outSize.y;

    }
onMeasure中进行测量

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int measureHeight = measureHeight(heightMeasureSpec);  // 通过模式测量的高度
        final int measureWidth = measureWidth(widthMeasureSpec); // 通过模式测量的宽度
        int totalHeight = 0;  // 实际的总高度
        int maxWidth = 0;  // 实际的最大宽度
        measureChildren(widthMeasureSpec, heightMeasureSpec);  //测量子view

        int childCount = getChildCount();  //子view数量

        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            int height = child.getMeasuredHeight();
            int width = child.getMeasuredWidth();
            totalHeight += height;
            maxWidth = Math.max(maxWidth, child.getMeasuredWidth() + child.getPaddingLeft() + child.getPaddingRight());

        }
        //比较出最大宽度与最大高度,并设置给MyScrollView
        setMeasuredDimension(Math.max(measureWidth, maxWidth), Math.max(measureHeight, totalHeight));
    }

    /**
     * 通过widthMeasureSpec得到的宽度
     *
     * @param widthMeasureSpec
     * @return
     */
    private int measureWidth(int widthMeasureSpec) {
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);
        int finalSize = 200;  //给定默认值
        switch (mode) {
            case MeasureSpec.EXACTLY:
                finalSize = specSize;
                break;
            case MeasureSpec.AT_MOST:
                finalSize = Math.min(finalSize, specSize);
                break;
        }
        return finalSize;
    }

    /**
     * 通过heightMeasureSpec得到的高度
     *
     * @param heightMeasureSpec
     * @return
     */
    private int measureHeight(int heightMeasureSpec) {
        int mode = MeasureSpec.getMode(heightMeasureSpec);
        int specSize = MeasureSpec.getSize(heightMeasureSpec);
        int finalSize = 200;
        switch (mode) {
            case MeasureSpec.EXACTLY:
                finalSize = specSize;
                break;
            case MeasureSpec.AT_MOST:
                finalSize = Math.min(finalSize, specSize);
                break;
        }
        return finalSize;
    }
onLayout方法中进行布局
   @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
//        int paddingTop = 0;
//        for (int i = 0; i < childCount; i++) {
//            View child = getChildAt(i);
//            int height = child.getMeasuredHeight();
//            int childLeft = l + child.getPaddingLeft();
//            child.layout(childLeft, paddingTop, childLeft + child.getMeasuredWidth(), paddingTop + child.getMeasuredHeight());
//            paddingTop += height;
//            lastChildY = paddingTop;
//        }
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            // 这里让每一个child都填充了屏幕
            child.layout(l, screenHeight * i, r, screenHeight * (i + 1));
        }
    }
onInterceptTouchEvent滑动冲突处理

    /**
     * 滑动冲突处理,
     * 当Y方向滑动大于X方向滑动距离,并且Y方向滑动距离大于touchSlop时,拦截事件
     * 其他情况不拦截
     *
     * @param ev
     * @return
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean result = false;
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = (int) ev.getX();
                startY = (int) ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                int dX = (int) (ev.getX() - startX);
                int dY = (int) (ev.getY() - startY);
                if (Math.abs(dY) > Math.abs(dX) && Math.abs(dY) > touchSlop) {  //y方向大于x方法距离,且y方向滑动大于touchSlop
                    result = true;
                } else {
                    result = false;
                }
                break;

        }

        return result;
    }
onTouchEvent滑动事件的处理
 @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (velocityTracker == null) {
                    velocityTracker = VelocityTracker.obtain();
                } else {
                    velocityTracker.clear();  // 重置到初始状态
                }
                velocityTracker.addMovement(ev);  // You should call this for the initial ACTION_DOWN
                if (!mScroller.isFinished())
                    mScroller.abortAnimation();
                startX = (int) ev.getX();
                startY = (int) ev.getY();
                startScrollY = getScrollY();
                break;
            case MotionEvent.ACTION_MOVE:
                velocityTracker.addMovement(ev); // Add a user's movement to the tracker.
                velocityTracker.computeCurrentVelocity(50); // 计算当前速率,按每50毫秒
                int dX = (int) (ev.getX() - startX);
                int dY = (int) (ev.getY() - startY);
                if (Math.abs(getScrollY()) > screenHeight * (getChildCount() - 1) && dY < 0) {  // 滑动边界限制
                    return false;
                }
                scrollBy(0, -dY);
                break;
            case MotionEvent.ACTION_UP:
                endScrollY = getScrollY();
                int deltaScrollY = endScrollY - startScrollY;
                int page = Math.abs(endScrollY / screenHeight); // 当前页
                if (Math.abs(velocityTracker.getYVelocity()) > 100 && page != getChildCount() - 1) {    //滑动速率大于100
                    if (velocityTracker.getYVelocity() > 100) {  //下滑
                        smoothScroll(0, getScrollY(), 0, -getScrollY() + page * screenHeight, 500);  // 上一页
                    } else if (-velocityTracker.getYVelocity() > 100) {  // 上滑
                        smoothScroll(0, getScrollY(), 0, -getScrollY() + (page + 1) * screenHeight, 500); // 下一页
                    }
                } else {
                    if (deltaScrollY < 0) {   //  下拉
                        if (Math.abs(deltaScrollY) < screenHeight / 3) {    //小于屏幕1/3时,回滚
                            smoothScroll(0, getScrollY(), 0, -deltaScrollY, deltaScrollY * 2);
                        } else {  //  自动滑动到下一页
                            smoothScroll(0, getScrollY(), 0, -getScrollY() + page * screenHeight, deltaScrollY * 2);
                        }
                    } else {   //上拉
                        if (deltaScrollY > screenHeight / 3) {  //大于屏幕1/3时,自动滑动到下一页
                            smoothScroll(0, getScrollY(), 0, -getScrollY() + (page + 1) * screenHeight, deltaScrollY * 2);
                        } else {  //回滚
                            smoothScroll(0, getScrollY(), 0, -deltaScrollY, deltaScrollY * 2);
                        }
                    }
                }

                break;
        }
        startX = (int) ev.getX();
        startY = (int) ev.getY();
        return true;
    }
弹性滑动的实现
/**
     * 弹性滑动
     * @param startX
     * @param startY
     * @param dX
     * @param dY
     * @param duration
     */
    private void smoothScroll(int startX, int startY, int dX, int dY, int duration) {
        if (Math.abs(duration) > 800) duration = 800;
        mScroller.startScroll(startX, startY, dX, dY, Math.abs(duration));
        invalidate();
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            invalidate();
        }
    }

源码: http://download.csdn.net/detail/qq_28261343/9600431

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值