原文网址:http://blog.csdn.net/jdsjlzx/article/details/7804080

最近在项目中使用到了seekbar和progressbar,且必须按照设计要求来进行设置,如下图。要实现这个效果就必须对这两个控件进行自定义。
 
 一,SeekBar
  
   一开始要实现这个效果参考网上的自定义方法根本无法达到这个效果,没办法只能投机取巧了。

1,背景刻度的图片我是用了一个ImageView,然后在ImageView上放一个SeekBar。因为是个定制的平板应用,分辨率是限定的1280*768,所以我使用的是AbsoluteLayout这样ImageView和SeekBar的位置和大小都是固定的了,估计在其他布局中这样使用会有问题。

2,在布局文件中的代码如下:

  1. <ImageView
  2. android:layout_width="400dip"
  3. android:layout_height="95dip"
  4. android:layout_x="830dip"
  5. android:layout_y="484dip"
  6. android:src="@drawable/seekbar_background_5" //刻度图片
  7. android:scaleType="centerCrop"
  8. android:background="@null"
  9. />
  10. <SeekBar
  11. android:id="@+id/sensor_sensitivity"
  12. android:layout_width="360dip"
  13. android:layout_height="64dip"
  14. android:layout_x="850dip"
  15. android:layout_y="498dip"
  16. android:progressDrawable="@drawable/suretouch_seekbar_img"
  17. android:thumb="@drawable/suretouch_seekbar_thumb"
  18. style="?android:attr/progressBarStyleHorizontal"
  19. android:paddingLeft="5dip"
  20. android:paddingRight="5dip"
  21. android:paddingBottom="2dip"
  22. android:maxHeight="1dip"  //注意:一定得设置进度条的高度,不然进度条会很高。
  23. android:minHeight="1dip"
  24. android:max="100"
  25. android:progress="0"
  26. />
  1. <ImageView
  2. android:layout_width="400dip"
  3. android:layout_height="95dip"
  4. android:layout_x="830dip"
  5. android:layout_y="484dip"
  6. android:src="@drawable/seekbar_background_5" //刻度图片
  7. android:scaleType="centerCrop"
  8. android:background="@null"
  9. />
  10. <SeekBar
  11. android:id="@+id/sensor_sensitivity"
  12. android:layout_width="360dip"
  13. android:layout_height="64dip"
  14. android:layout_x="850dip"
  15. android:layout_y="498dip"
  16. android:progressDrawable="@drawable/suretouch_seekbar_img"
  17. android:thumb="@drawable/suretouch_seekbar_thumb"
  18. style="?android:attr/progressBarStyleHorizontal"
  19. android:paddingLeft="5dip"
  20. android:paddingRight="5dip"
  21. android:paddingBottom="2dip"
  22. android:maxHeight="1dip"  //注意:一定得设置进度条的高度,不然进度条会很高。
  23. android:minHeight="1dip"
  24. android:max="100"
  25. android:progress="0"
  26. />

3,自定义滑块,在drawable文件中加入自定义的xml文件。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <!-- 按下状态 -->
  4. <item
  5. android:state_pressed="true"
  6. android:drawable="@drawable/seekbar_block" />
  7. <!-- 普通无焦点状态 -->
  8. <item
  9. android:state_focused="false"
  10. android:state_pressed="false"
  11. android:drawable="@drawable/seekbar_block" />
  12. </selector>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <!-- 按下状态 -->
  4. <item
  5. android:state_pressed="true"
  6. android:drawable="@drawable/seekbar_block" />
  7. <!-- 普通无焦点状态 -->
  8. <item
  9. android:state_focused="false"
  10. android:state_pressed="false"
  11. android:drawable="@drawable/seekbar_block" />
  12. </selector>

4,自定义进度条的颜色,同样在drawable中加入自定义需要的xml文件。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <layer-list
  3. xmlns:android="http://schemas.android.com/apk/res/android">
  4. <item android:id="@android:id/progress">
  5. <clip>
  6. <shape>
  7. <gradient
  8. android:startColor="@color/big_title"
  9. android:centerColor="@color/big_title"
  10. android:endColor="@color/big_title"
  11. />
  12. </shape>
  13. </clip>
  14. </item>
  15. </layer-list>
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <layer-list
  3. xmlns:android="http://schemas.android.com/apk/res/android">
  4. <item android:id="@android:id/progress">
  5. <clip>
  6. <shape>
  7. <gradient
  8. android:startColor="@color/big_title"
  9. android:centerColor="@color/big_title"
  10. android:endColor="@color/big_title"
  11. />
  12. </shape>
  13. </clip>
  14. </item>
  15. </layer-list>

5,设置滑块的位置,也就是当滑动滑块后只能让其停在刻度上,要现实这个效果我采用的方法是当滑块停止的时候判断当前的值,比如第二个刻度是25,这里在0到25中去个中间数比如13,也就是当滑块滑到大于13小于25到50的中间数时就setProgress(25),这样就设定在25的位置也就是第二个刻度位置。后面的以此类推。seekbar的事件中有个OnStopTrackingTouch,代码如下:

  1. public void onStopTrackingTouch(SeekBar seekBar) {
  2. // TODO Auto-generated method stub
  3. int seekProgress = mSeekBar.getProgress();
  4. if(seekProgress<13){
  5. mSeekBar.setProgress(0);
  6. }else if(seekProgress>=13 && seekProgress<38){
  7. mSeekBar.setProgress(25);
  8. }else if(seekProgress>=38 && seekProgress<63){
  9. mSeekBar.setProgress(50);
  10. }else if(seekProgress>=63 && seekProgress<88){
  11. mSeekBar.setProgress(75);
  12. }else if(seekProgress>=88){
  13. mSeekBar.setProgress(100);
  14. }
  15. }
  1. public void onStopTrackingTouch(SeekBar seekBar) {
  2. // TODO Auto-generated method stub
  3. int seekProgress = mSeekBar.getProgress();
  4. if(seekProgress<13){
  5. mSeekBar.setProgress(0);
  6. }else if(seekProgress>=13 && seekProgress<38){
  7. mSeekBar.setProgress(25);
  8. }else if(seekProgress>=38 && seekProgress<63){
  9. mSeekBar.setProgress(50);
  10. }else if(seekProgress>=63 && seekProgress<88){
  11. mSeekBar.setProgress(75);
  12. }else if(seekProgress>=88){
  13. mSeekBar.setProgress(100);
  14. }
  15. }

对于ProgressBar的设置同样是采用一个ImageView为背景(外围的黑框),在ImageView上放一个ProgressBar控件,然后自定义进度条的颜色。只是在调整它们之间的位置和大小的时候比较费时点,不管怎样已经达到了想要的效果。

【转】 为SeekBar滑块设置固定值以及自定义Seekbar,progressbar样式--不错的更多相关文章

  1. Android课程---Android 如何用滑杆(SeekBar)组件设置图片颜色的透明度(转)

    Android系统支持的颜色是由4个值组成的,前3个为RGB,也就是我们常说的三原色(红.绿.蓝),最后一个值是A,也就是Alpha.这4个值都在0~255之间.颜色值越小,表示该颜色越淡,颜色值越大 ...

  2. MTU介绍以及在windows和linux下怎么设置MTU值

    最大传输单元MTU(Maximum Transmission Unit,MTU)是指一种通信协议的某一层上面所能通过的最大数据包大小(以字节为单位).最大传输单元这个参数通常与通信接口有关(网络接口卡 ...

  3. Android 设置alpha值来制作透明与渐变效果的实例

    Android系统支持的颜色是由4个值组成的,前3个为RGB,也就是我们常说的三原色(红.绿.蓝),最后一个值是A,也就是Alpha.这4个值都在0~255之间.颜色值越小,表示该颜色越淡,颜色值越大 ...

  4. Format a Property Value 设置属性值的格式

    In this lesson, you will learn how to set a display format and an edit mask to a business class prop ...

  5. el-cascader组件根据最后一级向上找到父级并设置默认值

    vue + elementUI项目中,el-cascader级联选择器使用频率非常高,一些基本使用方法可以参考elementUI官方文档,本文主要研究当接口只返回最后一级id时,如何向上找出所有父级数 ...

  6. ng-option指令使用记录,设置默认值需要注意

    ng-options一般有以下用法: 数组作为数据源: label for value in array select as label for value in array label group ...

  7. 《Entity Framework 6 Recipes》中文翻译系列 (14) -----第三章 查询之查询中设置默认值和存储过程返回多结果集

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 3-6在查询中设置默认值 问题 你有这样一个用例,当查询返回null值时,给相应属性 ...

  8. EF Core1.0 CodeFirst为Modell设置默认值!

    当我们使用CodeFirst时,有时候需要设置默认值! 如下 ; public string AdminName {get; set;} = "admin"; public boo ...

  9. 使用mysql 命令行,增加 ,删除 字段 并 设置默认值 及 非空

    使用mysql 命令行,增加 ,删除 字段 并 设置默认值 及 非空 添加 alter table table_name add field_name field_type; 添加,并设置默认值,及非 ...

随机推荐

  1. "_Default"同时存在于两个dll文件中的解决办法

    编译器错误消息:CS0433: 类型“_Default”同时存在于“c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Fi ...

  2. HTML 5 Audio/Video DOM buffered 属性

    1.实例1获取视频第一段缓冲范围部分,以秒计: myVid=document.getElementById("video1"); alert("Start: " ...

  3. HTML5 FileReader读取Blob对象API详解

    使用FileReader对象,web应用程序可以异步的读取存储在用户计算机上的文件(或者原始数据缓冲)内容,可以使用File对象或者Blob对象来指定所要读取的文件或数据.其中File对象可以是来自用 ...

  4. sql -以零作除数

    将表达式改为: case when b=0 then 0 else a/b end

  5. 【转】 ios开发之倒计时实现的两种方法

    原文:http://blog.csdn.net/kylinbl/article/details/8972261 方法1:使用NSTimer来实现 主要使用的是NSTimer的scheduledTime ...

  6. iOS: 在代码中使用Autolayout (2) – intrinsicContentSize和Content Hugging Priority【转】

    原文:http://www.mgenware.com/blog/?p=491 接上文:iOS: 在代码中使用Autolayout (1) – 按比例缩放和优先级. 我们继续来看在代码中使用Autola ...

  7. iOS多线程的初步研究(八)-- dispatch队列

    GCD编程的核心就是dispatch队列,dispatch block的执行最终都会放进某个队列中去进行,它类似NSOperationQueue但更复杂也更强大,并且可以嵌套使用.所以说,结合bloc ...

  8. Codeforces 543B Destroying Roads(最短路)

    题意: 给定一个n个点(n<=3000)所有边长为1的图,求最多可以删掉多少条边后,图满足s1到t1的距离小于l1,s2到t2的距离小于l2. Solution: 首先可以分两种情况讨论: 1: ...

  9. 【POJ2406】【KMP】Power Strings

    Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...

  10. 中级Perl第二章习题

    2. 4. 1. 习题1 [15 分钟] 写一个程序从命令行取一个文件清单, 然后用grep 把那些文件大小在1000 字节以内的文件找出来.用map 把这个清单里的每个字串前加四个空格并在 字串后面 ...