最近遇到一個(gè)需求冯凹,要求在登陸界面上實(shí)現(xiàn)點(diǎn)擊 EditText彈出輸入法之后讓登陸的logo(或者標(biāo)題)動(dòng)態(tài)縮小吉嚣,一眼看到這個(gè)這個(gè)需求就是判斷軟鍵盤彈出與否+動(dòng)畫實(shí)現(xiàn)梢薪,軟鍵盤彈出與否在原生沒(méi)有方法直接去判斷,可以根據(jù)彈出鍵盤后布局的高度變化來(lái)判斷尝哆,詳情可以參考:android開(kāi)發(fā)中秉撇,監(jiān)聽(tīng)軟鍵盤的狀態(tài)(收起或者打開(kāi))
而動(dòng)畫效果首先想到的就是Animation和ObjectAnimator,但是這樣的動(dòng)畫效果對(duì)于布局的大小是不會(huì)有影響的秋泄,即使logo或者title縮小但是布局仍然是一樣的大小琐馆,這里我們可以用ValueAnimator + LayoutParams來(lái)實(shí)現(xiàn):
private void showHideTitle(final View view, final int maxHeight, int duration, boolean isShow) {
ValueAnimator animator;
if (isShow) {
animator = ValueAnimator.ofFloat(0f, 1f);
} else {
animator = ValueAnimator.ofFloat(1f, 0f);
}
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float currentValue = (float) animation.getAnimatedValue();
ViewGroup.LayoutParams params = view.getLayoutParams();
params.height = (int) (currentValue * maxHeight);
view.setLayoutParams(params);
}
});
animator.setDuration(duration).start();
}
然后在代碼中需要做動(dòng)畫的地方傳入title的view,titile的大小恒序,動(dòng)畫持續(xù)的時(shí)間瘦麸,還有是展開(kāi)動(dòng)畫還是縮小動(dòng)畫就好