简体   繁体   English

Android Drawable setLevel(); 没有适当地填充 SeekBar

[英]Android Drawable setLevel(); not filling SeekBar appropriately

Okay so I've been able to customize a few SeekBar's to be used as a Bar Graph type of image, but since I need to be able to switch between a Green or Red Image (depending on certain values) which you can see below.好的,所以我已经能够自定义一些 SeekBar 以用作条形图类型的图像,但是因为我需要能够在绿色或红色图像(取决于某些值)之间切换,您可以在下面看到。 The problem is that regardless of what value I use in the setLevel for the Drawable it doesn't fill appropriately (you can see the image below for an example since the green bar should be closer to the right based on the two values)问题是,无论我在 Drawable 的 setLevel 中使用什么值,它都不会正确填充(您可以查看下图作为示例,因为基于这两个值,绿色条应该更靠近右侧)

在此处输入图片说明

Below is the code for the section that setups this entire MTD Commission bar section, I don't know how much of the code you would need to see so I just decided to post all of this section.下面是设置整个 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;
        }
    });


}

I believe I found the issue.我相信我发现了问题。 Apparently when you call setProgressDrawable more than once then you need to re-draw the image that is used since it looses the format that was previously there.显然,当您多次调用 setProgressDrawable 时,您需要重新绘制所使用的图像,因为它会丢失以前存在的格式。 Also, don't really need to set the level each time of the drawable as well, it matches up with the progress value of the Seekbar.此外,也不需要每次都设置 drawable 的级别,它与 Seekbar 的进度值相匹配。 Below is the code that works for me so far以下是迄今为止对我有用的代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM