• 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();
    
    }
    
  • 相关阅读:
    剑指offer-02-替换空格
    剑指offer-03-从尾到头打印链表
    剑指offer-01-二维数组中的查找
    JS-几类函数
    【工具使用】—VSCode
    【工具使用】—Chrome工具使用技巧
    【codeReview】button-disabled
    c语言中结构体位段
    结构体位断
    malloc/free与 new/delete的区别
  • 原文地址:https://www.cnblogs.com/xl-phoenix/p/7192750.html
Copyright © 2020-2023  润新知