一、利用Android提供的左右移動工具類:AnimationUtils
LinearLayout ll_first = (LinearLayout) findViewById(R.id.ll_first);
LinearLayout ll_second = (LinearLayout) findViewById(R.id.ll_second);
ll_first.setVisibility(View.GONE);
ll_second.setVisibility(View.VISIBLE);
// 向右邊移出
ll_first.setAnimation(AnimationUtils.makeOutAnimation(this, true));
// 向右邊移入
ll_second.setAnimation(AnimationUtils.makeInAnimation(this, true));
ll_first.setVisibility(View.VISIBLE);
ll_second.setVisibility(View.GONE);
// 向左邊移入
ll_first.setAnimation(AnimationUtils.makeInAnimation(this, false));
// 向左邊移出
ll_second.setAnimation(AnimationUtils.makeOutAnimation(this, false));
二、用TranslateAnimation添加動畫
先寫一個AnimationUtil工具類:這里僅提供上下移動效果
public class AnimationUtil {
private static final String TAG = AnimationUtil.class.getSimpleName();
/**
* 從控件所在位置移動到控件的底部
*
* @return
*/
public static TranslateAnimation moveToViewBottom() {
TranslateAnimation mHiddenAction = 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);
mHiddenAction.setDuration(500);
return mHiddenAction;
}
/**
* 從控件的底部移動到控件所在位置
*
* @return
*/
public static TranslateAnimation moveToViewLocation() {
TranslateAnimation mHiddenAction = 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);
mHiddenAction.setDuration(500);
return mHiddenAction;
}
}
隱藏的時候設置下動畫就可以了
ll_first.setVisibility(View.GONE);
ll_second.setVisibility(View.VISIBLE);
ll_first.setAnimation(AnimationUtil.moveToViewBottom());
ll_second.setAnimation(AnimationUtil.moveToViewLocation());
