欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義scrollview實(shí)現(xiàn)回彈效果

 更新時間:2022年04月01日 12:59:33   作者:鴻鵠志遠(yuǎn)  
這篇文章主要為大家詳細(xì)介紹了Android自定義scrollview實(shí)現(xiàn)回彈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在ios手機(jī)上經(jīng)常看到頁面上下滑動回彈效果,安卓中沒有原生控件支持,這里自己就去自定義一個scrollview實(shí)現(xiàn)回彈效果

1. 新建MyScrollView并繼承ScrollView,可以通過事件分發(fā)機(jī)制攔截并處理滑動事件

2. 重寫事件分發(fā)攔截事件onInterceptTouchEvent方法,計(jì)算是否需要攔截事件

//攔截:實(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. 計(jì)算是否需要平移動畫

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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java語言讀取配置文件config.properties的方法講解

    Java語言讀取配置文件config.properties的方法講解

    今天小編就為大家分享一篇關(guān)于Java語言讀取配置文件config.properties的方法講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Android振動器使用方法詳解

    Android振動器使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了Android振動器使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • android中px和dp,px和sp之間的轉(zhuǎn)換方法

    android中px和dp,px和sp之間的轉(zhuǎn)換方法

    在Android開發(fā)中dp和px,sp和px之間的轉(zhuǎn)換時必不可少的。下面腳本之家小編給大家?guī)砹薬ndroid中px和dp,px和sp之間的轉(zhuǎn)換方法,感興趣的朋友一起看看吧
    2018-06-06
  • Spinner在Dialog中的使用效果實(shí)例代碼詳解

    Spinner在Dialog中的使用效果實(shí)例代碼詳解

    這篇文章主要介紹了Spinner在Dialog中的使用效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-05-05
  • Android開發(fā)實(shí)現(xiàn)廣告無限循環(huán)功能示例

    Android開發(fā)實(shí)現(xiàn)廣告無限循環(huán)功能示例

    這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)廣告無限循環(huán)功能,結(jié)合完整實(shí)例形式分析了Android廣告圖片輪播功能的具體實(shí)現(xiàn)步驟與相關(guān)功能、布局等操作技巧,需要的朋友可以參考下
    2017-11-11
  • React?Native之在Android上添加陰影的實(shí)現(xiàn)

    React?Native之在Android上添加陰影的實(shí)現(xiàn)

    這篇文章主要介紹了React?Native之在Android上添加陰影的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Android開發(fā)gradle拉取依賴的加速配置

    Android開發(fā)gradle拉取依賴的加速配置

    這篇文章主要為大家介紹了Android開發(fā)gradle拉取依賴的加速配置,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • 通知監(jiān)控NotificationListenerService onNotificationPosted重復(fù)回調(diào)問題

    通知監(jiān)控NotificationListenerService onNotificationPosted重復(fù)回

    這篇文章主要為大家介紹了通知監(jiān)控NotificationListenerService onNotificationPosted重復(fù)回調(diào)問題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Flutter 側(cè)滑欄及城市選擇UI的實(shí)現(xiàn)方法

    Flutter 側(cè)滑欄及城市選擇UI的實(shí)現(xiàn)方法

    這篇文章主要介紹了Flutter 側(cè)滑欄及城市選擇UI的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-07-07
  • Android實(shí)現(xiàn)自定義View控件的流程詳解

    Android實(shí)現(xiàn)自定義View控件的流程詳解

    這篇文章主要為大家詳細(xì)介紹了Android中實(shí)現(xiàn)自定義View控件的流程,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下
    2023-06-06

最新評論