更改 Android 上自定义垂直搜索栏进度颜色的方法,不适用于线程
我正在尝试更改自定义垂直 SeekBar 的进度颜色 如何获得工作Android 中的垂直 SeekBar? 如果我将此代码放入 View.OnTouchListener 中,它会正常工作:
VerticalSeekBar verticalSeekBar1 = (VerticalSeekBar)verticalSeekBar;
Rect bounds = verticalSeekBar1.getProgressDrawable().getBounds();
int progr = verticalSeekBar1.getProgress();
int max = verticalSeekBar1.getMax();
int width = verticalSeekBar1.getWidth();
int height = verticalSeekBar1.getHeight();
prDr = verticalSeekBar1.getProgressDrawable();
verticalSeekBar1.setProgressDrawable(r.getDrawable(R.drawable.myprogress));
verticalSeekBar1.getProgressDrawable().setBounds(bounds);
verticalSeekBar1.setMax(max);
verticalSeekBar1.setProgress(1);
verticalSeekBar1.setProgress(progr);
verticalSeekBar1.onSizeChanged(width, height);
verticalSeekBar1.refreshDrawableState();
但是,如果我将相同的代码放入另一个线程中,它会部分工作,因为搜索栏的拇指没有到达正确的位置,而是向下移动,但进度的颜色效果很好并到达最后一个位置。 我认为 onSizeChanged 方法可能是导致此问题的原因。我对其进行了调试,发现在这两种情况下, onSizeChanged 方法都有适当的参数,但在线程的代码中,它不会更新搜索栏。
我已经尝试将此代码移至 UI 线程,但也不起作用。
I'm trying to change the progress color of a custom vertical SeekBar How can I get a working vertical SeekBar in Android? and it works fine if I put this code in a View.OnTouchListener:
VerticalSeekBar verticalSeekBar1 = (VerticalSeekBar)verticalSeekBar;
Rect bounds = verticalSeekBar1.getProgressDrawable().getBounds();
int progr = verticalSeekBar1.getProgress();
int max = verticalSeekBar1.getMax();
int width = verticalSeekBar1.getWidth();
int height = verticalSeekBar1.getHeight();
prDr = verticalSeekBar1.getProgressDrawable();
verticalSeekBar1.setProgressDrawable(r.getDrawable(R.drawable.myprogress));
verticalSeekBar1.getProgressDrawable().setBounds(bounds);
verticalSeekBar1.setMax(max);
verticalSeekBar1.setProgress(1);
verticalSeekBar1.setProgress(progr);
verticalSeekBar1.onSizeChanged(width, height);
verticalSeekBar1.refreshDrawableState();
However, if I put this same code in another thread it works partially since the thumb of the seekbar does not go to the right position and instead goes down but the color of the progress works well and gets to the last position.
I figured that the method onSizeChanged is probably causing this. I debugged it and found out that in both scenarios the onSizeChanged method has the appropriate arguments but in the code from the thread it does not update the seekbar.
I already tried moving this code to UI's thread but does not work either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我读到使用 Handler 或 AsyncTask 可以完成更新 UI 的工作,这确实达到了目的。
绘画笔记。
I read that using Handler or AsyncTask can do the work for updating UI and that did the trick.
painting note.