当前位置:   article > 正文

Android呼吸灯效果两种实现_android 呼吸灯效果

android 呼吸灯效果

最近项目要做个呼吸灯实现,有两种方式,实现呼吸灯效果本质上都是使用ImageView动画,实现alpha值变化。

第一种方式,直接使用动画实现,代码如下:

    private final int BREATH_INTERVAL_TIME = 1000; //设置呼吸灯时间间隔
    private AlphaAnimation animationFadeIn;
    private AlphaAnimation animationFadeOut;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mode_select_layout);
        breathImageView = (ImageView)findViewById(R.id.breathImageView);

        animationFadeIn = new AlphaAnimation(0.1f, 1.0f);
        animationFadeIn.setDuration(BREATH_INTERVAL_TIME);
//        animationFadeIn.setStartOffset(100);

        animationFadeOut = new AlphaAnimation(1.0f, 0.1f);
        animationFadeOut.setDuration(BREATH_INTERVAL_TIME);
//        animationFadeIn.setStartOffset(100);

        animationFadeIn.setAnimationListener(new Animation.AnimationListener(){

            @Override
            public void onAnimationEnd(Animation arg0) {
                breathImageView.startAnimation(animationFadeOut);
            }

            @Override
            public void onAnimationRepeat(Animation arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationStart(Animation arg0) {
                // TODO Auto-generated method stub

            }

        });

        animationFadeOut.setAnimationListener(new Animation.AnimationListener(){

            @Override
            public void onAnimationEnd(Animation arg0) {
                breathImageView.startAnimation(animationFadeIn);
            }

            @Override
            public void onAnimationRepeat(Animation arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationStart(Animation arg0) {
                // TODO Auto-generated method stub

            }

        });
        breathImageView.startAnimation(animationFadeOut);

    }

第二种方式,使用timer实现,代码如下:

增加动画alpha_fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:fillEnabled="true" >
    <alpha
        android:duration="1500"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />

</set>
alpha_fade_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:fillEnabled="true" >
    <alpha
        android:duration="1500"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />

</set>

Activity中代码:

private ImageView breathImageView;
private Timer timer;
private boolean isOpen = true;
private int index = 0;
private final int BREATH_INTERVAL_TIME = 1000; //设置呼吸灯时间间隔


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mode_select_layout);
    breathImageView = (ImageView)findViewById(R.id.breathImageView);
    startTimer();
}
public Handler handler = new Handler() {
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case 1:
                breathImageView.clearAnimation();
                breathImageView.setAnimation(getFadeIn());
                break;
            case 2:
                breathImageView.clearAnimation();
                breathImageView.setAnimation(getFadeOut());
                break;
        }
        super.handleMessage(msg);
    }
};

private Animation getFadeIn() {
    Animation fadeIn = AnimationUtils.loadAnimation(ModeSelectPage.this,
            R.anim.alpha_fade_in);
    fadeIn.setDuration(BREATH_INTERVAL_TIME);
    fadeIn.setStartOffset(100);
    return fadeIn;
}

private Animation getFadeOut() {
    Animation fadeOut = AnimationUtils.loadAnimation(ModeSelectPage.this,
            R.anim.alpha_fade_out);
    fadeOut.setDuration(BREATH_INTERVAL_TIME);
    fadeOut.setStartOffset(100);
    return fadeOut;
}

private void startTimer() {
    timer = new Timer(true);
    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            if (isOpen) {
                if (index == 2) {
                    index = 0;
                }
                index++;
                Message message = new Message();
                message.what = index;
                handler.sendMessage(message);
            }
        }
    };
    timer.schedule(task, 0, BREATH_INTERVAL_TIME); // 延时0ms后执行,5000ms执行一次
}

@Override
protected void onDestroy() {

    isOpen = false;
    if (timer != null) {
        timer.cancel();// 退出计时器
    }
    timer = null;

    super.onDestroy();

}


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/660260
推荐阅读
相关标签
  

闽ICP备14008679号