热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

安卓学习笔记之自定义ViewGroup

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

涉及的知识点

- 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()); // 添加InterpolatorscreenHeight = 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中进行测量

@Overrideprotected 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); //测量子viewint childCount = getChildCount(); //子view数量for (int i = 0; i int height = child.getMeasuredHeight();int width = child.getMeasuredWidth();totalHeight += height;maxWidth = Math.max(maxWidth, child.getMeasuredWidth() + child.getPaddingLeft() + child.getPaddingRight());}//比较出最大宽度与最大高度,并设置给MyScrollViewsetMeasuredDimension(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方法中进行布局

@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {int childCount = getChildCount();
// int paddingTop = 0;
// for (int i = 0; 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 // 这里让每一个child都填充了屏幕
child.layout(l, screenHeight * i, r, screenHeight * (i + 1));}}

onInterceptTouchEvent滑动冲突处理

/*** 滑动冲突处理,* 当Y方向滑动大于X方向滑动距离,并且Y方向滑动距离大于touchSlop时,拦截事件* 其他情况不拦截** @param ev* @return*/@Overridepublic 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方向滑动大于touchSlopresult = true;} else {result = false;}break;}return result;}

onTouchEvent滑动事件的处理

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

弹性滑动的实现

/*** 弹性滑动* &#64;param startX* &#64;param startY* &#64;param dX* &#64;param dY* &#64;param duration*/private void smoothScroll(int startX, int startY, int dX, int dY, int duration) {if (Math.abs(duration) > 800) duration &#61; 800;mScroller.startScroll(startX, startY, dX, dY, Math.abs(duration));invalidate();}&#64;Overridepublic void computeScroll() {super.computeScroll();if (mScroller.computeScrollOffset()) {scrollTo(mScroller.getCurrX(), mScroller.getCurrY());invalidate();}}

源码&#xff1a; http://download.csdn.net/detail/qq_28261343/9600431


推荐阅读
author-avatar
手机用户2502887521
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有