2012-11-07 36 views
1

好吧,我已經能夠自定義一些SeekBar的作爲條形圖類型的圖像,但是因爲我需要能夠在綠色或紅色圖像之間切換(取決於某些值)你可以在下面看到。問題是,無論我在drawable的setLevel中使用什麼值,它都沒有適當地填充(您可以看到下面的圖像作爲示例,因爲根據這兩個值,綠色條應該更接近右側)Android Drawable setLevel();沒有適當填充SeekBar

enter image description here

下面是該設置這整個MTD委員會酒吧節一節的代碼,我不知道你會有多大的碼的需要看,所以我只是決定後所有本節。

void setupMTDBarSection() { 
    //Get Current Date and Number of Days in Current Month 
    Calendar cal = Calendar.getInstance(); 
    int numberOfDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH); 
    SimpleDateFormat today = new SimpleDateFormat("dd"); 
    String currentDate = today.format(new Date()); 

    //Get MTD Goal value from Preferences 
    String goalString = preferences.getString("keyMonthlyGoal", "0"); 
    float mtdGoalFloat = Float.valueOf(goalString); 
    Integer mtdGoal = (int)mtdGoalFloat; 
    MTDGoalValue.setText(NumberFormat.getCurrencyInstance().format(mtdGoalFloat)); 

    //Get Current MTD Value 
    String mtdString = preferences.getString("keyMTDValue", "0"); 
    float mtdValueFloat = Float.valueOf(mtdString); 
    Integer mtdValue = (int)mtdValueFloat; 
    MTDCurrentProgress.setText(NumberFormat.getCurrencyInstance().format(mtdValueFloat)); 

    //Do some math to determine if the Rep is below/above the daily goal 
    Integer dailyGoal = mtdGoal/numberOfDays; 
    Integer currentDayGoal = dailyGoal * Integer.valueOf(currentDate); 

    if (mtdValue >= currentDayGoal) { 
     MTDGreenTrack.setLevel(mtdValue); 
     MTDProgressBar.setProgressDrawable(MTDGreenTrack); 
     MTDProgressBar.setMax(mtdGoal); 
     MTDProgressBar.setProgress(mtdValue); 
    } 
    else { 
     MTDRedTrack.setLevel(mtdValue); 
     MTDProgressBar.setProgressDrawable(MTDRedTrack); 
     MTDProgressBar.setMax(mtdGoal); 
     MTDProgressBar.setProgress(mtdValue); 
    } 

    //Add Percentage to MTD Text 
    NumberFormat percentFormat = NumberFormat.getPercentInstance(); 
    float percent = mtdValueFloat/mtdGoalFloat; 
    String percentage = percentFormat.format(percent); 
    MTDPercentText.setText("(" + percentage + ")"); 


    //Setup MTD Indicator 
    MTDIndicator.setMax(numberOfDays); 
    MTDIndicator.setProgress(Integer.valueOf(currentDate)); 
    MTDIndicator.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View view, MotionEvent motionEvent) { 
      return true; 
     } 
    }); 


} 

編輯

我相信我發現這個問題。顯然,當你多次調用setProgressDrawable時,你需要重新繪製使用的圖像,因爲它丟失了以前的格式。此外,每次繪製drawable時都不需要設置該級別,它與Seekbar的進度值相匹配。以下是我目前使用的代碼

Rect bounds = MTDProgressBar.getProgressDrawable().getBounds(); 
     MTDProgressBar.setProgressDrawable(MTDGreenTrack); 
     MTDProgressBar.getProgressDrawable().setBounds(bounds); 
     MTDProgressBar.setProgress(mtdValue); 
     MTDProgressBar.setMax(mtdGoal); 
+0

很難說,因爲我們沒有你的實際價值,但我的直覺是你沒有設置最大的價值。如果最小值是5000,最大值是6000,那麼「最大值」應該是1000.但正如我所說的,很難說。 – dmon

+0

如果您是指進度條最大值和進度的值,則這些值將顯示在左側和右側。因此,在圖中的示例中,最大值爲6000,當前進度值爲5000.我沒有設置最小值,但如果這樣會產生差異,我可以將其設置爲0。 –

回答

0

我剛剛在以前的編輯中添加了解決方案。