溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android怎么自定義ScrollView實(shí)現(xiàn)阻尼回彈

發(fā)布時(shí)間:2022-04-01 16:00:37 來源:億速云 閱讀:306 作者:iii 欄目:開發(fā)技術(shù)

今天小編給大家分享一下Android怎么自定義ScrollView實(shí)現(xiàn)阻尼回彈的相關(guān)知識點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

解決思路:

ScrollView使用時(shí)要求內(nèi)部有且僅一個子View。當(dāng)ScrollView滑動到邊界時(shí),讓子View在ScrollView中隨著手指按一定的規(guī)則進(jìn)行平移,模擬出拉伸效果。當(dāng)手指松開時(shí),再讓子View恢復(fù)拉伸前的位置,模擬出回彈效果。

Android怎么自定義ScrollView實(shí)現(xiàn)阻尼回彈

完整的代碼如下,詳細(xì)的原理見注釋即可

public class StretchScrollView extends NestedScrollView {

    // 子View
    private View innerView;
    // 上次手勢事件的y坐標(biāo)
    private float mLastY;
    // 記錄子View的正常位置
    private Rect normal = new Rect();

    public StretchScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        initView();
        super.onFinishInflate();
    }

    /**
     * 獲取ScrollView的子布局
     */
    private void initView() {
        // 去除原本ScrollView滾動到邊界時(shí)的陰影效果
        setOverScrollMode(OVER_SCROLL_NEVER);
        if (getChildAt(0) != null) {
            innerView = getChildAt(0);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_UP:
                // 手指松開恢復(fù)
                if (!normal.isEmpty()) {
                    planAnimation();
                    normal.setEmpty();
                    mLastY = 0;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                float currentY = ev.getY();
                // 滑動距離
                int distanceY = (int) (mLastY - currentY);

                // 處理Y軸的滾動事件,當(dāng)滾動到最上或者最下時(shí)需要移動布局
                // 手指剛觸及屏幕時(shí),也會觸發(fā)此事件,此時(shí)mLastY的值還是0,會立即觸發(fā)一個比較大的移動。這里過濾掉這種情況
                if (isNeedTranslate() && mLastY != 0) {
                    if (normal.isEmpty()) {
                        // 保存正常的布局位置
                        normal.set(innerView.getLeft(), innerView.getTop(), innerView.getRight(), innerView.getBottom());
                    }
                    // 移動布局, 使distance / 2 防止平移過快
                    innerView.layout(innerView.getLeft(), innerView.getTop() - distanceY / 2,
                            innerView.getRight(), innerView.getBottom() - distanceY / 2);
                }
                mLastY = currentY;
                break;
        }
        return super.onTouchEvent(ev);
    }

    /**
     * 回縮動畫
     */
    public void planAnimation() {
        // 開啟移動動畫
        TranslateAnimation animation = new TranslateAnimation(0, 0, innerView.getTop(), normal.top);
        animation.setDuration(200);
        innerView.startAnimation(animation);
        // 補(bǔ)間動畫并不會真正修改innerView的位置,這里需要設(shè)置使得innerView回到正常的布局位置
        innerView.layout(normal.left, normal.top, normal.right, normal.bottom);
    }

    /**
     * 是否需要Y移動布局
     */
    public boolean isNeedTranslate() {
        int offset = innerView.getMeasuredHeight() - getHeight();
        int scrollY = getScrollY();
        // 頂部或者底部
        return scrollY == 0 || scrollY == offset;
    }
}

以上就是“Android怎么自定義ScrollView實(shí)現(xiàn)阻尼回彈”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學(xué)習(xí)更多的知識,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI