android實(shí)現(xiàn)可上下回彈的scrollview
在ios手機(jī)上經(jīng)??吹巾撁嫔舷禄瑒踊貜椥Ч?,安卓中沒有原生控件支持,這里自己就去自定義一個scrollview實(shí)現(xiàn)回彈效果
1. 新建MyScrollView并繼承ScrollView,可以通過事件分發(fā)機(jī)制攔截并處理滑動事件
2. 重寫事件分發(fā)攔截事件onInterceptTouchEvent方法,計算是否需要攔截事件
//攔截:實(shí)現(xiàn)父視圖對子視圖的攔截 //是否攔截成功,取決于方法的返回值。返回值true:攔截成功。反之,攔截失敗 private int lastY;//上一次y軸方向操作的坐標(biāo)位置 ? ? private Rect normal = new Rect();//用于記錄臨界狀態(tài)的左、上、右、下 ? ? private boolean isFinishAnimation = true;//是否動畫結(jié)束 ? ? private int lastX, downX, downY; ? ? @Override ? ? public boolean onInterceptTouchEvent(MotionEvent ev) { ? ? ? ? boolean isIntercept = false; ? ? ? ? int eventX = (int) ev.getX(); ? ? ? ? int eventY = (int) ev.getY(); ? ? ? ? switch (ev.getAction()) { ? ? ? ? ? ? case MotionEvent.ACTION_DOWN: ? ? ? ? ? ? ? ? lastX = downX = eventX; ? ? ? ? ? ? ? ? lastY = downY = eventY; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_MOVE: ? ? ? ? ? ? ? ? //獲取水平和垂直方向的移動距離 ? ? ? ? ? ? ? ? int absX = Math.abs(eventX - downX); ? ? ? ? ? ? ? ? int absY = Math.abs(eventY - downY); ? ? ? ? ? ? ? ? if(absY > absX && absY >= dp2px(10)){ ? ? ? ? ? ? ? ? ? ? isIntercept = true;//執(zhí)行攔截 ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? lastX = eventX; ? ? ? ? ? ? ? ? lastY = eventY; ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? return isIntercept; ? ? }
3. 得到scrollview的子view,便于操作
?//獲取子視圖 ? ? @Override ? ? protected void onFinishInflate() { ? ? ? ? super.onFinishInflate(); ? ? ? ? if (getChildCount() > 0) { ? ? ? ? ? ? childView = getChildAt(0); ? ? ? ? } ? ? }
4. 計算是否需要平移動畫
?private boolean isNeedMove() { ? ? ? ? int childMeasuredHeight = childView.getMeasuredHeight();//獲取子視圖的高度 ? ? ? ? int scrollViewMeasuredHeight = this.getMeasuredHeight();//獲取布局的高度 ? ? ? ? Log.e("TAG", "childMeasuredHeight = " + childMeasuredHeight); ? ? ? ? Log.e("TAG", "scrollViewMeasuredHeight = " + scrollViewMeasuredHeight); ? ? ? ? int dy = childMeasuredHeight - scrollViewMeasuredHeight;//dy >= 0 ? ? ? ? int scrollY = this.getScrollY();//獲取用戶在y軸方向上的偏移量 (上 + 下 -) ? ? ? ? if (scrollY <= 0 || scrollY >= dy) { ? ? ? ? ? ? return true;//按照我們自定義的MyScrollView的方式處理 ? ? ? ? } ? ? ? ? //其他處在臨界范圍內(nèi)的,返回false。即表示,仍按照ScrollView的方式處理 ? ? ? ? return false; ? ? }
5. 判斷是否需要平移動畫
//判斷是否需要執(zhí)行平移動畫 ? ? private boolean isNeedAnimation() { ? ? ? ? return !normal.isEmpty(); ? ? }
6. 既然我們做了事件攔截,那么就要重寫ontouchevent來執(zhí)行響應(yīng)事件
@Override ? ? public boolean onTouchEvent(MotionEvent ev) { ? ? ? ? if (childView == null || !isFinishAnimation) { ? ? ? ? ? ? return super.onTouchEvent(ev); ? ? ? ? } ? ? ? ? int eventY = (int) ev.getY();//獲取當(dāng)前的y軸坐標(biāo) ? ? ? ? switch (ev.getAction()) { ? ? ? ? ? ? case MotionEvent.ACTION_DOWN: ? ? ? ? ? ? ? ? lastY = eventY; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_MOVE: ? ? ? ? ? ? ? ? int dy = eventY - lastY;//微小的移動量 ? ? ? ? ? ? ? ? if (isNeedMove()) { ? ? ? ? ? ? ? ? ? ? if (normal.isEmpty()) { ? ? ? ? ? ? ? ? ? ? ? ? //記錄了childView的臨界狀態(tài)的左、上、右、下 ? ? ? ? ? ? ? ? ? ? ? ? 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; ? ? ? ? ? ? case MotionEvent.ACTION_UP: ? ? ? ? ? ? ? ? if (isNeedAnimation()) { ? ? ? ? ? ? ? ? ? ? //使用平移動畫 ? ? ? ? ? ? ? ? ? ? int translateY = childView.getBottom() - normal.bottom; ? ? ? ? ? ? ? ? ? ? TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 0, -translateY); ? ? ? ? ? ? ? ? ? ? translateAnimation.setDuration(200); // ? ? ? ?translateAnimation.setFillAfter(true);//停留在最終位置上 ? ? ? ? ? ? ? ? ? ? translateAnimation.setAnimationListener(new Animation.AnimationListener() { ? ? ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? ? ? public void onAnimationStart(Animation animation) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? isFinishAnimation = false; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? ? ? public void onAnimationEnd(Animation animation) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? isFinishAnimation = true; ? ? ? ? ? ? ? ? ? ? ? ? ? ? childView.clearAnimation();//清除動畫 ? ? ? ? ? ? ? ? ? ? ? ? ? ? //重新布局 ? ? ? ? ? ? ? ? ? ? ? ? ? ? childView.layout(normal.left, normal.top, normal.right, normal.bottom); ? ? ? ? ? ? ? ? ? ? ? ? ? ? //清除normal的數(shù)據(jù) ? ? ? ? ? ? ? ? ? ? ? ? ? ? normal.setEmpty(); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? ? ? public void onAnimationRepeat(Animation animation) { ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? }); ? ? ? ? ? ? ? ? ? ? //啟動動畫 ? ? ? ? ? ? ? ? ? ? childView.startAnimation(translateAnimation); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? return super.onTouchEvent(ev); }
這樣整個view的核心部分已經(jīng)完成了,把view嵌套到定義好了的scrollview就可以實(shí)現(xiàn)頁面的滑動回彈效果了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android基于reclyview實(shí)現(xiàn)列表回彈動畫效果
- Android?ScrollView實(shí)現(xiàn)滾動超過邊界松手回彈
- android ScrollView實(shí)現(xiàn)水平滑動回彈
- Android實(shí)現(xiàn)背景圖滑動變大松開回彈效果
- Android實(shí)現(xiàn)橡皮筋回彈和平移縮放效果
- Android自定義View實(shí)現(xiàn)豎向滑動回彈效果
- Android實(shí)現(xiàn)回彈ScrollView的原理
- Android自定義實(shí)現(xiàn)可回彈的ScollView
- Android ScrollView的頂部下拉和底部上拉回彈效果
- android自定義滾動上下回彈scollView
相關(guān)文章
Android 實(shí)現(xiàn)獲取手機(jī)里面的所有圖片詳解及實(shí)例
這篇文章主要介紹了Android 實(shí)現(xiàn)獲取手機(jī)里面的所有圖片詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05Android 實(shí)現(xiàn)掃雷小游戲?qū)嵗a
這篇文章主要介紹了Android 實(shí)現(xiàn)掃雷小游戲?qū)嵗a的相關(guān)資料,需要的朋友可以參考下2016-12-12Android自定義LinearLayout布局顯示不完整的解決方法
這篇文章主要給大家介紹了關(guān)于Android自定義LinearLayout但布局顯示不完整的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11Android自定義帶圓點(diǎn)的半圓形進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android自定義帶圓點(diǎn)的半圓形進(jìn)度條,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12Android自定義豎直方向SeekBar多色進(jìn)度條
這篇文章主要介紹了Android自定義SeekBar實(shí)現(xiàn)多色豎直進(jìn)度條效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10Android viewpager中動態(tài)添加view并實(shí)現(xiàn)偽無限循環(huán)的方法
這篇文章主要介紹了Android viewpager中動態(tài)添加view并實(shí)現(xiàn)偽無限循環(huán)的方法,涉及Android使用viewpager動態(tài)加載view及view無限循環(huán)顯示的相關(guān)技巧,需要的朋友可以參考下2016-01-01Android7.0 MTK設(shè)置默認(rèn)桌面
這篇文章主要為大家詳細(xì)介紹了Android7.0 MTK設(shè)置默認(rèn)桌面,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07Android Notification的多種用法總結(jié)
這篇文章主要介紹了Android Notification的多種用法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-06-06