1

好的,所以我已经能够自定义一些 SeekBar 以用作条形图类型的图像,但是因为我需要能够在绿色或红色图像(取决于某些值)之间切换,您可以在下面看到。问题是,无论我在 setLevel 中为 Drawable 使用什么值,它都不能正确填充(您可以查看下图作为示例,因为根据这两个值,绿色条应该更靠近右侧)

在此处输入图像描述

下面是设置整个 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;
        }
    });


}
4

1 回答 1

0

我相信我找到了问题所在。显然,当您多次调用 setProgressDrawable 时,您需要重新绘制使用的图像,因为它会丢失以前存在的格式。另外,也不需要每次都设置drawable的级别,它与Seekbar的进度值相匹配。以下是到目前为止对我有用的代码

Rect bounds = MTDProgressBar.getProgressDrawable().getBounds();
        MTDProgressBar.setProgressDrawable(MTDGreenTrack);
        MTDProgressBar.getProgressDrawable().setBounds(bounds);
        MTDProgressBar.setProgress(mtdValue);
        MTDProgressBar.setMax(mtdGoal);
于 2012-11-08T17:57:24.683 回答