如何正确使用setProgressDrawable()?

发布于 2024-08-22 01:44:35 字数 175 浏览 4 评论 0原文

我在为我的 ProgressBar 设置新的 Drawable 时遇到问题。

如果我在 onCreate() 方法中使用 setProgressDrawable() ,效果会很好。但是,当我尝试在处理程序后回调中调用相同的方法时,它不起作用并且进度条消失。

有人可以解释这种行为吗?我该如何解决这个问题?

I am having problem with setting a new Drawable to my ProgressBar.

If I use the setProgressDrawable() inside onCreate() method it works great. But when I try to call the same method inside a Handler post callback it doesn't work and the progressbar disapears.

Can someone explain this behaviour? How can I solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

呆橘 2024-08-29 01:44:35
downloadingBar.setProgress(0); 

Drawable progressDrawable = getResources().getDrawable(R.drawable.download_progressbar_pause_bg);

progressDrawable.setBounds(downloadingBar.getProgressDrawable().getBounds()); 

downloadingBar.setProgressDrawable(progressDrawable); 

downloadingBar.setProgress(mCurrentPercent);  
  1. 首先,您应该将进度重置为零
  2. 设置进度可绘制边界
  3. 设置新进度可绘制
  4. 设置新进度
downloadingBar.setProgress(0); 

Drawable progressDrawable = getResources().getDrawable(R.drawable.download_progressbar_pause_bg);

progressDrawable.setBounds(downloadingBar.getProgressDrawable().getBounds()); 

downloadingBar.setProgressDrawable(progressDrawable); 

downloadingBar.setProgress(mCurrentPercent);  
  1. First you should reset the progress to zero
  2. Set the progress drawable bounds
  3. Set new progress drawable
  4. Set new progress
静若繁花 2024-08-29 01:44:35

我自己遇到了这个问题,我设法让它工作:)

我使用了 AsyncTask 来处理后台任务/线程,但其想法应该与使用 Runnable/Handler 相同(尽管 AsyncTask 确实感觉更好我认为)。

所以,这就是我所做的...将 setContentView(R.layout.my_screen); 放入 onPostExecute 方法中! (即,而不是 onCreate 方法)

所以代码看起来像这样:

public class MyScreen extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.my_screen); !!! Don't setContentView here... (see bottom)

    new MySpecialTask().execute();
}

private int somethingThatTakesALongTime() {
    int result;
    // blah blah blah
    return result;
}

private void updateTheUiWithResult(int result) {
    // Some code that changes the UI
    // For exampe:
    TextView myTextView = (TextView) findViewById(R.id.result_text);
    myTextView.setText("Result is: " + result);

    ProgressBar anyProgressBar = (ProgressBar) findViewById(R.id.custom_progressbar);
    anyProgressBar.setProgressDrawable(res.getDrawable(R.drawable.progressbar_style));
    anyProgressBar.setMax(100);
    anyProgressBar.setProgress(result);       
}

private class MySpecialTask extends AsyncTask<String, Void, Integer> {
    ProgressDialog mProgressDialog;

    @Override
    protected void onPreExecute() {
        mProgressDialog = ProgressDialog.show(MyScreen.this, "", "Calculating...\nPlease wait...", true);
    }
    @Override
    protected Integer doInBackground(String... strings) {
        return somethingThatTakesALongTime();
    }

    @Override
    protected void onPostExecute(Integer result) {
        mProgressDialog.dismiss();
        setContentView(R.layout.my_screen); // setContent view here... then it works...
        updateTheUiWithResult(result);
    }
}
}

说实话,为什么你需要在 onPostExecute 中调用 setContentView我不知道......但这样做意味着您可以为进度条设置自定义样式(并且它们不会在您身上消失!)

Bumped into this problem myself and I managed to get it working :)

I used the AsyncTask to handle the background tasks/threads, but the idea should be the same as using Runnable/Handler (though AsyncTask does feel nicer imo).

So, this is what I did... put setContentView(R.layout.my_screen); in the onPostExecute method! (ie. instead of the onCreate method)

So the code looks something like this:

public class MyScreen extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.my_screen); !!! Don't setContentView here... (see bottom)

    new MySpecialTask().execute();
}

private int somethingThatTakesALongTime() {
    int result;
    // blah blah blah
    return result;
}

private void updateTheUiWithResult(int result) {
    // Some code that changes the UI
    // For exampe:
    TextView myTextView = (TextView) findViewById(R.id.result_text);
    myTextView.setText("Result is: " + result);

    ProgressBar anyProgressBar = (ProgressBar) findViewById(R.id.custom_progressbar);
    anyProgressBar.setProgressDrawable(res.getDrawable(R.drawable.progressbar_style));
    anyProgressBar.setMax(100);
    anyProgressBar.setProgress(result);       
}

private class MySpecialTask extends AsyncTask<String, Void, Integer> {
    ProgressDialog mProgressDialog;

    @Override
    protected void onPreExecute() {
        mProgressDialog = ProgressDialog.show(MyScreen.this, "", "Calculating...\nPlease wait...", true);
    }
    @Override
    protected Integer doInBackground(String... strings) {
        return somethingThatTakesALongTime();
    }

    @Override
    protected void onPostExecute(Integer result) {
        mProgressDialog.dismiss();
        setContentView(R.layout.my_screen); // setContent view here... then it works...
        updateTheUiWithResult(result);
    }
}
}

To be honest, why you need to call setContentView in onPostExecute I have no idea... but doing so means you can set custom styles for your progress bars (and they don't disappear on you!)

时光是把杀猪刀 2024-08-29 01:44:35

也许您将代码放在非主线程的线程中。
如果您想使用 UI,则必须在主线程中执行此操作:)

Maybe you put the code in a thread which is not main thread.
If you want to work with the UI, you must do that in the main thread :)

七七 2024-08-29 01:44:35

我也面临同样的问题,但就我而言,这是由于使用了 Drawable.mutate() 方法。当我删除该方法时,它开始正常工作。我还注意到这个问题存在于 api level-21(lollipop) 以下。

I was also facing the same issue but in my case it is due to the use of Drawable.mutate() method. When i removed that method it started working fine. I also noticed that this issue exist below api level-21(lollipop).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文