您的位置:首页 > 编程语言 > PHP开发

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:
[code]Rect bounds = mySeekBar.getProgressDrawable().getBounds();
mySeekBar.setProgressDrawable(newSeekBarBackground);
mySeekBar.getProgressDrawable().setBounds(bounds);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: