Android的绘制对象执行setLevel();未正确填写搜索栏 [英] Android Drawable setLevel(); not filling SeekBar appropriately

查看:291
本文介绍了Android的绘制对象执行setLevel();未正确填写搜索栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我已经能够自定义一些搜索栏的用作条形图类型的图像,但因为我需要能够向绿色或红色图像之间进行切换(取决于特定的值),你可以见下文。问题是,不管我在执行setLevel的绘制对象不正确填写所使用的值(你可以看到下面的图片为例子,因为绿坝应该更接近于基于这两个值右)

下面是code为设置这整个MTD委员会酒吧节一节的,我不知道有多少$ C $的c您需要看到,所以我只是决定后所有本节。

 无效setupMTDBarSection(){
    //获取当前天的日期和数量当月
    日历CAL = Calendar.getInstance();
    INT NUMBEROFDAYS = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    今天的SimpleDateFormat =新的SimpleDateFormat(DD);
    字符串的currentdate = today.format(新的Date());    //从preferences MTD目标价值
    串goalString = preferences.getString(keyMonthlyGoal,0);
    浮mtdGoalFloat = Float.valueOf(goalString);
    整数mtdGoal =(INT)mtdGoalFloat;
    MTDGoalValue.setText(NumberFormat.getCurrencyInstance()格式(mtdGoalFloat));    //获取当前MTD值
    串mtdString = preferences.getString(keyMTDValue,0);
    浮mtdValueFloat = Float.valueOf(mtdString);
    整数mtdValue =(INT)mtdValueFloat;
    MTDCurrentProgress.setText(NumberFormat.getCurrencyInstance()格式(mtdValueFloat));    //做一些数学,以确定是否众议员低于/高于每日目标
    整数dailyGoal = mtdGoal / NUMBEROFDAYS;
    整数currentDayGoal = dailyGoal * Integer.valueOf(的currentdate);    如果(mtdValue> = currentDayGoal){
        MTDGreenTrack.setLevel(mtdValue);
        MTDProgressBar.setProgressDrawable(MTDGreenTrack);
        MTDProgressBar.setMax(mtdGoal);
        MTDProgressBar.setProgress(mtdValue);
    }
    其他{
        MTDRedTrack.setLevel(mtdValue);
        MTDProgressBar.setProgressDrawable(MTDRedTrack);
        MTDProgressBar.setMax(mtdGoal);
        MTDProgressBar.setProgress(mtdValue);
    }    //添加比例MTD文本
    NumberFormat的percentFormat = NumberFormat.getPercentInstance();
    浮动百分比= mtdValueFloat / mtdGoalFloat;
    串百分比= percentFormat.format(百分比);
    MTDPercentText.setText((+百分比+));
    //设置MTD指示器
    MTDIndicator.setMax(NUMBEROFDAYS);
    MTDIndicator.setProgress(Integer.valueOf(的currentdate));
    MTDIndicator.setOnTouchListener(新View.OnTouchListener(){
        @覆盖
        公共布尔onTouch(查看视图,MotionEvent motionEvent){
            返回true;
        }
    });
}

修改

我相信我发现这个问题。很显然,当你调用不止一次然后setProgressDrawable更需要,因为它失去这是previously那里的格式重新绘制时使用的图像。另外,不要真的需要在绘制每次设置的级别为好,它匹配与搜索栏的进度值。下面是code,它为我的作品至今

 矩形边界= MTDProgressBar.getProgressDrawable()的getBounds()。
        MTDProgressBar.setProgressDrawable(MTDGreenTrack);
        MTDProgressBar.getProgressDrawable()的setBounds(边界)。
        MTDProgressBar.setProgress(mtdValue);
        MTDProgressBar.setMax(mtdGoal);


解决方案

我只是增加了previous编辑解决方案。

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

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.

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


}

EDIT

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

解决方案

I just added the solution in a previous edit.

这篇关于Android的绘制对象执行setLevel();未正确填写搜索栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆