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

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

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

alphaAnimation appearAnimation = new AlphaAnimation(0, 1);
		appearAnimation.setDuration(500);

                disappearAnimation = new AlphaAnimation(1, 0);
                disappearAnimation.setDuration(500);
</pre> 第三步:想让控件出现时</p><p><pre name="code" class="java">if (flowBottomLL.getVisibility() == View.GONE) {
flowBottomLL.startAnimation(appearAnimation);
                            flowBottomLL.setVisibility(View.VISIBLE);
                        }

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

</pre></p><p></p><p></p><pre name="code" class="java">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);
                        }<pre name="code" class="java">} 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);
                        }
                    });
                }
            }
        }
    };




已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页