Android 浮動按鈕的伸縮效果

來源:互聯網
上載者:User

標籤:amp   bubuko   tran   nan   override   簡單   mat   col   mac   

在做項目時想增加點動感,於是就有如下效果:

實現起來也很簡單,通過屬性動畫和recyclerview 滑動結合就很好實現了。

通過給recycleview添加一個滑動監聽:通過滾動的差值來處理動畫

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {            @Override            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {            }            @Override            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {                if (dy > 0 ){                    hidenFabAnim();                }else{                    showFabAnim();                }            }        });

兩個動畫如下:

/**     * 動畫隱藏浮動按鈕     */    private void hidenFabAnim() {        if (!isFabAnimg && mActionButton != null && mActionButton.getVisibility() == View.VISIBLE) {                Animator animator = ObjectAnimator.ofFloat(mActionButton, "translationY", 0f, 100f);                animator.setDuration(500);                animator.addListener(new Animator.AnimatorListener() {                    @Override                    public void onAnimationStart(Animator animation) {                        isFabAnimg = true;                    }                    @Override                    public void onAnimationEnd(Animator animation) {                        isFabAnimg = false;                        mActionButton.setVisibility(View.GONE);                    }                    @Override                    public void onAnimationCancel(Animator animation) {                        isFabAnimg = false;                    }                    @Override                    public void onAnimationRepeat(Animator animation) {                    }                });                animator.start();            }    }    /**     * 動畫顯示浮動按鈕     */    private void showFabAnim(){        if (mActionButton != null && !isFabAnimg && mActionButton.getVisibility() == View.GONE) {            Animator animator =  ObjectAnimator.ofFloat(mActionButton,"translationY",100f,0f);            animator.setDuration(500);            animator.addListener(new Animator.AnimatorListener() {                @Override                public void onAnimationStart(Animator animation) {                    isFabAnimg = true;                }                @Override                public void onAnimationEnd(Animator animation) {                    isFabAnimg = false;                }                @Override                public void onAnimationCancel(Animator animation) {                    isFabAnimg = false;                }                @Override                public void onAnimationRepeat(Animator animation) {                }            });            mActionButton.setVisibility(View.VISIBLE);            animator.start();        }    }

動畫中給定了移出螢幕和顯示在螢幕的距離是100,這個值並不是那麼確定性,項目中需要通過螢幕密度來算這個值的大小,以便適應更好的滑動效果;定義了個

isFabAnimg 變數,用來明確動畫的狀態,避免了重複執行顯示或者隱藏的動畫。

可見android中要實現某些小效果還是非常的方便的

Android 浮動按鈕的伸縮效果

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.