解决方案 »

  1.   

    把动画放到handler里,发消息执行它。
    不要在线程里对ui进行操作。
      

  2.   


    layout.setBackgroundColor(R.color.red);应该也有问题,同动画,ui不能在线程修改。
      

  3.   

    稍微改下loading和handler应该就可以了   private void loading() {
            Runnable r = new Runnable() {
                public void run() {
                    try {
                        Timer timer = new Timer();
                        TimerTask task=new TimerTask() {
                            @Override
                            public void run() {
                                switch ((int) time / 1000) {
                                case 1:
                                case 2:
                                case 3:
                                case 4:
                                  mHandler.sendEmptyMessage((int) time / 1000);
                                default:
                                    break;
                                }
                                time+=1000l;
                            }
                        };
                        if(time<10000l){
                            timer.schedule(task, 1000);
                        }else{
                            mHandler.sendEmptyMessage(-1);
                        }
                        //Thread.sleep(10000);
                    } catch (Exception e) {
                        // Log.e(TAG, e.toString());
                    }
                }
            };
            new Thread(r).start();
        }
     
        private class MessageHandler extends Handler {
     
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case -1:
                    Intent intent = new Intent(WelcomeActivity.this,
                            MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    finish();
                    break;
                case 1:
                    layout.setBackgroundColor(R.color.red);
                    ObjectAnimator.ofFloat(progressImage, "rotation", 0f, 360f).setDuration(1000).start();
                    break;
                case 2:
                    layout.setBackgroundColor(R.color.brown);
                    ObjectAnimator.ofFloat(progressImage, "rotation", 0f, 360f).setDuration(1000).start();
                    break;
                case 3:
                    layout.setBackgroundColor(R.color.yellow);
                    ObjectAnimator.ofFloat(progressImage, "rotation", 0f, 360f).setDuration(1000).start();
                    break;
                case 4:
                    layout.setBackgroundColor(R.color.green);
                    ObjectAnimator.ofFloat(progressImage, "rotation", 0f, 360f).setDuration(1000).start();
                    break;
                default:
                    super.handleMessage(msg);
                }
            }
        }
      

  4.   

    private void loading()
    {
    Runnable r = new Runnable()
    {
    public void run()
    {
    try
    {
    final Timer timer = new Timer();
    final TimerTask task = new TimerTask()
    {
    @Override
    public void run()
    {
    while (time < 10000)
    {// 10秒内
    time += 1000;
    switch ((int) time / 1000)
    {
    case 1:
    //第一次因为time==0所以执行
    ObjectAnimator.ofFloat(progressImage, "rotation", 0f, 360f).setDuration(10000).start();
    break;
    default:
    mHandler.sendEmptyMessage((int) time / 1000);
    break;
    }

    if (time < 10000l)
    {
    timer.schedule(task, 1000);
    }
    else
    {
    mHandler.sendEmptyMessage(-1);
    }
    }
    }
    };
    }
    catch (Exception e)
    {
    // Log.e(TAG, e.toString());
    }
    }
    };
    new Thread(r).start();
    } private class MessageHandler extends Handler
    { @Override
    public void handleMessage(Message msg)
    {
    switch (msg.what)
    {
    case -1:
    Intent intent = new Intent(WelcomeActivity.this,
    MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    finish();
    break;
    case 2:
    layout.setBackgroundColor(R.color.red);
    break;
    case 3:
    layout.setBackgroundColor(R.color.brown);
    break;
    case 4:
    layout.setBackgroundColor(R.color.yellow);
    break;
    case 5:
    layout.setBackgroundColor(R.color.green);
    break;
    //TODO:增加更多颜色6、7、8、9、10
    default:
    super.handleMessage(msg);
    }
    }
    }
      

  5.   

    有while也是不行的...
    12-29 08:03:45.525: E/AndroidRuntime(575): android.util.AndroidRuntimeException: Animators may only be run on Looper threads
      

  6.   

    有while也是不行的...
    12-29 08:03:45.525: E/AndroidRuntime(575): android.util.AndroidRuntimeException: Animators may only be run on Looper threads
    我少些了一个消息private void loading()
    {
        Runnable r = new Runnable()
        {
            public void run()
            {
                try
                {
                    final Timer timer = new Timer();
                    final TimerTask task = new TimerTask()
                    {
                        @Override
                        public void run()
                        {
                            while (time < 10000)
                            {// 10秒内
                                time += 1000;
                                switch ((int) time / 1000)
                                {
                                case 1:
                                    //第一次因为time==1所以执行
                                    mHandler.sendEmptyMessage(1);
                                    break;
                                default:
                                    mHandler.sendEmptyMessage((int) time / 1000);
                                    break;
                                }
                                 
                                if (time < 10000l)
                                {
                                    timer.schedule(task, 1000);
                                }
                                else
                                {
                                    mHandler.sendEmptyMessage(-1);
                                }
                            }
                        }
                    };
                }
                catch (Exception e)
                {
                    // Log.e(TAG, e.toString());
                }
            }
        };
        new Thread(r).start();
    }
     
    private class MessageHandler extends Handler
    {
     
        @Override
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
            case -1:
                Intent intent = new Intent(WelcomeActivity.this,
                        MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                finish();
                break;
            case 1:
                 ObjectAnimator.ofFloat(progressImage, "rotation", 0f, 360f).setDuration(10000).start();
            case 2:
                layout.setBackgroundColor(R.color.red);
                break;
            case 3:
                layout.setBackgroundColor(R.color.brown);
                break;
            case 4:
                layout.setBackgroundColor(R.color.yellow);
                break;
            case 5:
                layout.setBackgroundColor(R.color.green);
                break;
                //TODO:增加更多颜色6、7、8、9、10
            default:
                super.handleMessage(msg);
            }
        }
    }
    原因已经说过了……