2016-04-29 44 views
0

我已經創建了一個donutprogressBar,並且我設置了進度條值,並且它第一次正常工作,但是當我第二次設置值比進度條在第一值和第二值,我想的進度被設置conatsntant到第二值當我第二次設置進度值時,DonutProgressBar值會波動

here is my code : 

strMalePercentage = jsonObject.getJSONObject("male").getString("percentage_manglik_present"); 
         txtPercentage.setText("Manglik Male"+strMalePercentage); 
         Progress(strMalePercentage); 

public void onClick(View v) { 
     int id = v.getId(); 
     if (id == R.id.btn_ManglikMatchMale){ 
      linearMale.setVisibility(View.VISIBLE); 
      linearFemale.setVisibility(View.GONE); 

      imgMatchGender.setBackgroundResource(R.drawable.ic_male1); 
      txtPercentage.setText("Manglik Male"); 
      Progress(strMalePercentage); 
      btnMale.setBackgroundColor(getResources().getColor(R.color.textPrimary)); 
      btnMale.setBorderColor(getResources().getColor(R.color.sky_blue)); 
      btnFemale.setBackgroundColor(getResources().getColor(R.color.light_pink)); 
      btnFemale.setTextColor(R.color.textPrimary); 
      btnMale.setTextColor(R.color.gray_light); 
     }else 
     { linearMale.setVisibility(View.GONE); 
      linearFemale.setVisibility(View.VISIBLE); 

      imgMatchGender.setBackgroundResource(R.drawable.ic_female1); 
      txtPercentage.setText("Manglik Female "); 
      Progress(strFemalePercentage); 
      btnFemale.setBackgroundColor(getResources().getColor(R.color.textPrimary)); 
      btnFemale.setBorderColor(getResources().getColor(R.color.light_pink)); 
      btnMale.setBackgroundColor(getResources().getColor(R.color.sky_blue)); 
      btnMale.setTextColor(R.color.textPrimary); 
      btnFemale.setTextColor(R.color.gray_light); 

     } 

    } 


public void Progress(final String Percentage){ 
     Timer timer; 


     donutProgress = (DonutProgress) findViewById(R.id.donut_progress_male); 

     timer = new Timer(); 
     timer.schedule(new TimerTask() { 
      @Override 
      public void run() { 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         double d = Double.parseDouble(Percentage); 
         int i = (int) d; 
         donutProgress.setProgress(0); 
         donutProgress.setProgress(i); 


        } 
       }); 
      } 
     },1000,100); 
    } 
+0

嗨,你可能沒有塞子d第一次運行後的計時器,以便它保持重置 – iestync

+0

@iestync如何停止計時器? – bipin

+0

如果你在Progress方法外保留了一個定時器的引用,那麼你可以直接調用'timer.cancel();'再次啓動日程表之前 – iestync

回答

1

也許這樣的事情,所以計時器參考在外面,你可以取消它,如果已經運行

private Timer timer; 

public void progress(final String Percentage){ 

    donutProgress = (DonutProgress) findViewById(R.id.donut_progress_male); 

    if (timer == null) { 
     timer = new Timer(); 
    } else { 
     timer.cancel(); 
    } 
    timer.schedule(new TimerTask() { 

     @Override 
     public void run() { 
      runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        double d = Double.parseDouble(Percentage); 
        int i = (int) d; 
        donutProgress.setProgress(0); 
        donutProgress.setProgress(i); 


       } 
      }); 
     } 
    },1000,100); 
} 
相關問題