日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android?實現卡片堆疊錢包管理動畫效果_Android

作者:Sand ? 更新時間: 2022-09-14 編程語言

先上效果圖

源碼 github.com/woshiwzy/Ca…

實現原理:

1.繼承LinearLayout
2.重寫onLayout,onMeasure 方法
3.利用ValueAnimator 實施動畫
4.在動畫回調中requestLayout 實現動畫效果

思路:

1.用Bounds 對象記錄每一個CardView 對象的初始位置,當前位置,運動目標位置

2.點擊時計算出對應的view以及可能會產生關聯運動的View的運動的目標位置,從當前位置運動到目標位置,然后以這2個位置作為動畫參數實施ValueAnimator動畫,在動畫回調中觸發onLayout,達到動畫的效果。

重寫adView 方法

確保新添加的在這里確保所有的子view 都有一個初始化的bounds位置

   @Override
    public void addView(View child, ViewGroup.LayoutParams params) {
        super.addView(child, params);
        Bounds bounds = getBunds(getChildCount());
    }

確保每個子View的測量屬性寬度填滿父組件

    boolean mesured = false;
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (mesured == true) {//只需要測量一次
            return;
        }
        mesured = true;
        int childCount = getChildCount();
        int rootWidth = getWidth();
        int rootHeight = getHeight();
        if (childCount > 0) {
            View child0 = getChildAt(0);
            int modeWidth = MeasureSpec.getMode(child0.getMeasuredWidth());
            int sizeWidth = MeasureSpec.getSize(child0.getMeasuredWidth());

            int modeHeight = MeasureSpec.getMode(child0.getMeasuredHeight());
            int sizeHeight = MeasureSpec.getSize(child0.getMeasuredHeight());

            if (childCount > 0) {
                for (int i = 0; i < childCount; i++) {
                    View childView = getChildAt(i);
                    childView.measure(MeasureSpec.makeMeasureSpec(sizeWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(sizeHeight, MeasureSpec.EXACTLY));
                    int top = (int) (i * (sizeHeight * carEvPercnet));
                    getBunds(i).setTop(top);
                    getBunds(i).setCurrentTop(top);
                    getBunds(i).setLastCurrentTop(top);
                    getBunds(i).setHeight(sizeHeight);
                }

            }

        }
    }

重寫onLayout 方法是關鍵

是動畫觸發的主要目的,這里layout參數并不是寫死的,而是計算出來的(通過ValueAnimator 計算出來的)

@Override
    protected void onLayout(boolean changed, int sl, int st, int sr, int sb) {
        int childCount = getChildCount();
        if (childCount > 0) {
            for (int i = 0; i < childCount; i++) {
                View view = getChildAt(i);
                int mWidth = view.getMeasuredWidth();
                int mw = MeasureSpec.getSize(mWidth);
                int l = 0, r = l + mw;
                view.layout(l, getBunds(i).getCurrentTop(), r, getBunds(i).getCurrentTop() + getBunds(i).getHeight());
            }
        }
    }

源碼

github: github.com/woshiwzy/Ca…

原文鏈接:https://juejin.cn/post/7073371960150851615

欄目分類
最近更新