[英]Timer speeds up after every run
我正在為Android開發一個簡單的圖像查看器。 我還需要實現幻燈片顯示。 我已經完成了,除了我有一個煩人的錯誤。 當我運行幻燈片放映(按一個按鈕)時,它將起作用。 但是,當我取消幻燈片放映並再次運行(相同按鈕)時,幻燈片放映會加快。
編輯:添加了固定的代碼,請查看代碼的幻燈片部分中的注釋,以查看需要修復的內容
package csc2002.imageviewer;
//imports
public class MainActivity extends Activity implements OnClickListener {
static Timer timer = new Timer();
int arrayIndex = 0;
int checker=0;
int start,delay = 1800;
boolean toggle;
private static Integer[] imageIds = { //Hard coded array
R.raw.bulbasaur,R.raw.switch_brain,R.raw.quote,R.raw.victory,R.raw.penguins,R.raw.jellyfish,R.raw.koala};
private static final int IMAGE_COUNT = imageIds.length;
@Override
public void onCreate(Bundle savedInstanceState) { // Called when the app is opened
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button back = (Button)findViewById(R.id.backButton);
back.setOnClickListener(this);
Button next = (Button)findViewById(R.id.nextButton);
next.setOnClickListener(this);
Button slideshow = (Button)findViewById(R.id.slideButton);
slideshow.setOnClickListener(this);
displayImage();
}
// responsible for displaying the image and the name of the image.
private void displayImage() { // Displays the image on the screen according the the current array index
ImageView imgView = (ImageView) findViewById(R.id.myimage);
imgView.setImageResource(imageIds[arrayIndex]);
TextView text = (TextView)findViewById(R.id.name);
text.setText(imageIds[arrayIndex]);
}
// This method allows the cycling of images
public void onClick(View v) {
if(v.getId()==(R.id.backButton)){ //Back button
arrayIndex--;
if(arrayIndex==-1){
arrayIndex = IMAGE_COUNT-1;
}
displayImage();
}
else if (v.getId()==(R.id.nextButton)){ //NextButton
arrayIndex++;
}
if(arrayIndex==IMAGE_COUNT){
arrayIndex = 0;
}
displayImage();
if (v.getId()==R.id.slideButton){ //Slideshow button
Button slideshow = (Button)findViewById(R.id.slideButton);
toggle^= true;
if(toggle==true){
slideshow.setText("Stop Slideshow");
}
else{slideshow.setText("Start Slideshow");}
}
// Slideshow functionality
if(toggle==true){
//timer=new Timer(); //this was added in the correct solution(This was the main problem).
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if(toggle==true){
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
arrayIndex++;
if(arrayIndex==IMAGE_COUNT){
arrayIndex=0;
}
displayImage();
}
});
}
}
},start,delay);
}
else if(toggle==false){
//timer.cancel(); This was added to correct the solution
}
}
@Override
protected void onSaveInstanceState(Bundle outState) { //This saves data when the app is rotated
outState.putInt("KEY", arrayIndex);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle save){ //Restores Data after interruption
super.onRestoreInstanceState(save);
arrayIndex = save.getInt("KEY");
displayImage();
}
}
我幾年前做過的一個項目也遇到過類似的問題。 當然,我使用的是Timer
對象,但這是我為解決該問題所做的幾件事:
如果這些建議不起作用,請嘗試發布更多代碼。
編輯
public void onClick(View v) {
//...
if (v.getId()==R.id.slideButton){ //Slideshow button
toggle^= true;
if(toggle==true){
timer = new Timer();
timer.schedule(/*Whatever TimerTask you had here before*/);
}
else{
timer.cancel();
}
}//end if(slide button)
}//end onClick()
通過這種方式,僅在單擊切換按鈕時啟動或結束計時器。 單擊按鈕時將創建計時器,再次單擊時將取消計時器。 永遠不能同時有兩個活動計時器。 我不確定該如何適應您現在的代碼,但是它應該可以與您初次使用的代碼一起使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.