android 组件消失了,Android 控件逐渐出现,逐渐消失的动画

第一步:首先在xml布局文件中设置该控件为android:visibility="gone"。

第二步:在代码中配置两个Animation

alphaAnimation appearAnimation = new AlphaAnimation(0, 1);

appearAnimation.setDuration(500);

disappearAnimation = new AlphaAnimation(1, 0);

disappearAnimation.setDuration(500);

第三步:想让控件出现时

if (flowBottomLL.getVisibility() == View.GONE) {

flowBottomLL.startAnimation(appearAnimation);

flowBottomLL.setVisibility(View.VISIBLE);

}

第四步:想让控件消失时,

flowBottomLL.startAnimation(disappearAnimation);

disappearAnimation.setAnimationListener(new AnimationListener() {

@Override

public void onAnimationStart(Animation animation) {}

@Override

public void onAnimationRepeat(Animation animation) {}

@Override

public void onAnimationEnd(Animation animation) {

flowBottomLL.setVisibility(View.GONE);

}

});

总结:出现和消失的代码是不同的,因为我们的控件一开始是隐藏的,当执行出现的动画时,如果不立即设置控件为可见,就看不到这个动画效果;同样,在消失的时候,不能马上隐藏控件,那样也会看不到动画效果,必须监听动画的执行,当动画执行完成后,再设置控件隐藏。本文只提到如何让控件逐渐出现和消失,并没有提到如何触发这两个效果,我提供一种情况吧。如果要达到触摸屏幕,控件就逐渐出现,手离开屏幕若干秒后,控件就逐渐消失的效果,可以重写onTouchEvent()

@Override

public boolean onTouchEvent(MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_DOWN) {

//让控件出现

}

if (event.getAction() == MotionEvent.ACTION_UP) {

isFlowing = false;

if (flowBottomLL.getVisibility() == View.VISIBLE) {

Message msg = handler.obtainMessage(1);

currentTime = System.currentTimeMillis(); //用时间来设置标志位,在handleMessage方法中判断触发事件的来源是否是同一个.

Bundle bundle = new Bundle();

bundle.putLong("currentTime", currentTime);

msg.setData(bundle);

handler.sendMessageDelayed(msg, 3000);

}

} return super.onTouchEvent(event); }

在Handler中:

private Handler handler = new Handler() {

public void handleMessage(android.os.Message msg) {

if (msg.what == 1) {

if (!isFlowing && msg.getData().getLong("currentTime") == currentTime) {

flowBottomLL.startAnimation(disappearAnimation);

disappearAnimation.setAnimationListener(new AnimationListener() {

@Override

public void onAnimationStart(Animation animation) {}

@Override

public void onAnimationRepeat(Animation animation) {}

@Override

public void onAnimationEnd(Animation animation) {

flowBottomLL.setVisibility(View.GONE);

}

});

}

}

}

};

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值