SeekBar中setThumb setProgressDrawable 无法更新Bar图片的问题

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:

Rect bounds = mySeekBar.getProgressDrawable().getBounds();
mySeekBar.setProgressDrawable(newSeekBarBackground);
mySeekBar.getProgressDrawable().setBounds(bounds);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值