我正在尝试创建一个小的动画,以平滑地改变背景颜色。我的问题是,它仅显示最后一个值(100,这意味着它直接变为红色背景)。我不知道为什么我创建的while循环无法实现所有值(以便显示平滑的彩色动画)

新代码(几乎可以正常运行,但请问如何停止动画)

imageButton_info.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){


                final Handler handler = new Handler();

                Runnable ChangeBackgroundRunnable = new Runnable() {

                    @Override
                    public void run() {
                        number++;
                        float[] hsvColor = {0, 1, 1};
                        hsvColor[0] = 360f * number / 100;
                        color.setBackgroundColor(Color.HSVToColor(hsvColor));

                        handler.postDelayed(this, 80);
                        if (number >=100)
                            number = 1;

                    }
                };
                number = 0;
                handler.removeCallbacks(ChangeBackgroundRunnable);
                handler.postDelayed(ChangeBackgroundRunnable, 0);

        }
    });


码:

 public void onClick(View v){

            try {
                while (number<=100) {

                        number=number+1;

                        float[] hsvColor = {0, 1, 1};
                        hsvColor[0] = 360f * number / 100;
                        color.setBackgroundColor(Color.HSVToColor(hsvColor));

                        Thread.sleep(10);
                }
            }catch(Exception e){

                //New exception
                Log.e("Camera Error!",e.getMessage());

            }


谢谢您的提前答复...

最佳答案

包装了@ gabe-sechan和@ jesse-buss发布的答案

来自ValueAnimator之上的设备SDK的HONEYCOMB支持。因此,在该SDK级别以下,我们将使用@ gabe-sechan建议。检查以下代码。

    私人void executeBackgroundChange(){
        //处理程序和可运行状态,以在蜂窝下面的设备sdk中运行动画。
        mHandler = new Handler();
        mChangeBackgroundRunnable = new Runnable(){
            @Override
            公共无效run(){
                数字++;

                float [] hsvColor = {0,1,1};
                hsvColor [0] = 360f *数字/ 100;
                mContainer.setBackgroundColor(Color.HSVToColor(hsvColor));

                mHandler.postDelayed(this,500);
                如果(数字== 100)
                    数字= 0;
            }
        };
        数字= 0;
        mHandler.removeCallbacks(mChangeBackgroundRunnable);
        mHandler.postDelayed(mChangeBackgroundRunnable,0);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    私人void executeBackgroundChangeUsingValueAnimator(){
        colorAnimation = ValueAnimator.ofObject(新ArgbEvaluator(),getResources()。getColor(R.color.red),getResources()。getColor(R.color.blue));
        colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){
            @Override
            public void onAnimationUpdate(最终ValueAnimator动画师){
                mContainer.setBackgroundColor((Integer)animator.getAnimatedValue());
            }
        });
        colorAnimation.setRepeatCount(ValueAnimator.INFINITE);
        colorAnimation.setDuration(10 * 1000);
        colorAnimation.start();
    }


添加以下方法,并在单击某些动画时停止动画,请调用以下方法。

    私人无效stopBackgroundChangeAnimation(){
        如果(android.os.Build.VERSION.SDK_INT> = android.os.Build.VERSION_CODES.HONEYCOMB){
            如果(colorAnimation!= null && colorAnimation.isRunning())
                colorAnimation.end();
        }其他{
            如果(mHandler!= null && mChangeBackgroundRunnable!= null)
                mHandler.removeCallbacks(mChangeBackgroundRunnable);
        }
    }


检查github project供参考。

10-06 14:03