Android Drawable setLevel();没有恰当地填写SeekBar

时间:2012-11-07 23:19:01

标签: android drawable seekbar

好的,所以我已经能够自定义一些SeekBar用作条形图类型的图像,但是因为我需要能够在绿色或红色图像(取决于某些值)之间切换,你可以见下文。问题是,无论我在Drawable的setLevel中使用什么值,它都没有适当填充(你可以看到下面的图像作为一个例子,因为绿色条应该基于这两个值更接近右边)

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);

1 个答案:

答案 0 :(得分:0)

我刚刚在之前的编辑中添加了解决方案。