Android View的显示隐藏(上下左右移动动画)
程序员文章站
2022-05-06 08:09:36
...
一、利用Android提供的左右移动工具类:AnimationUtils
//view1在右边,view2在左边
// 向右边移出
view1.setVisibility(View.GONE);
view1.setAnimation(AnimationUtils.makeOutAnimation(this, true));
// 向左边移入
view1.setVisibility(View.VISIBLE);
view1.setAnimation(AnimationUtils.makeInAnimation(this, false));
/*========== 分割线 ===========*/
// 向右边移入
view2.setAnimation(AnimationUtils.makeInAnimation(this, true));
view2.setVisibility(View.VISIBLE);
// 向左边移出
view2.setVisibility(View.GONE);
view2.setAnimation(AnimationUtils.makeOutAnimation(this, false));
实例:
//通过事件分发来判断 TextView是否显示
rl_home.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
if(mposVData.getSelf_pos_num()>0){
textView_jycx.setVisibility(View.GONE);
// 向右边移出
tv_check.setAnimation(AnimationUtils.makeOutAnimation(App.get(), true));
}
break;
case MotionEvent.ACTION_MOVE:
if(mposVData.getSelf_pos_num()>0){
textView_jycx.setVisibility(View.GONE);
// 向右边移出
tv_check.setAnimation(AnimationUtils.makeOutAnimation(App.get(), true));
}
break;
case MotionEvent.ACTION_UP:
if(mposVData.getSelf_pos_num()>0){
textView_jycx.setVisibility(View.VISIBLE);
//向左边移入
tv_check.setAnimation(AnimationUtils.makeInAnimation(getActivity(), false));
}
break;
}
return 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;
}
}
隐藏的时候设置下动画就可以了
view1.setVisibility(View.GONE);
view1.setAnimation(AnimationUtil.moveToViewBottom());
view2.setVisibility(View.VISIBLE);
view2.setAnimation(AnimationUtil.moveToViewLocation());
上一篇: 设计模式-迭代器模式
下一篇: Binary Search