1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      Android中如何通過自定義ViewGroup實現(xiàn)一個彈性滑動效果

      本篇內(nèi)容主要講解“Android中如何通過自定義ViewGroup實現(xiàn)一個彈性滑動效果”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“Android中如何通過自定義ViewGroup實現(xiàn)一個彈性滑動效果”吧!

      創(chuàng)新互聯(lián)建站是一家專注于成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)與策劃設(shè)計,東昌網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:東昌等地區(qū)。東昌做網(wǎng)站價格咨詢:18980820575

      實現(xiàn)原理

      onMeasure()中測量所有子View

       @Override
       protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 測量所有子View
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
         View childView = getChildAt(i);
         measureChild(childView, widthMeasureSpec, heightMeasureSpec);
        }
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
       }

      onLayout()中,將所有的子View按照位置依次往下排列

      @Override
       protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // 設(shè)置ViewGroup的高度,對所有子View進行排列
        int childCount = getChildCount();
        MarginLayoutParams params = (MarginLayoutParams) getLayoutParams();
        params.height = mScreenHeight * childCount;
        for (int i = 0; i < childCount; i++) {
         View childView = getChildAt(i);
         if (childView.getVisibility() != View.GONE) {
          // 給每個ChildView放置在指定位置
          childView.layout(l, i * mScreenHeight, r, (i + 1) * mScreenHeight);
         }
        }
       }

      onTouchEvent()中處理滑動

       @Override
       public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
         case MotionEvent.ACTION_DOWN:
          mLastY = (int) event.getY();
          mStart = getScrollY();
          return true;
         case MotionEvent.ACTION_MOVE:
          if (!mScroller.isFinished()) {
           // 終止滑動
           mScroller.abortAnimation();
          }
          int offsetY = (int) (mLastY - event.getY());
          Log.d(TAG, "onTouchEvent: getScrollY: " + getScrollY());
          Log.d(TAG, "onTouchEvent: offsetY " + offsetY);
          // 到達頂部,使用offset判斷方向
          if (getScrollY() + offsetY < 0) { // 當(dāng)前已經(jīng)滑動的 Y 位置
           offsetY = 0;
          }
          // 到達底部
          if (getScrollY() > getHeight() - mScreenHeight && offsetY > 0) {
           offsetY = 0;
          }
          scrollBy(0, offsetY);
          // 滑動完成后,重新設(shè)置LastY位置
          mLastY = (int) event.getY();
          break;
         case MotionEvent.ACTION_UP:
          mEnd = getScrollY();
          int distance = mEnd - mStart;
          if (distance > 0) { // 向上滑動
           if (distance < mScreenHeight / 3) {
            Log.d(TAG, "onTouchEvent: distance < screen/3");
            // 回到原來位置
            mScroller.startScroll(0, getScrollY(), 0, -distance);
           } else {
            // 滾到屏幕的剩余位置
            mScroller.startScroll(0, getScrollY(), 0, mScreenHeight - distance);
           }
          } else {    // 向下滑動
           if (-distance < mScreenHeight / 3) {
            mScroller.startScroll(0, getScrollY(), 0, -distance);
           } else {
            mScroller.startScroll(0, getScrollY(), 0, -mScreenHeight - distance);
           }
          }
          postInvalidate();
        }
        return super.onTouchEvent(event);
       }

      其中ACTION_UP這段代碼是處理彈性滑動的

      case MotionEvent.ACTION_UP:
          mEnd = getScrollY();
          int distance = mEnd - mStart;
          if (distance > 0) { // 向上滑動
           if (distance < mScreenHeight / 3) {
            Log.d(TAG, "onTouchEvent: distance < screen/3");
            // 回到原來位置
            mScroller.startScroll(0, getScrollY(), 0, -distance);
           } else {
            // 滾到屏幕的剩余位置
            mScroller.startScroll(0, getScrollY(), 0, mScreenHeight - distance);
           }
          } else {    // 向下滑動
           if (-distance < mScreenHeight / 3) {
            mScroller.startScroll(0, getScrollY(), 0, -distance);
           } else {
            mScroller.startScroll(0, getScrollY(), 0, -mScreenHeight - distance);
           }
          }
          postInvalidate();

      完整代碼

      public class ScrollViewGroup extends ViewGroup {
      
       private static final String TAG = "ScrollView";
      
       private Scroller mScroller;
       private int mScreenHeight; // 窗口高度
       private int mLastY;
       private int mStart;
       private int mEnd;
      
       public ScrollViewGroup(Context context) {
        this(context, null);
       }
      
       public ScrollViewGroup(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
       }
      
       public ScrollViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mScroller = new Scroller(context);
        // 獲取屏幕高度
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(metrics);
        mScreenHeight = metrics.heightPixels;
        Log.d(TAG, "ScrollViewGroup: ScreenHeight " + mScreenHeight);
       }
      
      
       @Override
       protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 測量所有子View
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
         View childView = getChildAt(i);
         measureChild(childView, widthMeasureSpec, heightMeasureSpec);
        }
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
       }
      
       @Override
       protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // 設(shè)置ViewGroup的高度,對所有子View進行排列
        int childCount = getChildCount();
        MarginLayoutParams params = (MarginLayoutParams) getLayoutParams();
        params.height = mScreenHeight * childCount;
        for (int i = 0; i < childCount; i++) {
         View childView = getChildAt(i);
         if (childView.getVisibility() != View.GONE) {
          // 給每個ChildView放置在指定位置
          childView.layout(l, i * mScreenHeight, r, (i + 1) * mScreenHeight);
         }
        }
       }
      
       @Override
       public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
         case MotionEvent.ACTION_DOWN:
          mLastY = (int) event.getY();
          mStart = getScrollY();
          return true;
         case MotionEvent.ACTION_MOVE:
          if (!mScroller.isFinished()) {
           // 終止滑動
           mScroller.abortAnimation();
          }
          int offsetY = (int) (mLastY - event.getY());
          Log.d(TAG, "onTouchEvent: getScrollY: " + getScrollY());
          Log.d(TAG, "onTouchEvent: offsetY " + offsetY);
          // 到達頂部,使用offset判斷方向
          if (getScrollY() + offsetY < 0) { // 當(dāng)前已經(jīng)滑動的 Y 位置
           offsetY = 0;
          }
          // 到達底部
          if (getScrollY() > getHeight() - mScreenHeight && offsetY > 0) {
           offsetY = 0;
          }
          scrollBy(0, offsetY);
          // 滑動完成后,重新設(shè)置LastY位置
          mLastY = (int) event.getY();
          break;
         case MotionEvent.ACTION_UP:
          mEnd = getScrollY();
          int distance = mEnd - mStart;
          if (distance > 0) { // 向上滑動
           if (distance < mScreenHeight / 3) {
            Log.d(TAG, "onTouchEvent: distance < screen/3");
            // 回到原來位置
            mScroller.startScroll(0, getScrollY(), 0, -distance);
           } else {
            // 滾到屏幕的剩余位置
            mScroller.startScroll(0, getScrollY(), 0, mScreenHeight - distance);
           }
          } else {    // 向下滑動
           if (-distance < mScreenHeight / 3) {
            mScroller.startScroll(0, getScrollY(), 0, -distance);
           } else {
            mScroller.startScroll(0, getScrollY(), 0, -mScreenHeight - distance);
           }
          }
          postInvalidate();
        }
        return super.onTouchEvent(event);
       }
      
      
       @Override
       public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
         scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
         postInvalidate();
        }
       }
      }

      到此,相信大家對“Android中如何通過自定義ViewGroup實現(xiàn)一個彈性滑動效果”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!


      網(wǎng)站標題:Android中如何通過自定義ViewGroup實現(xiàn)一個彈性滑動效果
      當(dāng)前路徑:http://www.ef60e0e.cn/article/jgohcc.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        普陀区| 津南区| 博湖县| 余庆县| 华坪县| 阿尔山市| 阿拉尔市| 牟定县| 宣武区| 闻喜县| 启东市| 龙游县| 封丘县| 武定县| 连平县| 登封市| 读书| 民县| 楚雄市| 西吉县| 金阳县| 葵青区| 仁布县| 盐边县| 寿阳县| 普陀区| 年辖:市辖区| 白山市| 梅州市| 华安县| 黄骅市| 会同县| 台州市| 邹城市| 南安市| 哈密市| 丁青县| 南木林县| 偏关县| 藁城市| 勐海县|