我刚刚实现了一个简单的应用程序,它随机改变屏幕颜色并在文本视图中显示颜色的十六进制代码。经过大量搜索(尤其是 stackoverflow 上的许多帖子),我几乎有了可以满足我需求的代码;只有一个差异。当我单击以重新启动可运行的颜色闪烁时,它会从停止的位置“恢复”,即它最初不会完全延迟,但似乎只会延迟我停止它时可能停止的时间。
我的完整代码如下。目前,我有一个短按启动屏幕颜色闪烁运行,并长按停止它。
package com.example.colorflashingproject;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.os.Handler;
import android.widget.TextView;
public class MainActivity extends Activity {
//initialize a boolean variable (true or false only)
//to check whether the overlay is tinted or not
boolean isTinted = false;
boolean colorIsFlashing = false;
//initialize a handler that we will use to loop a section of code
//that constantly changes the color of the screen
public Handler mHandler = new Handler();
//this creates the UI and screen view, and sets up some
//other aspects of the program
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set the current view in the user interface as our xml file:
setContentView(R.layout.activity_main);
//set the background color, and the initial overlay tint:
((View)findViewById(R.id.background_view)).setBackgroundColor(0xFF000000);
((View)findViewById(R.id.overlay_view)).setBackgroundColor(0x00000000);
//create the overlayView variable that replesents our overlay for tinting
View overlayView = findViewById(R.id.overlay_view);
//implement a method that will listen for a short press
overlayView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//implement function to change tint of our overlay
if (!colorIsFlashing) {
colorIsFlashing = true;
mHandler.post(startColorFlashing);
} else {
}
}
});
//implement a method that will listen for long press:
overlayView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//stop the color flashing if it was started:
if (colorIsFlashing) {
colorIsFlashing = false;
stopColorFlashing();
}
//return true tells the system that the long press registered
//return false doesn't. A false return would cause
//the long click to register as a short click as well
return true;
}
});
}
//this function generates a random color and sets it to the screen
public void randomChangeTint(){
//generate random color components:
int randa = (int) Math.round((int) 0xff * (float) Math.random());
int randr = (int) Math.round((int) 0xff * (float) Math.random());
int randg = (int) Math.round((int) 0xff * (float) Math.random());
int randb = (int) Math.round((int) 0xff * (float) Math.random());
int randColor = Color.argb(randa, randr, randg, randb);
//convert color integer to string:
String strColor = String.format("#%08X", randColor);
//set overlay to our generated random color:
((View)findViewById(R.id.overlay_view)).setBackgroundColor(randColor);
((TextView)findViewById(R.id.textView2)).setText(strColor);
}
//this is our 'runnable' that randomly sets the screen color over
//an interval of time in milliseconds (timeInterval)
public Runnable startColorFlashing = new Runnable(){
//set overlay view to randomly switch colors
int timeInterval=600;
public void run() {
if (colorIsFlashing){
randomChangeTint();
} else {
}
mHandler.postDelayed(this, timeInterval);
}
};
//this method stops the color flashing:
public void stopColorFlashing() {
//pauses the runnable:
mHandler.removeCallbacksAndMessages(startColorFlashing);
//re-initializes the runnable as an empty one
startColorFlashing = new Runnable(){
public void run() {
//empty, nothing here
}
};
}
}
如果我省略
//re-initializes the runnable as an empty one
startColorFlashing = new Runnable(){
public void run() {
//empty, nothing here
}
};
stopColorFlashing() 的一部分,然后连续多次短按屏幕似乎一遍又一遍地启动可运行对象,在停止它然后用短按重新启动它之后,它又开始闪烁,似乎从它停止的地方恢复。我希望它“重新开始”。
我用 Thread.start() 和 Thread.pause() 尝试了一些东西,但无法弄清楚。有没有办法“杀死”或“摧毁”一个可运行的?
我是java新手(我知道matlab),任何想法或建议都值得赞赏。我确信我的代码写得非常糟糕而且效率低下。