第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Android:展開/折疊動(dòng)畫

Android:展開/折疊動(dòng)畫

Qyouu 2019-06-24 09:59:46
Android:展開/折疊動(dòng)畫假設(shè)我有一個(gè)垂直線性Layout:[v1][v2]默認(rèn)情況下,v1已經(jīng)明顯地=消失了。我想用擴(kuò)展動(dòng)畫顯示v1,同時(shí)按下v2。我試過這樣的方法:Animation a = new Animation(){     int initialHeight;     @Override     protected void applyTransformation(float interpolatedTime, Transformation t) {         final int newHeight = (int)(initialHeight * interpolatedTime);         v.getLayoutParams().height = newHeight;         v.requestLayout();     }     @Override     public void initialize(int width, int height, int parentWidth, int parentHeight) {         super.initialize(width, height, parentWidth, parentHeight);         initialHeight = height;     }     @Override     public boolean willChangeBounds() {         return true;     }};但是有了這個(gè)解決方案,當(dāng)動(dòng)畫開始的時(shí)候,我會(huì)眨眼。我認(rèn)為這是因?yàn)関1在動(dòng)畫應(yīng)用之前顯示了完整的尺寸。對于javascript,這是jQuery的一行!用Android做這件事有什么簡單的方法嗎?
查看完整描述

3 回答

?
溫溫醬

TA貢獻(xiàn)1752條經(jīng)驗(yàn) 獲得超4個(gè)贊

我看到這個(gè)問題很受歡迎,所以我貼出了我的實(shí)際解決方案。主要的優(yōu)點(diǎn)是您不需要知道擴(kuò)展的高度才能應(yīng)用動(dòng)畫,并且一旦視圖被展開,如果內(nèi)容發(fā)生變化,它就會(huì)適應(yīng)高度。對我來說很管用。

public static void expand(final View v) {
    int matchParentMeasureSpec = View.MeasureSpec.makeMeasureSpec(((View) v.getParent()).getWidth(), View.MeasureSpec.EXACTLY);
    int wrapContentMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    v.measure(matchParentMeasureSpec, wrapContentMeasureSpec);
    final int targetHeight = v.getMeasuredHeight();

    // Older versions of android (pre API 21) cancel animations for views with a height of 0.
    v.getLayoutParams().height = 1;
    v.setVisibility(View.VISIBLE);
    Animation a = new Animation()
    {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            v.getLayoutParams().height = interpolatedTime == 1
                    ? LayoutParams.WRAP_CONTENT                    : (int)(targetHeight * interpolatedTime);
            v.requestLayout();
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    // Expansion speed of 1dp/ms
    a.setDuration((int)(targetHeight / v.getContext().getResources().getDisplayMetrics().density));
    v.startAnimation(a);}public static void collapse(final View v) {
    final int initialHeight = v.getMeasuredHeight();

    Animation a = new Animation()
    {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            if(interpolatedTime == 1){
                v.setVisibility(View.GONE);
            }else{
                v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
                v.requestLayout();
            }
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    // Collapse speed of 1dp/ms
    a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));
    v.startAnimation(a);}

正如@Jefferson在評(píng)論中所提到的,您可以通過更改動(dòng)畫的持續(xù)時(shí)間(以及速度)來獲得更流暢的動(dòng)畫。目前,它已被設(shè)置為1dp/ms。


查看完整回答
反對 回復(fù) 2019-06-24
?
不負(fù)相思意

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊

我試圖做一個(gè)我認(rèn)為是非常相似的動(dòng)畫,并找到了一個(gè)優(yōu)雅的解決方案。此代碼假定您總是從0->h或h->0(h是最大高度)。這三個(gè)構(gòu)造函數(shù)參數(shù)是view=要?jiǎng)赢嫷囊晥D(在我的示例中是webview)、Target tHL.8=視圖的最大高度,以及down=指定方向的布爾值(true=展開、false=折疊)。

public class DropDownAnim extends Animation {
    private final int targetHeight;
    private final View view;
    private final boolean down;

    public DropDownAnim(View view, int targetHeight, boolean down) {
        this.view = view;
        this.targetHeight = targetHeight;
        this.down = down;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        int newHeight;
        if (down) {
            newHeight = (int) (targetHeight * interpolatedTime);
        } else {
            newHeight = (int) (targetHeight * (1 - interpolatedTime));
        }
        view.getLayoutParams().height = newHeight;
        view.requestLayout();
    }

    @Override
    public void initialize(int width, int height, int parentWidth,
            int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }}


查看完整回答
反對 回復(fù) 2019-06-24
?
慕沐林林

TA貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個(gè)贊

我今天在同一個(gè)問題上絆倒了,我想這個(gè)問題的真正解決辦法是

<LinearLayout android:id="@+id/container"android:animateLayoutChanges="true"... />

您必須為所有最頂層的布局設(shè)置此屬性,這些布局涉及到移位。如果您現(xiàn)在設(shè)置一個(gè)布局的可見性為消失,另一個(gè)將占用空間,因?yàn)檎谙У囊粋€(gè)正在釋放它。會(huì)有一個(gè)默認(rèn)的動(dòng)畫,這是某種“淡出”,但我認(rèn)為你可以改變這個(gè)-但最后一個(gè)我還沒有測試,目前。


查看完整回答
反對 回復(fù) 2019-06-24
  • 3 回答
  • 0 關(guān)注
  • 1822 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)