欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android 自定义SeekBar 实现分段显示不同背景颜色的示例代码

程序员文章站 2022-07-06 16:41:04
在最近的开发工作中,要实现一个调色板的进度条,seekbar要分成10段显示不同颜色,功夫不负有心人,终于实现了这个功能,下面分享给大家示例图:1.自定义seekbarimport android.c...

在最近的开发工作中,要实现一个调色板的进度条,seekbar要分成10段显示不同颜色,功夫不负有心人,终于实现了这个功能,下面分享给大家
示例图:

Android 自定义SeekBar 实现分段显示不同背景颜色的示例代码

1.自定义seekbar

import android.content.context;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.paint;
import android.graphics.rect;
import android.os.build;
import android.util.attributeset;
import android.widget.seekbar;

/**
 *
 * @time 2020/6/4 18:32
 * <p>
 * 类描述:自定义多颜色的seekbar
 */
public class multicolourseekbar extends seekbar {

 /**
 * 画笔
 */
 private paint mmulticlourpaint;

 /**
 * 刻度线的个数,等分数等于刻度线的个数加1
 */
 private int mmulticlourcount = 9;

 /**
 * 每条刻度线的宽度
 */
 private int mmulticlourwidth = 2;

 /**
 * 刻度线的颜色
 */
 private int mmulticlourcolor = color.white;

 /**
 * 滑块上面是否要显示刻度线
 */
 private boolean isshowtopofthumb = false;

 public multicolourseekbar(context context) {
 super(context);
 init();
 }

 public multicolourseekbar(context context, attributeset attrs) {
 super(context, attrs);
 init();
 }

 public multicolourseekbar(context context, attributeset attrs, int defstyleattr) {
 super(context, attrs, defstyleattr);
 init();
 }

 /**
 * 初始化
 */
 private void init() {
 //创建绘制刻度线的画笔
 mmulticlourpaint = new paint();
 mmulticlourpaint.setcolor(mmulticlourcolor);
 mmulticlourpaint.setantialias(true);

 //api21及以上调用,去掉滑块后面的背景
 if (build.version.sdk_int >= build.version_codes.lollipop) {
  setsplittrack(false);
 }
 }

 /**
 * 重写ondraw方法绘制刻度线
 *
 * @param canvas
 */
 @override
 protected synchronized void ondraw(canvas canvas) {
 super.ondraw(canvas);

 //极限条件校验
 if (getwidth() <= 0 || mmulticlourcount <= 0) {
  return;
 }

 //获取每一份的长度
// int length = (getwidth() - getpaddingleft() - getpaddingright() - mmulticlourcount * mmulticlourwidth) / (mmulticlourcount + 1);
 int length = (getwidth() - getpaddingleft() - getpaddingright()) / (mmulticlourcount + 1);
 //计算刻度线的顶部坐标和底部坐标
 int rulertop = getheight() / 2 - getminimumheight() / 2;
 int rulerbottom = rulertop + getminimumheight();

 //获取滑块的位置信息
 rect thumbrect = null;
 if (getthumb() != null) {
  thumbrect = getthumb().getbounds();
 }
 //绘制刻度线
// for (int i = 1; i <= mmulticlourcount; i++) {
//  //计算刻度线的左边坐标和右边坐标
//  int rulerleft = i * length + getpaddingleft();
//  int rulerright = rulerleft + mmulticlourwidth;
//
//  //判断是否需要绘制刻度线
//  if (!isshowtopofthumb && thumbrect != null && rulerleft - getpaddingleft() > thumbrect.left && rulerright - getpaddingleft() < thumbrect.right) {
//  continue;
//  }
//
//  //进行绘制
//  canvas.drawrect(rulerleft, rulertop, rulerright, rulerbottom, mmulticlourpaint);
// }
 for (int i = 0; i <= mmulticlourcount; i++) {
  int left = getpaddingleft() + i * length;
  int right = left + length;
  if (i % 3 == 0) {
  mmulticlourpaint.setcolor(color.red);
  } else if (i % 3 == 1) {
  mmulticlourpaint.setcolor(color.yellow);
  } else {
  mmulticlourpaint.setcolor(color.blue);
  }
  if (i == 0) {
  canvas.drawcircle(getpaddingleft() + 10, 20, 10, mmulticlourpaint);
  left += 10;
  canvas.drawrect(left, 10, right, 30, mmulticlourpaint);
  } else if (i == mmulticlourcount) {
  right -= 10;
  canvas.drawrect(left, 10, right, 30, mmulticlourpaint);
  canvas.drawcircle(right, 20, 10, mmulticlourpaint);
  } else {
  canvas.drawrect(left, 10, right, 30, mmulticlourpaint);
  }
 }
 }

 /**
 * 设置刻度线的个数
 *
 * @param mrulercount
 */
 public void setrulercount(int mrulercount) {
 this.mmulticlourcount = mrulercount;
 requestlayout();
 }

 /**
 * 设置刻度线的宽度,单位(px)
 *
 * @param mrulerwidth
 */
 public void setrulerwidth(int mrulerwidth) {
 this.mmulticlourwidth = mrulerwidth;
 requestlayout();
 }

 /**
 * 设置刻度线的颜色
 *
 * @param mrulercolor
 */
 public void setrulercolor(int mrulercolor) {
 this.mmulticlourcolor = mrulercolor;
 if (mmulticlourpaint != null) {
  mmulticlourpaint.setcolor(mrulercolor);
  requestlayout();
 }
 }

 /**
 * 滑块上面是否需要显示刻度线
 *
 * @param isshowtopofthumb
 */
 public void setshowtopofthumb(boolean isshowtopofthumb) {
 this.isshowtopofthumb = isshowtopofthumb;
 requestlayout();
 }
}

2.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".mainactivity">

 <com.xinrui.view.multicolourseekbar
 android:id="@+id/seek_bar"
 android:layout_width="350px"
 android:layout_height="wrap_content"
 android:background="@null"
 android:maxheight="20px"
 android:minheight="20px"
 android:max="100"
 android:progress="5"
 android:progressdrawable="@drawable/shape_progress_drawable"
 android:thumb="@drawable/shape_thumb_icon"
 android:thumboffset="-2px"
 app:layout_constraintbottom_tobottomof="parent"
 app:layout_constraintend_toendof="parent"
 app:layout_constraintstart_tostartof="parent"
 app:layout_constrainttop_totopof="parent" />

</android.support.constraint.constraintlayout>

3.shape_progress_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:id="@android:id/background">
 <shape>
  <solid android:color="#e1e8f0" />
  <size android:height="8px" />
  <corners android:radius="8px" />
 </shape>
 </item>

 <item android:id="@android:id/progress">
 <clip>
  <shape>
  <gradient
   android:endcolor="@android:color/transparent"
   android:startcolor="@android:color/transparent" />
  <size android:height="8px" />
  <corners android:radius="8px" />
  </shape>
 </clip>
 </item>
</layer-list>

4.shape_thumb_icon.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle">

 <size
 android:width="5px"
 android:height="40px" />

 <solid android:color="@android:color/widget_edittext_dark" />
</shape>

总结

到此这篇关于android 自定义seekbar 实现分段显示不同背景颜色的文章就介绍到这了,更多相关android 自定义seekbar 背景颜色内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!