我們在開發中經常要實現view的可見性的設置,但是如果view面積較大時可見性如果變化,在視覺效果上會比較生硬,因此我們需要加上動畫,在可見不可見之間增加一個過渡
直接上代碼:
if (view.getVisibility() == View.VISIBLE) {
TranslateAnimation hideAnim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f);
hideAnim.setDuration(500);
view.startAnimation(hideAnim);
hideAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
llShiftClass.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
} else {
TranslateAnimation showAnim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
showAnim.setDuration(500);
view.startAnimation(showAnim);
view.setVisibility(View.VISIBLE);
}
這裏,最重要的是TranslateAnimation,進入這個構造函數:
/** * Constructor to use when building a TranslateAnimation from code * * @param fromXType Specifies how fromXValue should be interpreted. One of * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or * Animation.RELATIVE_TO_PARENT. * @param fromXValue Change in X coordinate to apply at the start of the * animation. This value can either be an absolute number if fromXType * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. * @param toXType Specifies how toXValue should be interpreted. One of * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or * Animation.RELATIVE_TO_PARENT. * @param toXValue Change in X coordinate to apply at the end of the * animation. This value can either be an absolute number if toXType * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. * @param fromYType Specifies how fromYValue should be interpreted. One of * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or * Animation.RELATIVE_TO_PARENT. * @param fromYValue Change in Y coordinate to apply at the start of the * animation. This value can either be an absolute number if fromYType * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. * @param toYType Specifies how toYValue should be interpreted. One of * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or * Animation.RELATIVE_TO_PARENT. * @param toYValue Change in Y coordinate to apply at the end of the * animation. This value can either be an absolute number if toYType * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. */ public TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) { mFromXValue = fromXValue; mToXValue = toXValue; mFromYValue = fromYValue; mToYValue = toYValue; mFromXType = fromXType; mToXType = toXType; mFromYType = fromYType; mToYType = toYType; }
可以看到,這個構造函數主要是傳入設置起始點座標(x,y)、終點座標(x,y),每個座標值對應一個類型type。
Animation.RELATIVE_TO_SELF代表着座標以當前view爲基準。
0.0f即0%,代表view初始位置座標;
1.0f即100%,代表以view初始位置爲原點,相應x座標/y座標增加父寬度/父高度的100%;
TranslateAnimation showAnim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f);
表示從(1.0f,0.0f)移動到(0.0f,0.0f),即view從屏幕右側不可見區域移動到初始位置,符合VISIBLE效果。new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f);
表示從(0.0f,0.0f)移動到(1.0f,0.0f),即view從初始位置移動到屏幕右側不可見區域,符合GONE效果。