2012-02-15 77 views
0

進出口設立一個進度條如下:縮放進度條值

void CProgressBar::add(int ammount) 
{ 
    mProgress += ammount; 
} 

float CProgressBar::get() 
{ 
    float pr = (float)mProgress * 100.0f/(float)mMax; 
    return pr; 
} 

而現在這裏是problem.I'm試圖呈現一個小面雖然它不正確填充它,因爲我可以「T 弄清楚如何正確縮放值:

/* 
    Progress bar box has size of 128x16 
    |-----------| 
    |-----------| 
*/ 
float progress = progressBar->get(); 
float scale = 4.0f; //Here i have it hardcoded although i have to make this generic 

progress *= scale; 
graphics->color(prgColor); 
graphics->renderQd(CRect(x,y,progress,height)); 

這樣的IM親切詢問對此事的一些幫助......

+1

這是一個C++問題,而不是C,C#或其他任何問題。請僅選擇相關標籤 – Shai 2012-02-15 13:10:17

回答

1

你要的寬度之間的線性插值進度爲0%的矩形和進度爲100%的矩形的寬度。 E.G:

float width_0 = 0.f; // or any other number of pixels 
float width_100 = 250.f; // or any other number of pixels 

插值的工作原理如下:

float interpolated_width = (width_100 - width_0) * progress + width_0; 

重要:progress必須在0到1的範圍內!所以你可能想要改變CProgressBar::get()函數或者先除以100。

現在你可以只呈現新寬度的矩形:

graphics->renderQd(CRect(x,y,interpolated_width,height)); 
+0

謝謝,我試了一下,雖然它沒有任何區別:( – user1010005 2012-02-15 13:27:29

+0

@ user1010005:什麼問題?什麼是不工作? – Constantinius 2012-02-15 14:08:54

+0

沒關係,讓它工作。指定每個字符的像素單位,然後乘以比例 – user1010005 2012-02-15 16:53:44

0

你的進度條的寬度是128和progress->獲得()函數返回0和100之間的事情,因此,在不知道你的圖書館詳細信息,看起來你的規模應該是1.28

我假設mMax是完整進度的價值。

對於小整理我會使get()常量,而不是使用C風格的演員。

+0

謝謝,但似乎1.28是不夠的 – user1010005 2012-02-15 13:35:11

+0

並且是nMax是最大值 – user1010005 2012-02-15 13:35:39

+0

我通過指定每個字符的像素單位,然後乘以它與規模 – user1010005 2012-02-15 16:52:18