android自定义seekbar样式,SeekBar动态改变颜色 -- 关于自定义SeekBar

本文介绍了如何在Android中自定义SeekBar样式,包括创建seekbar_define_style.xml和seekbar_thumb.xml文件来设置背景和滑块样式。核心内容是动态改变进度条颜色,通过包装GradientDrawable在ScaleDrawable中实现,从而在Java代码中可以访问并修改颜色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

————————————————————————————

|   本文链接:http://my.oschina.net/lifj/blog/153569  |

|   (防止盗链)                                                   |

————————————————————————————

案例使用的图片如下:

3f5f8a7b5d08ac634dcf70a5a1073b80.png          

18a6d780cddb88a14509797bf09dd8c8.png                

ac8a06d273c34e08f6f14cb255c2c071.png

1.在res/drawable目录下新增一个xml风格文件,seekbar_define_style.xml

xmlns:android="http://schemas.android.com/apk/res/android">

android:id="@android:id/background"

android:drawable="@drawable/hou"/>

android:id="@android:id/progress"

android:drawable="@drawable/qian"/>

android:id="@android:id/secondaryProgress"

android:drawable="@drawable/qian"/>

2.在res/drawable下定义个seekbar_thumb.xml文件

android:state_focused="true"

android:state_pressed="true"

android:drawable="@drawable/ic_launcher" />

android:state_focused="false"

android:state_pressed="false"

android:drawable="@drawable/orbino_icon_pack_006" />

android:state_focused="true"

android:state_pressed="false"

android:drawable="@drawable/ios" />

android:state_focused="true"

android:drawable="@drawable/ios"/>

3.在res/layut下定义布局资源文件seekbar_define.xml

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical"

>

android:id="@+id/seekbar_tetview_one"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="SeekBar自定义"

/>

android:id="@+id/seekbar_tetview_two"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="SeekBar拖动时信息提示"

/>

android:layout_width="321px"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:maxHeight="20px"

android:minHeight="20px"

android:paddingLeft="18px"

android:paddingRight="18px"

android:max="100"

android:progressDrawable="@drawable/seekbar_define_style"

android:thumb="@drawable/seekbar_thumb"

android:id="@+id/seekBar"/>

效果如下:

7db21992b85be9841779040866714f82.png

二。使用颜色显示,和尚面是一样的,只有我们定义颜色资源来替代图片资源文件seekbar_define_color_style.xml:如下:

android:paddingTop="3px"

android:paddingBottom="3px">

android:startColor="#ffffffff"

android:centerColor="#ff000000"

android:endColor="#ff808A87"

android:centerY="0.45"

android:angle="270"/>

android:paddingTop="3px"

android:paddingBottom="3px" >

android:startColor="#ffffffff"

android:centerColor="#ffFFFF00"

android:endColor="#ffAABD00"

android:centerY="0.45"

android:angle="270"/>

之后再SeekBar标签使用如下属性进行引入:其他保持不变

android:progressDrawable="@drawable/seekbar_define_color_style"

执行效果:

6d925eb16f1b94f5d4d93adfed806938.png

这些都不是本文的核心。本文的核心是:如何动态的设置进度条的颜色?

关于这个问题,网上的解答几乎没有。这里,我照抄一段谷歌官方的回答:

(原链接:https://groups.google.com/forum/#!topic/android-developers/vPAxGwiSM-o) When using the progress drawable provided by the Android platform it

should not be possible to change the color dynamically as you cannot

access the GradientDrawable. The resource ID for the GradientDrawable

used as progress indication is defined in com.android.internal.R and

you do not have access to it from your application. Changing colors

dynamically should be possible if you use your own drawable as

progress drawable.

The platform's (horizontal) progress drawable is a clipped

GradientDrawable inside a LayerDrawable (see

http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob_plain;f=core/res/res/drawable/progress_horizontal.xml;hb=HEAD).

You can use it in your application and replace the progress drawable

with it.

However, to get access to the GradientDrawable from Java you

should package the GradientDrawable inside a ScaleDrawable (instead of

packaging it inside of a ClipDrawable).Then you can get access to it

via ScaleDrawable.getDrawable().

--------------------------------------------------------------------- I meant something like the following:

In progress_horizontal replace ClipDrawable with id 'progress' by a

ScaleDrawable:

android:startColor="#ffffd300"

android:centerColor="#ffffb600"

android:centerY="0.75"

android:endColor="#ffffcb00"

android:angle="270"

/>

From Java you can access the GradientDrawable inside the ScaleDrawable

as follows:

ProgressBar progBar;

ScaleDrawable sd = (ScaleDrawable) ((LayerDrawable)

progBar.getProgressDrawable()).findDrawableByLayerId(R.id.progress);

GradientDrawable gd = (GradientDrawable) sd.getDrawable();

gd.setColor(Color.BLUE);

=============================================================

Setting own ProgressDrawable from xml works. You only need to tell

Android that you want to use your own drawable by specifying your own

drawable as android:progressDrawable:

android:id="@+id/ProgressBar01"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

style="?android:attr/progressBarStyleHorizontal"

android:progressDrawable="@drawable/progress_horizontal"

/>

注意到上面紫色的那段英文:但是,要获得访问到GradientDrawable从Java

应该打包GradientDrawable的的一个ScaleDrawable内(而不是

它打包里面一个ClipDrawable)。

回头看看大家通用的方法:都是里面包一个。谷歌的建议是用包起来。这样就可以动态的设置颜色了。

好不好使呢,代码写起来:代码在上面的摘录中都有。自己写一段,果然好使!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值