SeekBar中setThumb setProgressDrawable 无法更新Bar图片的问题
2013-11-01 10:48
1046 查看
Android中对于SeekBar的setThumb函数可以设置某个Drawable作为Bar,该函数初始化SeekBar是有效的,但是在程序中使用setThumb动态修改Bar图标时,Bar不能显示。
看了下SeekBar父类AbsSeekBar的源码,发现setThumb仅仅是把AbsSeekBar的mThumb指向新的Drawable,并没有对新的Thumb的Bounds进行设定,但是在draw时,canvas要根据图像的bounds去画,这就出现了问题,输出了一下bounds的Log,发现新的Thumb的bounds的top和bottom都为0,也就是新Bar的高度为0, 这肯定是画不出来的。
解决方法:在setThumb时,对新的Thumb Drawable设置它的Bounds,比如:
Rect oldbound = mOldThumbDrawable.getBounds();
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
int left = (oldbound.left + oldbound.right - width) / 2;
int top = (oldbound.top + oldbound.bottom - height) / 2;
Rect newbound = new Rect(left, top, left + width, top + height);
drawable.setBounds(newbound);
setThumb(drawable);
What I found out is that the drawable doesn't know it's size when setprogressdrawable is called. When it is initially set up, it does know it's size. This means there is a new drawable set to the seekbar, but the size of the drawable is 0, you won't see anything.
The solution is to first get the bounds of the current drawable, then set the new drawable and finally set the bounds again:
看了下SeekBar父类AbsSeekBar的源码,发现setThumb仅仅是把AbsSeekBar的mThumb指向新的Drawable,并没有对新的Thumb的Bounds进行设定,但是在draw时,canvas要根据图像的bounds去画,这就出现了问题,输出了一下bounds的Log,发现新的Thumb的bounds的top和bottom都为0,也就是新Bar的高度为0, 这肯定是画不出来的。
解决方法:在setThumb时,对新的Thumb Drawable设置它的Bounds,比如:
Rect oldbound = mOldThumbDrawable.getBounds();
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
int left = (oldbound.left + oldbound.right - width) / 2;
int top = (oldbound.top + oldbound.bottom - height) / 2;
Rect newbound = new Rect(left, top, left + width, top + height);
drawable.setBounds(newbound);
setThumb(drawable);
What I found out is that the drawable doesn't know it's size when setprogressdrawable is called. When it is initially set up, it does know it's size. This means there is a new drawable set to the seekbar, but the size of the drawable is 0, you won't see anything.
The solution is to first get the bounds of the current drawable, then set the new drawable and finally set the bounds again:
[code]Rect bounds = mySeekBar.getProgressDrawable().getBounds(); mySeekBar.setProgressDrawable(newSeekBarBackground); mySeekBar.getProgressDrawable().setBounds(bounds);
相关文章推荐
- 消除图片在ie中缓存而无法更新的问题
- android开发视频监控时,抓拍图片存储在相册,图片无法显示问题?通知相册更新即可
- 消除图片在ie中缓存而无法更新的问题
- 解决网通英文wiki无法显示图片问题【20100723更新】
- 利用vbs脚本快速修改hosts记录,提供Google Picasa相册无法显示图片问题的傻瓜级方案(2009.11.11更新) 推荐
- 关于图片在窗体显示后,无法更新或删除的问题
- Glide加载相同URL时由于缓存无法更新图片的问题
- 关于向Android项目中的drawable下添加图片却无法使用的问题
- WKInterfaceImage 无法更新图片的问题
- Glide加载相同URL时由于缓存无法更新图片的问题
- 消除图片在ie中缓存而无法更新的问题
- Android 保存图片到相册无法显示的问题
- CMYK图片在IE中无法显示的问题
- 关于 基于maven创建的项目jsp页面无法加载css、js、图片的问题
- ios中调用相册选择图片问题(继续更新中)
- ASP.NET在线用户列表精确版——解决用户意外退出在线列表无法及时更新问题
- 港行Galaxy Nexus(i9250)无法更新4.3的问题的解决
- 纠结了好久的Android SDK无法更新问题
- 解决Android Studio正式版无法在线更新的问题
- html引用图片资源无法显示的问题