zoukankan      html  css  js  c++  java
  • Android实现动画循环的方式

    每次想到循环播放、重复执行时,脑海中总是冒出在while(true)的实现方式。

    Thread thread = new Thread(new Runnable(){
        public void run(){
            while(true){
            // do animation operation
            }
        }
    }).start();

    但这种方式总给人一种不可靠的感觉。

    为此,在这多记录几种实现方式,方便以后参考。

    第一种:使用属性动画实现(ObjectAnimator)

    Path path = new Path();
    path.addOval(100, -500, 500, -100, Path.Direction.CW);
    ObjectAnimator ivGreenObjectAnimator = ObjectAnimator.ofFloat(ivRed, View.TRANSLATION_X, View.TRANSLATION_Y, path);        ivGreenObjectAnimator.setDuration(5000).setRepeatCount(ValueAnimator.INFINITE);
    ivGreenObjectAnimator.setRepeatMode(ValueAnimator.RESTART);
    ivGreenObjectAnimator.start();
    

    效果:绿色小球沿着椭圆循环运动。

    第二种:使用属性动画实现(ViewPropertyAnimator)

    private void startAnimateByViewAnimationProperty() {
            ViewPropertyAnimator ivGreenAnimate = ivGreen.animate();int[] positions = new int[]{600, 100, 100, 400};
            ivGreenAnimate.translationX(400).setDuration(500).setListener(new ComplexAnimatorListener(ivGreen, positions));
        }
    
    static class ComplexAnimatorListener implements Animator.AnimatorListener {
            View view;
            int[] positions;
            int times = 0;
            public ComplexAnimatorListener(View view, int[] positions) {
                this.view = view;
                this.positions = positions;
            }
    
            @Override
            public void onAnimationStart(Animator animation) {
    
            }
    
            @Override
            public void onAnimationEnd(Animator animation) {
                Log.v("qian", "repeat running!");
                times++;
                if (times % 4 == 1) {
                    view.animate().translationY(positions[0]).setDuration(500).setListener(this);
                } else if (times % 4 == 2) {
                    view.animate().translationX(positions[1]).setDuration(500).setListener(this);
                } else if (times % 4 == 3) {
                    view.animate().translationY(positions[2]).setDuration(500).setListener(this);
                } else
                    view.animate().translationX(positions[3]).setDuration(500).setListener(this);
            }
    
            @Override
            public void onAnimationCancel(Animator animation) {
    
            }
    
            @Override
            public void onAnimationRepeat(Animator animation) {
    
            }
        }

    效果:绿色小球沿着矩形循环运动。

    第三种:使用一般动画实现(TranslateAnimation)

    TranslateAnimation translateAnimation = new TranslateAnimation(-400, -100, -400, -100);
    translateAnimation.setRepeatCount(Animation.INFINITE);
    translateAnimation.setRepeatMode(Animation.REVERSE);
    ivGreen.startAnimation(translateAnimation);

    效果:小球沿着矩形循环运动。

    第四种:使用handler及其callback递归调用实现

        Handler handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if(msg.what==1){
                    startAnimation();
                }
                return false;
            }
        });
    
        private void startAnimation(){
            //do animation operation
            Message message = new Message();
            message.what = 1;
            handler.sendMessage(message);
        }

    一次执行完又执行

    第五种:使用handler及其runnable递归调用实现

    Handler handler = new Handler();
    
    Runnable runnable = new Runnable() {
            @Override
            public void run() {
                ViewPropertyAnimator animate = binding.ivBlue.animate();
                animate.translationXBy(-200).translationYBy(-200).scaleX(2.0f).scaleY(2.0f).
                        setInterpolator(new AccelerateDecelerateInterpolator()).setDuration(500).start();
                printProperty(binding.ivBlue);
                handler.postDelayed(this, 500);
            }
        };

    private void startAnimation(){
      
    handler.postDelayed(this, 500);
    }

    递归调用postDelayed方法。

  • 相关阅读:
    PHP代码审计学习(7)——XSS漏洞
    PHP代码审计学习(6)——命令执行
    PHP代码审计学习(5)——代码执行漏洞
    PHP代码审计学习(4)——SQL漏洞
    PHP代码审计学习(3)——常见的超全局变量
    PHP代码审计学习(2)——php.ini核心配置
    关于Backus-Naur Form巴克斯诺尔范式和扩展巴克斯范式的知识点和相关词语中英文对照
    Python对列表的相关操作(与JAVA对比)
    MergeSort归并排序和利用归并排序计算出数组中的逆序对
    统计字符串中每种字符出现的评率(HashMap中getOrDefault(K, V)方法的使用)
  • 原文地址:https://www.cnblogs.com/xiangxing/p/6036245.html
Copyright © 2011-2022 走看看