0

我有一门课应该以白色显示屏幕并淡出很多颜色。

public class SecondActivity extends Activity implements Runnable {

protected PowerManager.WakeLock mWakeLock;

boolean isrunning = true;
int red = 0, blue = 0, green = 0;

@Override
public void onCreate(Bundle savedInstanceState) .... etc

TextView btnstart = (TextView) findViewById(R.id.btnstart);
    btnstart.setTypeface(font1);
    btnstart.setOnClickListener(new OnClickListener() {
@Override
        public void onClick(View v) {

            MediaPlayer mediaPlayer = MediaPlayer.create(
                    getApplicationContext(), R.raw.button);
            try {
                mediaPlayer.start();
                mediaPlayer.setLooping(false);
            } catch (Exception e) {
                e.printStackTrace();
            }

            {
                run();
            }
            ;
        }
    });

然后我的可运行

@Override
public void run() {
    while (isrunning) {

        try {

            red = red + 1;
            blue = blue + 2;
            green = green + 3;
            if (green > 240){

                red = 0;
                blue = 0;
                green = 0;
            }

            LinearLayout colorchanger = (LinearLayout) findViewById(R.id.colorchange);
            colorchanger.setBackgroundColor(Color.argb(255, red, blue,
                    green));
            Thread.sleep(100);
                } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

当我按下应用程序强制关闭的按钮时,我遇到了以下问题。如果我取出“ while(isrunning)”并将其替换为 if (isrunning ),则一切正常,但我希望在单击按钮后颜色不断变化。

我确定我做错了什么......

也许用布局做不是最好的方法,我必须用一个矩形(屏幕大小)来做?并在 onDraw 期间更改颜色?

4

1 回答 1

2

处理程序非常适合这种情况。

// Initialize your handler:
Handler h = new Handler();

// Start fading:
h.postDelayed(color_fade, 0);


// Set up your runnable:     
private Runnable color_fade = new Runnable(){
   public void run(){
      /*
       *  Do your color and layout changes here.
       */
      h.postDelayed(this, 100);  // loop with 100 ms delay.
   }
};
于 2013-05-16T04:06:58.997 回答