开发者

any way to make a progress bar that when the value is 0, it's 100%?

I'm working on a debt tracking app, and have need for a progress bar that as the current debt goes down, the progress goes up. The only way I can think to do this would be to fudge the numbers around so that the progress bar MAX is set to 0, and then change the starting debt to be a negitive number, as follows

ProgressBar.setMax(0);

Integer startingdebt = -1000;  // 1000$ owed
ProgressBar.setProgress(currentlyowed);  //say, -500

so you start with -1000, and add 500$ to it if you paid it. I don't know if this would work though, because I'm sure the progressbar control has a set minimum of 0 already, so you can't set max the same...is there a way to do this?

Edit: Thanks for the answers guys, but I found an easier way: to share with others, here's my final code block:

    double startingamount = (this.c.getDouble(this.c.getColumnIndex(DbAdapter.KEY_STARTINGAMOUNT)));
    double currentamount = (this.c.getDouble(this.c.getColumnIndex(DbAdapter.KEY_CURRENTAMOUNT)));
    currentdebt.setText(formatter.format(currentamount));
    double progresspaid = new Double(startingamount-currentamount);
    double progresspercentage = new Double((progresspaid / startingamount)*100);
    int progresspercent = (int)progresspercentage;
    progressbar.setText(Integer.toString(progresspercent)+"%");
    progressbar.setMax(100);
    progressbar.setProgress(progresspercent);

the key was to get a variable that subtracts the current amount from the starting amount, and then divides that by the starting amount.

double progresspaid = new Double(startingamount-currentamount);
double progresspercentage = new Double((progresspaid / startingamount)*100);

Thanks again though, I really开发者_运维百科 appreciate people helping me learn Java and android development, I'm a VB.net developer so some of this is still foreign to me.


While this may not be ideal (I don't use Android and avoid UI coding ;-), the numbers and percentages can be calculated with a little math -- it may be problematic if the raw value is itself displayed on the progress bar.

The percent going from 0 ... 100 is:

percent = (current - min) / (max - min)

(When min = 0, this is trivially percent = current / max.)

And the percent going from 100 to 0 is:

percentReverse = 100 - percent

or, expanded:

percentReverse = 100 - ((current - min) / (max - min))

Example, current = -800, max = 0, min = -1000:

percent = (-800 - -1000) / (0 - -1000) = 200/1000 = 20 (% done)
percentReverse = 100 - 20 = 80 (% left)

Another example, current = 0, max = 2000, min = -1000:

percent = (0 - -1000) / (2000 - -1000) = 1000/3000 = 33 (% done)
percentReverse = 100 - 33 = 66 (% left)

Happy coding.


It's probably easiest just to track the debt as a positive integer and subtract the payments, but in case you have to keep it negative, maybe try something like this:

// initialize
int startingDebt = -1000;
int currentlyOwed = startingDebt;
ProgressBar.setMax(-startingDebt);

// make a payment
int payment = 500;
currentlyOwed += payment
ProgressBar.setProgress(-currentlyOwed);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜