代码如下 | 复制代码 |
publicclassMyScrollViewextendsScrollView { privateView childView; publicMyScrollView(Context context) { super(context); } publicMyScrollView(Context context, AttributeSet attrs) { super(context, attrs); } publicMyScrollView(Context context, AttributeSet attrs,intdefStyleAttr) { super(context, attrs, defStyleAttr); } // @Override // protected void onLayout(boolean changed, int l, int t, int r, int b) { // super.onLayout(changed, l, t, r, b); // } //获取子视图 @Override protectedvoidonFinishInflate() { super.onFinishInflate(); if(getChildCount() >0) { childView = getChildAt(0); } } privateintlastY;//上一次y轴方向操作的坐标位置 privateRect normal =newRect();//用于记录临界状态的左、上、右、下 privatebooleanisFinishAnimation =true;//是否动画结束 privateintlastX, downX, downY; //拦截:实现父视图对子视图的拦截 //是否拦截成功,取决于方法的返回值。返回值true:拦截成功。反之,拦截失败 @Override publicbooleanonInterceptTouchEvent(MotionEvent ev) { booleanisIntercept =false; inteventX = (int) ev.getX(); inteventY = (int) ev.getY(); switch(ev.getAction()) { caseMotionEvent.ACTION_DOWN: lastX = downX = eventX; lastY = downY = eventY; break; caseMotionEvent.ACTION_MOVE: //获取水平和垂直方向的移动距离 intabsX = Math.abs(eventX - downX); intabsY = Math.abs(eventY - downY); if(absY > absX && absY >= UIUtils.dp2px(10)){ isIntercept =true;//执行拦截 } lastX = eventX; lastY = eventY; break; } returnisIntercept; } @Override publicbooleanonTouchEvent(MotionEvent ev) { if(childView ==null|| !isFinishAnimation) { returnsuper.onTouchEvent(ev); } inteventY = (int) ev.getY();//获取当前的y轴坐标 switch(ev.getAction()) { caseMotionEvent.ACTION_DOWN: lastY = eventY; break; caseMotionEvent.ACTION_MOVE: intdy = eventY - lastY;//微小的移动量 if(isNeedMove()) { if(normal.isEmpty()) { //记录了childView的临界状态的左、上、右、下 normal.set(childView.getLeft(), childView.getTop(), childView.getRight(), childView.getBottom()); } //重新布局 childView.layout(childView.getLeft(), childView.getTop() + dy /2, childView.getRight(), childView.getBottom() + dy /2); } lastY = eventY;//重新赋值 break; caseMotionEvent.ACTION_UP: if(isNeedAnimation()) { //使用平移动画 inttranslateY = childView.getBottom() - normal.bottom; TranslateAnimation translateAnimation =newTranslateAnimation(0,0,0, -translateY); translateAnimation.setDuration(200); // translateAnimation.setFillAfter(true);//停留在最终位置上 translateAnimation.setAnimationListener(newAnimation.AnimationListener() { @Override publicvoidonAnimationStart(Animation animation) { isFinishAnimation =false; } @Override publicvoidonAnimationEnd(Animation animation) { isFinishAnimation =true; childView.clearAnimation();//清除动画 //重新布局 childView.layout(normal.left, normal.top, normal.right, normal.bottom); //清除normal的数据 normal.setEmpty(); } @Override publicvoidonAnimationRepeat(Animation animation) { } }); //启动动画 childView.startAnimation(translateAnimation); } break; } returnsuper.onTouchEvent(ev); } //判断是否需要执行平移动画 privatebooleanisNeedAnimation() { return!normal.isEmpty(); } privatebooleanisNeedMove() { intchildMeasuredHeight = childView.getMeasuredHeight();//获取子视图的高度 intscrollViewMeasuredHeight =this.getMeasuredHeight();//获取布局的高度 Log.e("TAG","childMeasuredHeight = "+ childMeasuredHeight); Log.e("TAG","scrollViewMeasuredHeight = "+ scrollViewMeasuredHeight); intdy = childMeasuredHeight - scrollViewMeasuredHeight;//dy >= 0 intscrollY =this.getScrollY();//获取用户在y轴方向上的偏移量 (上 + 下 -) if(scrollY <=0|| scrolly="">= dy) { returntrue;//按照我们自定义的MyScrollView的方式处理 } //其他处在临界范围内的,返回false。即表示,仍按照ScrollView的方式处理 returnfalse; } } |
凡尔赛法环免广告版 最新版v1.69.1
下载天煞异种 安卓版v1.0.21
下载十三月 (Undecember)最新官方中文版v3.26.0200
下载我不是废柴无限99999钻999999金币游戏 不减反增版v1.2.16
下载我的女友是声优免广告 安卓版v1
我的女友是声优免广告是一款非常好玩的模拟经营类手游,玩家在游
石器帝国去广告版 v8
石器帝国免广告版是游戏的破解版本,在该版本中为玩家去除了广告
校园恋爱模拟器真人版去广告版 v0.0.7
校园恋爱模拟器真人版免广告版是游戏的破解版本,在该版本中为玩
教练我要开球馆内置菜单修改器 安卓版v1.5.4
教练我要开球馆内购版是一款模拟经营类游戏,玩家们将在游戏中创
我的游轮无限砖石 安卓版v1.7.0
我的游轮无限金币钻石版是一款模拟经营类游戏,你将成为一艘游轮