原文网址: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. JMeter 使用

    Jmeter工具和其他性能工具在原理上完全一致,工具包含4个部分: (1)负载发生器:用于产生负载,通常以多线程或是多进程的方式模拟用户行为. (2)用户运行器:通常是一个脚本运行引擎,用户运行器附加 ...

  2. 【Android】Android的优点和不足之处

    随着Android的越来越红火,不少应聘Android开发的人,难免会被问到这样的问题,就是这个平台的优点,当然有优点也会有缺点的, 下面是我从网上总结出来的,希望对大家应聘Android开发有所帮助 ...

  3. java返回参数中几种常见的方法

    1.有参数有返回值 public class text_1 {    1)创建add方法 public int add(int i, int j) {        int res = i + j;  ...

  4. iBatis 的删除一条记录

    Student.xml 设置删除参数的类型,可以是一个对象的 <delete id="delStudent" parameterClass="int" & ...

  5. Axiom3D学习日记 4.地形,天空,雾

    首先需要引用Axiom.SceneManagers.Octree.dll. 地形: 载入地形配置,从一个文件中. scene.LoadWorldGeometry( "Terrain.xml& ...

  6. 重要性!important

    我们在做网页代码的时,有些特殊的情况需要为某些样式设置具有最高权值,怎么办?这时候我们可以使用!important来解决. 如下代码: p{color:red!important;} p{color: ...

  7. 如何获取App当前版本号

    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary]; NSString *currentVersion = [infoDic ...

  8. hdoj 2040

    #include<stdio.h>int i,j,s1,s2;int cha(int a,int b){ s1=0; s2=0;   for(i=1;i<a;i++)   {    ...

  9. SGU 125.Shtirlits

    时间限制:0.25s 空间限制:4M 题意: 有N*N的矩阵(n<=3),对所有i,j<=n有G[i][j]<=9,定义f[i][j]为G[i][j]四周大于它的数的个数(F[i][ ...

  10. html 标签的嵌套规则

    1. 块元素可以包含内联元素或某些块元素,但内联元素却不能包含块元素,它只能包含其它的内联元素: <div><h1></h1><p></p> ...