2013-03-11 3 views
-2

Я написал game play screen с индикатором выполнения. Я только хочу, чтобы индикатор выполнения работал в течение 4 секунд, но он не останавливается.progress bar running continue

Я написал класс MainActivity с номером gamelayout. В этом макете я хочу добавить progress bar.

Почему индикатор прогресса продолжает работать?

public class MainActivity extends Activity { 
private static final String TAG = null; 
public int ROWS = 10; 
public int COLS = 10; 
public String a = ""; 
public String x = ""; 
public static int textSize; 
public static int wordListTextSize; 
public boolean gameFinish = false; 
public Timer timer; 
public Vector<Integer> coordinate_X1 = new Vector<Integer>(); 
public Vector<Integer> coordinate_Y1 = new Vector<Integer>(); 
public Vector<Integer> coordinate_X2 = new Vector<Integer>(); 
public Vector<Integer> coordinate_Y2 = new Vector<Integer>(); 
RelativeLayout winPopUp = null; 
public int cellWidth = 0; 
public int cellHeight = 0; 
public TextView tv1; 
public char[][] charArray; 
public Vector<String> wordList; 
public String[] tempWordList; 
public RelativeLayout gameLayout; 
//public RelativeLayout mainLayout; 
public int colorCounter = 0; 
public int noOfSelectedWords = 0; 
public int totalNoOfWords = 0; 

public int sec = 0; 
public int hour = 0; 
public int min = 0; 
boolean gameStarted = false; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.gamescreen); 



    // here is a relative layout , we get id of this layout and add progress bar 
    // to this reletive layout 

    final RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main); 
    mainLayout.post(new Runnable() { 

     public void run() { 
      // TODO Auto-generated method stub 
      getLayoutInflater().inflate(R.layout.loading, mainLayout); 
      ProgressBar spinner = (ProgressBar) findViewById(R.id.progressBar1); 
      spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000, 
        android.graphics.PorterDuff.Mode.MULTIPLY); 
     new DrawGame().execute(MainActivity.this); 
     } 
    });//here is code of add progress bar how we stop progress bar which is running 
        //continue 

} 

public void initPlay() { 
    ROWS = ScreenConstants.ROWS; 
    cellWidth = cellHeight = (((RelativeLayout) findViewById(R.id.gameLayoutBg)) 
      .getHeight() * 98/100)/ROWS; 
    System.out.println("Height " 
      + findViewById(R.id.gameLayoutBg).getHeight()); 
    cellHeight = cellHeight - cellHeight/ROWS; 
    cellWidth = 200; 
    COLS = ScreenConstants.SCREENWIDTH/cellWidth; 
    COLS = 10; 
    gamePlay(); 
} 

public void gamePlay() { 
    Game game = new Game(); 
    game.getGame(ROWS, COLS, this, ScreenConstants.MAX_LENGTH); 
    gameLayout = (RelativeLayout) findViewById(R.id.gameLayoutBg); 
    RelativeLayout.LayoutParams rlparams = new RelativeLayout.LayoutParams(
      280, 300); 
    rlparams.topMargin = 30; 
    tv1 = new TextView(this); 
    // tv1.setLayoutParams(rlparams); 
    tv1.setId(1); 

    charArray = new char[ROWS][COLS]; 
    int index = 0; 
    for (int i = 0; i < ROWS; i++) { 
     for (int j = 0; j < COLS; j++) { 
      charArray[i][j] = game.charArray[index]; 
      x = x + " " + charArray[i][j]; 
      tv1.setText(x); 
      tv1.setTextSize(20); 
      // tv1.setText(""+charArray[i][j]); 
      Log.d(TAG, "VALUE OF TEXT BOX " + tv1); 
      gameLayout.removeAllViews(); 
      gameLayout.addView(tv1, rlparams); 
      index++; 
     } 
    } 

    wordList = game.wordList; 
    TextView tv = (TextView) findViewById(R.id.wordListLayoutBg1); 

    tempWordList = Utility.getStringArray(wordList); 
    for (int i = 0; i < tempWordList.length; i++) { 

     a = a + " " + tempWordList[i]; 
    } 
    tv.setText(a); 
    tv.setTextColor(getResources().getColor(R.color.black)); 
    // } 
    totalNoOfWords = wordList.size(); 
    noOfSelectedWords = 0; 

} 


    private class DrawGame extends AsyncTask<MainActivity, Void, Boolean> { 

    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     super.onPreExecute(); 
    } 


@Override 
    protected void onProgressUpdate(Void... values) { 
     // TODO Auto-generated method stub 
     super.onProgressUpdate(values); 
     initPlay(); 
    } 


@Override 
    protected void onPostExecute(Boolean result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 
    //initPlay(); 

    } 


@Override protected Boolean doInBackground(MainActivity... obj) 
{ 

    //initPlay(); 
    try { 
     publishProgress(); 
     Thread.sleep(3000); 
    } catch (Exception e) { 

    } 


     return true; 
    } 
    } 
+0

При отправке справки о размещении кода отличный, но даже лучше сначала его обрезать каждый который не связан с той помощью, которую вы ищете. Чем меньше шума вы предоставляете, тем проще для кого-то вам помочь. – mah

ответ

0

Предполагая R.id.progressBar1 есть прогресс бар вы хотите иметь дело с, когда вы будете готовы продолжить, просто:

ProgressBar spinner = (ProgressBar) findViewById(R.id.progressBar1); 
spenner.setVisibility(View.INVISIBLE); 
0

использование onPreExecute для показа ProgressBar и отклонить его в onPostExecute Изменение AsyncTask, как :

private class DrawGame extends AsyncTask<MainActivity, Void, Boolean> { 
ProgressBar spinner; 
    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     Show Progress Bar here... 
      spinner = (ProgressBar)MainActivity.this. 
            findViewById(R.id.progressBar1); 
     spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000, 
        android.graphics.PorterDuff.Mode.MULTIPLY); 

      spinner.setVisibality(0); //<<< make Visible here 
     super.onPreExecute(); 
    } 
@Override 
    protected void onPostExecute(Boolean result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 
     spinner.setVisibility(8); //<<< make in-Visible here 
    //initPlay(); 

    } 
//your code here...