当我第二次设置进度值时,DonutProgressBar值会波动

时间:2016-04-29 07:58:18

标签: java android

我创建了一个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);
    }

1 个答案:

答案 0 :(得分: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);
}