0
public void run() {
            runOnUiThread(new Runnable() {
                public void run() {
                    tvTimer.setText("timer=" + String.valueOf(TimeCounter));
                    TimeCounter++;
                    A.setBackgroundColor(123455+TimeCounter*100000);
                }

               });
            }
    }, 0, 1000); 

アプリケーションの実行時間をカウントする役割のタイマーを作成しました。タイマーが上がる限り、背景色を変更したいと考えています。私のスクリプトの何が問題になっていますか?

4

3 に答える 3

0

必要に応じて、色変更用の新しいアプリケーションを作成しました.

public class MainActivity extends Activity {

ImageView iv;

int red = 255, green = 0, blue = 0;
int i = 0;
int a = 30;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    iv = (ImageView) findViewById(R.id.imageView1);
    // m.postScale(2f, 2f);
    Timer t = new Timer();
    t.scheduleAtFixedRate(new TimerTask() {

        public void run() {

            runOnUiThread(new Runnable() {

                public void run() {

                    iv.setBackgroundColor(Color.argb(a, red, green, blue));
                    Log.d("main", "i: " + i + " a:+ " + a + " red: " + red
                            + " green: " + green + " Blue: " + blue);
                    a = a + 30;
                    // set 30 to 60 for more difference
                    if (a > 250) {
                        a = 30;
                        i++;
                        if (i == 1) {
                            red = 0;
                            green = 255;
                            blue = 0;
                        } else if (i == 2) {
                            red = 0;
                            green = 0;
                            blue = 255;

                        } else if (i == 3) {
                            red = 255;
                            green = 0;
                            blue = 0;

                        }

                        if (i == 3) {
                            i = 1;
                        }

                    }

                }

            });
        }
    }, 0, 1000);
}
}

今、幸運をお楽しみください..

于 2014-06-14T13:58:28.440 に答える
0

問題は、 A.setBackgroundColor();で設定されたカラー コードです。

私はこれのシンプルで論理的な解決策を持っています: 1.このようなカラー配列を作ります

int[] colors=new int[]{Color.BLACK,Color.BLUE,Color.GREEN,Color.RED,Color.YELLOW};
   int i=0;
  1. Runnable で i がインクリメントされると、そのインデックスによって配列の色を設定します。

       public void run() {
    
            runOnUiThread(new Runnable() {
    
                public void run() {
    
                    tvTimer.setText("timer=" + String.valueOf(TimeCounter));
    
                    TimeCounter++;
    
                    A.setBackgroundColor(colors[i]);
    
                    i++;
                    if(i==5){
                    i=0;
                    }
                }
    
               });
            }
    }, 0, 1000); 
    
于 2014-06-14T13:09:02.747 に答える