Android屬性動畫實現(xiàn)View隱藏顯示

我們知道屬性動畫要設(shè)置get、set 方法,但是有時不好設(shè)置,因此我們找新方法。

/**

* Created by gongdongdong on 2017/1/16.

*/

public class OffsetAnimation {

private LinearLayout mOffsetll;

private int mHeight;

public OffsetAnimation(LinearLayout mOffsetll, int height) {

this.mOffsetll = mOffsetll;

mHeight = height;

}

public void startShowAnimate() {

ViewWrapper wrapper = new ViewWrapper(mOffsetll);

ObjectAnimator.ofInt(wrapper, "bottomMargin", mHeight).setDuration(500).start();

}

public void startHideAnimate() {

ViewWrapper wrapper = new ViewWrapper(mOffsetll);

ObjectAnimator.ofInt(wrapper, "bottomMargin", 0).setDuration(500).start();

}

public class ViewWrapper {

private LinearLayout ll;

public int getBottomMargin() {

return ll.getLayoutParams().height;

}

public void setBottomMargin(int bottomMargin) {

ll.getLayoutParams().height = bottomMargin;

ll.requestLayout();

}

public ViewWrapper(LinearLayout view) {

this.ll = view;

}

}

}

使用方式:

?????? 將隱藏顯示的內(nèi)容放在LinearLayout中,啟動的時候測量出LinearLayout的高度height(這里你可以onCreate獲取高度,可以使用measure、ViewTreeObserver、post三種方式中的一種),這里我使用ViewTreeObserver,如下:

ViewTreeObserver vto=mInsuranceTv.getViewTreeObserver();

vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

@Override

public void onGlobalLayout() {

mInsuranceTv.getViewTreeObserver().removeGlobalOnLayoutListener(this);

height=mInsuranceTv.getMeasuredHeight();

}

});

啟動動畫的時候可以實例化OffsetAnimation對象m,將LinearLayout和height傳入;調(diào)用m.startShowAnimate()顯示、m.startHideAnimate()隱藏。如果想隱藏有動畫效果的View,可以在onCreate里做如下處理。

mInsuranceTv= (TextView) findViewById(R.id.insurance_tv); //TextView要隱藏顯示的view

LinearLayout.LayoutParams? mLayoutParams = ((LinearLayout.LayoutParams) mInsuranceTv2.getLayoutParams());

mLayoutParams.bottomMargin=-height;


大家可以試下,效果還可以,有問題大家可以留言下,相互學(xué)習(xí)。本文借鑒《Android開發(fā)藝術(shù)探索》

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容