美女天下 發表於 2019-11-28 10:48:00

Android开发 SeekBar基本使用与开发点子记录

<h1><span style="color: rgba(0, 128, 128, 1)">版权声明</span></h1>
<p>本文来自博客园,作者:观心静&nbsp;,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/11947457.html</p>
<div>本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。</div>
<h1><span style="color: rgba(0, 128, 128, 1)">前言</span></h1>
<p><span style="color: rgba(0, 128, 128, 1)">  <span style="color: rgba(0, 0, 0, 1)">开发记录博客不是讲解使用博客,更多的是功能与各个点子的记录</span></span></p>
<h1><span style="color: rgba(0, 128, 128, 1)">基本使用</span></h1>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">SeekBar
      </span><span style="color: rgba(255, 0, 0, 1)">android:layout_width</span><span style="color: rgba(0, 0, 255, 1)">="match_parent"</span><span style="color: rgba(255, 0, 0, 1)">
      android:layout_height</span><span style="color: rgba(0, 0, 255, 1)">="wrap_content"</span><span style="color: rgba(255, 0, 0, 1)">
      android:max</span><span style="color: rgba(0, 0, 255, 1)">="200"</span><span style="color: rgba(255, 0, 0, 1)">
      android:maxHeight</span><span style="color: rgba(0, 0, 255, 1)">="3dp"</span><span style="color: rgba(255, 0, 0, 1)">
      android:minHeight</span><span style="color: rgba(0, 0, 255, 1)">="3dp"</span><span style="color: rgba(255, 0, 0, 1)">
      android:progressDrawable</span><span style="color: rgba(0, 0, 255, 1)">="@drawable/seekbar_bg"</span><span style="color: rgba(255, 0, 0, 1)">
      android:thumb</span><span style="color: rgba(0, 0, 255, 1)">="@drawable/seekbar_thumb_bg"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span></pre>
</div>
<p><strong>注意,如果你发现进度的高度或者宽度设置后依然很大,可以尝试使用<span style="color: rgba(0, 0, 0, 1)">maxHeight与minHeight 来调整</span></strong></p>
<p><span style="color: rgba(0, 0, 0, 1)">xml属性:</span></p>
<ul>
<li><span style="color: rgba(0, 0, 0, 1)">android:max="200"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 设置进度最大值</span></li>
<li><span style="color: rgba(0, 0, 0, 1)">android:progressDrawable="@drawable/seekbar_bg"&nbsp; 设置进度条的背景</span></li>
<li><span style="color: rgba(0, 0, 0, 1)">android:thumb="@drawable/seekbar_thumb_bg"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 设置进度条上圆点的背景</span></li>
<li><span style="color: rgba(0, 0, 0, 1)">android:progress="10"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 设置当前进度值</span></li>
<li><span style="color: rgba(0, 0, 0, 1)">android:min="0"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; 设置最小值</span></li>
<li><span style="color: rgba(0, 0, 0, 1)">android:splitTrack="false"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 设置滑块圆点是否背景透明,在用图片设置thumb后,thumb的四周可能会出现正方形的白色背景。设置此属性可以取消这个背景</span></li>
<li><span style="color: rgba(0, 0, 0, 1)">android:padding=“0dp”  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>请注意</strong>,SeekBar 实际上是有内边距的,如果你需要填充满效果就需要设置成0dp。另外如果你需要圆点比今天背景大,也是调整这个内边距<br></span></li>
</ul>
<p>&nbsp;</p>
<p>Api:</p>
<ul>
<li>setProgress(int value) 设置滑块的位置</li>
<li>setMax(int value) 设置进度条的最大长度</li>
<li>setOnSeekBarChangeListener(OnSeekBarChangeListener l)这个主要是监听进度改变 里面包含3个回调方法: <strong>onProgressChanged</strong>进度条变化 <strong>onStartTrackingTouch</strong>(SeekBar seekBar) 监听开始拖动滚动条时的操作 <strong>onStopTrackingTouch</strong>(SeekBar seekBar) 监听停止拖动滚动条的操作</li>
<li>setSecondaryProgress(int secondaryProgress) 设置缓冲的进度</li>
<li><span style="color: rgba(0, 0, 0, 1)">setProgressDrawable(Drawable d) 在代码上设置进度条背景,<strong>请注意</strong>,这里可以直接设置GradientDrawable或者<span style="color: rgba(0, 0, 0, 1)">Drawable</span>。但是,设置后只会成为背景。如果需要有进度背景那就需要导入LayerDrawable。<br></span></li>
</ul>
<div>
<h2>进度条背景xml</h2>
<p><span style="color: rgba(0, 0, 0, 1)">seekbar_bg.xml</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;?</span><span style="color: rgba(255, 0, 255, 1)">xml version="1.0" encoding="utf-8"</span><span style="color: rgba(0, 0, 255, 1)">?&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">layer-list </span><span style="color: rgba(255, 0, 0, 1)">xmlns:android</span><span style="color: rgba(0, 0, 255, 1)">="http://schemas.android.com/apk/res/android"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">item </span><span style="color: rgba(255, 0, 0, 1)">android:id</span><span style="color: rgba(0, 0, 255, 1)">="@android:id/background"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">shape</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">solid </span><span style="color: rgba(255, 0, 0, 1)">android:color</span><span style="color: rgba(0, 0, 255, 1)">="#ff51495e"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">shape</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">item</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">item </span><span style="color: rgba(255, 0, 0, 1)">android:id</span><span style="color: rgba(0, 0, 255, 1)">="@android:id/secondaryProgress"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">clip</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">shape</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">solid </span><span style="color: rgba(255, 0, 0, 1)">android:color</span><span style="color: rgba(0, 0, 255, 1)">="#f9062a"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">shape</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">clip</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">item</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">item </span><span style="color: rgba(255, 0, 0, 1)">android:id</span><span style="color: rgba(0, 0, 255, 1)">="@android:id/progress"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">clip</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">shape</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">solid </span><span style="color: rgba(255, 0, 0, 1)">android:color</span><span style="color: rgba(0, 0, 255, 1)">="#2db334"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">shape</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">clip</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">item</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">layer-list</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>根据属性解释一下3个背景的对应功能,原则上如果你不需要显示预加载的功能,可以将<span style="color: rgba(255, 0, 0, 1)"><span style="color: rgba(0, 0, 255, 1)">@android:id/secondaryProgress</span></span> 设置为透明,或者不写</p>
<p><span style="color: rgba(255, 0, 0, 1)">android:id<span style="color: rgba(0, 0, 255, 1)">="@android:id/background"</span></span> 是没有如何拖动或者设置的背景,<strong><span style="color: rgba(255, 0, 0, 1)">请注意!</span>这个属性下面是没有用<span style="color: rgba(0, 0, 0, 1)">&lt;clip&gt;</span>包裹的,如果不小心包裹了会出现没有背景的bug</strong></p>
<p><span style="color: rgba(255, 0, 0, 1)">android:id<span style="color: rgba(0, 0, 255, 1)">="@android:id/secondaryProgress"</span></span> 是次级进度背景,类似你在看视频的时候,视频的进度条会有预加载的进度. <strong><span style="color: rgba(255, 0, 0, 1)">请注意!</span><span style="color: rgba(0, 0, 0, 1)">这个属性下面是用&lt;clip&gt;包裹的,如果没有包裹<strong><span style="color: rgba(0, 0, 0, 1)">&lt;clip&gt;就</span></strong>会出现进度条没有颜色的bug</span></strong></p>
<p><span style="color: rgba(255, 0, 0, 1)">android:id<span style="color: rgba(0, 0, 255, 1)">="@android:id/progress"</span></span>是主进度背景,就是你看视频的时候当前看到哪里的进度条背景. <strong><span style="color: rgba(255, 0, 0, 1)">请注意!</span><span style="color: rgba(0, 0, 0, 1)">这个属性下面是用&lt;clip&gt;包裹的,如果没有包裹<strong><span style="color: rgba(0, 0, 0, 1)">&lt;clip&gt;</span></strong><strong><span style="color: rgba(0, 0, 0, 1)"><strong><span style="color: rgba(0, 0, 0, 1)">就</span></strong>会出现进度条没有颜色的bug</span></strong></span></strong></p>
<p><strong><span style="color: rgba(0, 0, 0, 1)"><strong><span style="color: rgba(0, 0, 0, 1)">另外,进度条背景还支持设置伸缩方向,横竖方向与自定义矢量图,可以参考如下设置:</span></strong></span></strong></p>
<div class="cnblogs_code">
<pre>    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">item </span><span style="color: rgba(255, 0, 0, 1)">android:id</span><span style="color: rgba(0, 0, 255, 1)">="@android:id/progress"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">clip </span><span style="color: rgba(255, 0, 0, 1)">android:drawable</span><span style="color: rgba(0, 0, 255, 1)">="@drawable/ic_curtain"</span><span style="color: rgba(255, 0, 0, 1)">
            android:gravity</span><span style="color: rgba(0, 0, 255, 1)">="right"</span><span style="color: rgba(255, 0, 0, 1)">
            android:clipOrientation</span><span style="color: rgba(0, 0, 255, 1)">="horizontal"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">clip</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">item</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<h2>进度条的圆点背景</h2>
<p><span style="color: rgba(0, 0, 0, 1)">seekbar_thumb_bg.xml</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">selector </span><span style="color: rgba(255, 0, 0, 1)">xmlns:android</span><span style="color: rgba(0, 0, 255, 1)">="http://schemas.android.com/apk/res/android"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">获取焦点和没有按下的时候</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">item </span><span style="color: rgba(255, 0, 0, 1)">android:drawable</span><span style="color: rgba(0, 0, 255, 1)">="@drawable/seekbar_thumb_normal"</span><span style="color: rgba(255, 0, 0, 1)"> android:state_focused</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(255, 0, 0, 1)"> android:state_pressed</span><span style="color: rgba(0, 0, 255, 1)">="false"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">获取焦点但按下的时候</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">item </span><span style="color: rgba(255, 0, 0, 1)">android:drawable</span><span style="color: rgba(0, 0, 255, 1)">="@drawable/seekbar_thumb_pressed"</span><span style="color: rgba(255, 0, 0, 1)"> android:state_focused</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(255, 0, 0, 1)"> android:state_pressed</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">没有获取焦点按下的时候</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">item </span><span style="color: rgba(255, 0, 0, 1)">android:drawable</span><span style="color: rgba(0, 0, 255, 1)">="@drawable/seekbar_thumb_pressed"</span><span style="color: rgba(255, 0, 0, 1)"> android:state_focused</span><span style="color: rgba(0, 0, 255, 1)">="false"</span><span style="color: rgba(255, 0, 0, 1)"> android:state_pressed</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">默认的时候</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">item </span><span style="color: rgba(255, 0, 0, 1)">android:drawable</span><span style="color: rgba(0, 0, 255, 1)">="@drawable/seekbar_thumb_normal"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">selector</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>&nbsp;在代码上设置的监听</p>
<div class="cnblogs_code">
<pre>mVideoSeekbar.setOnSeekBarChangeListener(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SeekBar.OnSeekBarChangeListener() {
            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onProgressChanged(SeekBar seekBar, <span style="color: rgba(0, 0, 255, 1)">int</span> progress, <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> fromUser) {
         </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">进度变化回调
         </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">fromUser 表示改变这个进度的这个操作来自用户,这个布尔值可以区分是代码上改变的进度还是,用户操作后改变的进度</span><span style="color: rgba(0, 0, 0, 1)">
            }

            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onStartTrackingTouch(SeekBar seekBar) {
          </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">开始触摸移动进度条</span>
<span style="color: rgba(0, 0, 0, 1)">

            }

            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onStopTrackingTouch(SeekBar seekBar) {
          </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">停止触摸移动进度条</span><span style="color: rgba(0, 0, 0, 1)">
            }
      });</span></pre>
</div>
<h1><span style="color: rgba(0, 128, 128, 1)">进度条圆点偏移</span></h1>
<p>如果你需要进度条圆点能超出进度条就需要将thumbOffset属性设置超出距离,反之如果你希望圆点永远不会超出进度条只需要设置为0dp</p>
<div class="cnblogs_code">
<pre>android:thumbOffset="0dp"</pre>
</div>
<h1><span style="color: rgba(0, 128, 128, 1)">点子:自定义实现只允许拖拽不允许点击的seekbar进度条</span></h1>
<div class="cnblogs_code">
<pre> oldProgress =<span style="color: rgba(0, 0, 0, 1)"> mSeekBar.getProgress();
      mSeekBar.setOnSeekBarChangeListener(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SeekBar.OnSeekBarChangeListener() {
            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onProgressChanged(SeekBar seekBar, <span style="color: rgba(0, 0, 255, 1)">int</span> progress, <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> fromTouch) {
                </span><span style="color: rgba(0, 0, 255, 1)">if</span>(progress &gt; oldProgress + 3 || progress &lt; oldProgress - 3<span style="color: rgba(0, 0, 0, 1)">){
                  seekBar.setProgress(oldProgress);
                  </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
                }
                seekBar.setProgress(progress);
                oldProgress </span>=<span style="color: rgba(0, 0, 0, 1)"> progress;
            }
            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onStartTrackingTouch(SeekBar seekBar) {

            }
            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onStopTrackingTouch(SeekBar seekBar) {

            }
      });</span></pre>
</div>
<h1><span style="color: rgba(0, 128, 128, 1)">设置Thumb大小</span></h1>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">    fun setThumbSize(){
      val bitmap </span>=<span style="color: rgba(0, 0, 0, 1)"> BitmapFactory.decodeResource(resources, R.mipmap.main_ic_vehicle_1)
      val dp238 </span>=<span style="color: rgba(0, 0, 0, 1)"> TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 238f, resources.displayMetrics).toInt()
      val dp108 </span>=<span style="color: rgba(0, 0, 0, 1)"> TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 108f, resources.displayMetrics).toInt()
      val thumb: Bitmap </span>= Bitmap.createScaledBitmap(bitmap, dp238, dp108, <span style="color: rgba(0, 0, 255, 1)">true</span>) <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置大小</span>
      val d =<span style="color: rgba(0, 0, 0, 1)"> BitmapDrawable(thumb)
      val bitmap1 </span>=<span style="color: rgba(0, 0, 0, 1)"> d.bitmap
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (bitmap1.density ==<span style="color: rgba(0, 0, 0, 1)"> Bitmap.DENSITY_NONE) {
            d.setTargetDensity(resources.getDisplayMetrics())
      }
      val drawable: Drawable </span>=<span style="color: rgba(0, 0, 0, 1)"> d
      mBinding.seekBar.thumb </span>=<span style="color: rgba(0, 0, 0, 1)"> drawable
    }</span></pre>
</div>
<p>&nbsp;</p>
<h1><span style="color: rgba(0, 128, 128, 1)">点子:将SeekBar进度条竖立</span></h1>
<p><span style="color: rgba(0, 0, 0, 1)">在百度里有很多SeekBar竖立的例子,都是重新实现</span>onDraw方法将画布旋转,但是我觉得那些都是化简为繁,也没深刻理解View这个基础类的强大之处,我的方式就是完美利用View的旋转属性</p>
<p><span style="color: rgba(0, 0, 0, 1)">请注意,这里有两种做法:</span></p>
<p>第一种适合一些页面简单的地方,直接将SeekBar 旋转,优点是不需要布局嵌套了,但是需要小心翼翼调试好位置.</p>
<p>第二种更适合进度条的形式,那就是在SeekBar外面套一个ConstraintLayout,然后将SeekBar在旋转. 这种方式可以适合在一些复杂的页面中.</p>
<h2>第一种</h2>
<p>直接将SeekBar 旋转,优点是不需要布局嵌套了,但是需要小心翼翼调试好位置.适合一些简单的地方</p>
<p><span style="color: rgba(0, 0, 0, 1)">效果图:</span></p>
<p><span style="color: rgba(0, 128, 128, 1)"><img src="https://img2018.cnblogs.com/common/1497956/201911/1497956-20191128104434647-1739844378.png" alt=""></span></p>
<p>activity布局</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;?</span><span style="color: rgba(255, 0, 255, 1)">xml version="1.0" encoding="utf-8"</span><span style="color: rgba(0, 0, 255, 1)">?&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">androidx.constraintlayout.widget.ConstraintLayout
    </span><span style="color: rgba(255, 0, 0, 1)">xmlns:android</span><span style="color: rgba(0, 0, 255, 1)">="http://schemas.android.com/apk/res/android"</span><span style="color: rgba(255, 0, 0, 1)">
    xmlns:app</span><span style="color: rgba(0, 0, 255, 1)">="http://schemas.android.com/apk/res-auto"</span><span style="color: rgba(255, 0, 0, 1)">
    xmlns:tools</span><span style="color: rgba(0, 0, 255, 1)">="http://schemas.android.com/tools"</span><span style="color: rgba(255, 0, 0, 1)">
    android:layout_width</span><span style="color: rgba(0, 0, 255, 1)">="match_parent"</span><span style="color: rgba(255, 0, 0, 1)">
    android:layout_height</span><span style="color: rgba(0, 0, 255, 1)">="match_parent"</span><span style="color: rgba(255, 0, 0, 1)">
    android:background</span><span style="color: rgba(0, 0, 255, 1)">="@color/color_black_1"</span><span style="color: rgba(255, 0, 0, 1)">
    tools:context</span><span style="color: rgba(0, 0, 255, 1)">=".function.setting.system.view.SetScreenTimeoutActivity"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">TextView
      </span><span style="color: rgba(255, 0, 0, 1)">android:id</span><span style="color: rgba(0, 0, 255, 1)">="@+id/title"</span><span style="color: rgba(255, 0, 0, 1)">
      android:layout_width</span><span style="color: rgba(0, 0, 255, 1)">="match_parent"</span><span style="color: rgba(255, 0, 0, 1)">
      android:layout_height</span><span style="color: rgba(0, 0, 255, 1)">="76dp"</span><span style="color: rgba(255, 0, 0, 1)">
      android:text</span><span style="color: rgba(0, 0, 255, 1)">="@string/off_screen_time"</span><span style="color: rgba(255, 0, 0, 1)">
      android:textColor</span><span style="color: rgba(0, 0, 255, 1)">="@color/ColorWhite"</span><span style="color: rgba(255, 0, 0, 1)">
      android:textSize</span><span style="color: rgba(0, 0, 255, 1)">="22sp"</span><span style="color: rgba(255, 0, 0, 1)">
      android:gravity</span><span style="color: rgba(0, 0, 255, 1)">="center"</span><span style="color: rgba(255, 0, 0, 1)">
      android:background</span><span style="color: rgba(0, 0, 255, 1)">="@color/black"</span><span style="color: rgba(255, 0, 0, 1)">
      app:layout_constraintTop_toTopOf</span><span style="color: rgba(0, 0, 255, 1)">="parent"</span><span style="color: rgba(255, 0, 0, 1)">
      app:layout_constraintLeft_toLeftOf</span><span style="color: rgba(0, 0, 255, 1)">="parent"</span><span style="color: rgba(255, 0, 0, 1)">
      app:layout_constraintRight_toRightOf</span><span style="color: rgba(0, 0, 255, 1)">="parent"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">ImageView
      </span><span style="color: rgba(255, 0, 0, 1)">android:id</span><span style="color: rgba(0, 0, 255, 1)">="@+id/back"</span><span style="color: rgba(255, 0, 0, 1)">
      android:layout_width</span><span style="color: rgba(0, 0, 255, 1)">="wrap_content"</span><span style="color: rgba(255, 0, 0, 1)">
      android:layout_height</span><span style="color: rgba(0, 0, 255, 1)">="0dp"</span><span style="color: rgba(255, 0, 0, 1)">
      android:src</span><span style="color: rgba(0, 0, 255, 1)">="@mipmap/img_back_arrow"</span><span style="color: rgba(255, 0, 0, 1)">
      android:paddingLeft</span><span style="color: rgba(0, 0, 255, 1)">="20dp"</span><span style="color: rgba(255, 0, 0, 1)">
      app:layout_constraintTop_toTopOf</span><span style="color: rgba(0, 0, 255, 1)">="@id/title"</span><span style="color: rgba(255, 0, 0, 1)">
      app:layout_constraintBottom_toBottomOf</span><span style="color: rgba(0, 0, 255, 1)">="@id/title"</span><span style="color: rgba(255, 0, 0, 1)">
      app:layout_constraintLeft_toLeftOf</span><span style="color: rgba(0, 0, 255, 1)">="parent"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">ImageView
      </span><span style="color: rgba(255, 0, 0, 1)">android:id</span><span style="color: rgba(0, 0, 255, 1)">="@+id/icon"</span><span style="color: rgba(255, 0, 0, 1)">
      android:layout_width</span><span style="color: rgba(0, 0, 255, 1)">="wrap_content"</span><span style="color: rgba(255, 0, 0, 1)">
      android:layout_height</span><span style="color: rgba(0, 0, 255, 1)">="wrap_content"</span><span style="color: rgba(255, 0, 0, 1)">
      android:layout_marginTop</span><span style="color: rgba(0, 0, 255, 1)">="100dp"</span><span style="color: rgba(255, 0, 0, 1)">
      android:src</span><span style="color: rgba(0, 0, 255, 1)">="@drawable/ic_screen_timeout"</span><span style="color: rgba(255, 0, 0, 1)">
      app:layout_constraintTop_toBottomOf</span><span style="color: rgba(0, 0, 255, 1)">="@id/title"</span><span style="color: rgba(255, 0, 0, 1)">
      app:layout_constraintLeft_toLeftOf</span><span style="color: rgba(0, 0, 255, 1)">="parent"</span><span style="color: rgba(255, 0, 0, 1)">
      app:layout_constraintRight_toRightOf</span><span style="color: rgba(0, 0, 255, 1)">="parent"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">SeekBar
      </span><span style="color: rgba(255, 0, 0, 1)">android:id</span><span style="color: rgba(0, 0, 255, 1)">="@+id/seek_bar"</span><span style="color: rgba(255, 0, 0, 1)">
      android:layout_width</span><span style="color: rgba(0, 0, 255, 1)">="300dp"</span><span style="color: rgba(255, 0, 0, 1)">
      android:layout_height</span><span style="color: rgba(0, 0, 255, 1)">="70dp"</span><span style="color: rgba(255, 0, 0, 1)">
      android:rotationX</span><span style="color: rgba(0, 0, 255, 1)">="0.5"</span><span style="color: rgba(255, 0, 0, 1)">
      android:rotationY</span><span style="color: rgba(0, 0, 255, 1)">="0.5"</span><span style="color: rgba(255, 0, 0, 1)">
      android:rotation</span><span style="color: rgba(0, 0, 255, 1)">="270"</span><span style="color: rgba(255, 0, 0, 1)">
      android:thumb</span><span style="color: rgba(0, 0, 255, 1)">="@null"</span><span style="color: rgba(255, 0, 0, 1)">
      android:layout_marginTop</span><span style="color: rgba(0, 0, 255, 1)">="135dp"</span><span style="color: rgba(255, 0, 0, 1)">
      android:progressDrawable</span><span style="color: rgba(0, 0, 255, 1)">="@drawable/bg_seekbar"</span><span style="color: rgba(255, 0, 0, 1)">
      android:max</span><span style="color: rgba(0, 0, 255, 1)">="6"</span><span style="color: rgba(255, 0, 0, 1)">
      app:layout_constraintTop_toBottomOf</span><span style="color: rgba(0, 0, 255, 1)">="@id/icon"</span><span style="color: rgba(255, 0, 0, 1)">
      app:layout_constraintLeft_toLeftOf</span><span style="color: rgba(0, 0, 255, 1)">="parent"</span><span style="color: rgba(255, 0, 0, 1)">
      app:layout_constraintRight_toRightOf</span><span style="color: rgba(0, 0, 255, 1)">="parent"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">ImageView
      </span><span style="color: rgba(255, 0, 0, 1)">android:id</span><span style="color: rgba(0, 0, 255, 1)">="@+id/add_icon"</span><span style="color: rgba(255, 0, 0, 1)">
      android:layout_width</span><span style="color: rgba(0, 0, 255, 1)">="wrap_content"</span><span style="color: rgba(255, 0, 0, 1)">
      android:layout_height</span><span style="color: rgba(0, 0, 255, 1)">="wrap_content"</span><span style="color: rgba(255, 0, 0, 1)">
      android:src</span><span style="color: rgba(0, 0, 255, 1)">="@drawable/ic_add"</span><span style="color: rgba(255, 0, 0, 1)">
      android:layout_marginTop</span><span style="color: rgba(0, 0, 255, 1)">="55dp"</span><span style="color: rgba(255, 0, 0, 1)">
      app:layout_constraintLeft_toLeftOf</span><span style="color: rgba(0, 0, 255, 1)">="@id/seek_bar"</span><span style="color: rgba(255, 0, 0, 1)">
      app:layout_constraintRight_toRightOf</span><span style="color: rgba(0, 0, 255, 1)">="@id/seek_bar"</span><span style="color: rgba(255, 0, 0, 1)">
      app:layout_constraintTop_toBottomOf</span><span style="color: rgba(0, 0, 255, 1)">="@id/icon"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">ImageView
      </span><span style="color: rgba(255, 0, 0, 1)">android:id</span><span style="color: rgba(0, 0, 255, 1)">="@+id/reduce_icon"</span><span style="color: rgba(255, 0, 0, 1)">
      android:layout_width</span><span style="color: rgba(0, 0, 255, 1)">="wrap_content"</span><span style="color: rgba(255, 0, 0, 1)">
      android:layout_height</span><span style="color: rgba(0, 0, 255, 1)">="wrap_content"</span><span style="color: rgba(255, 0, 0, 1)">
      android:src</span><span style="color: rgba(0, 0, 255, 1)">="@drawable/ic_reduce"</span><span style="color: rgba(255, 0, 0, 1)">
      android:layout_marginTop</span><span style="color: rgba(0, 0, 255, 1)">="195dp"</span><span style="color: rgba(255, 0, 0, 1)">
      app:layout_constraintTop_toBottomOf</span><span style="color: rgba(0, 0, 255, 1)">="@id/add_icon"</span><span style="color: rgba(255, 0, 0, 1)">
      app:layout_constraintLeft_toLeftOf</span><span style="color: rgba(0, 0, 255, 1)">="parent"</span><span style="color: rgba(255, 0, 0, 1)">
      app:layout_constraintRight_toRightOf</span><span style="color: rgba(0, 0, 255, 1)">="parent"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">TextView
      </span><span style="color: rgba(255, 0, 0, 1)">android:id</span><span style="color: rgba(0, 0, 255, 1)">="@+id/current_select_time"</span><span style="color: rgba(255, 0, 0, 1)">
      android:layout_width</span><span style="color: rgba(0, 0, 255, 1)">="wrap_content"</span><span style="color: rgba(255, 0, 0, 1)">
      android:layout_height</span><span style="color: rgba(0, 0, 255, 1)">="wrap_content"</span><span style="color: rgba(255, 0, 0, 1)">
      android:layout_marginTop</span><span style="color: rgba(0, 0, 255, 1)">="340dp"</span><span style="color: rgba(255, 0, 0, 1)">
      android:textSize</span><span style="color: rgba(0, 0, 255, 1)">="22sp"</span><span style="color: rgba(255, 0, 0, 1)">
      android:textColor</span><span style="color: rgba(0, 0, 255, 1)">="@color/ColorWhite"</span><span style="color: rgba(255, 0, 0, 1)">
      app:layout_constraintTop_toBottomOf</span><span style="color: rgba(0, 0, 255, 1)">="@id/icon"</span><span style="color: rgba(255, 0, 0, 1)">
      app:layout_constraintLeft_toLeftOf</span><span style="color: rgba(0, 0, 255, 1)">="parent"</span><span style="color: rgba(255, 0, 0, 1)">
      app:layout_constraintRight_toRightOf</span><span style="color: rgba(0, 0, 255, 1)">="parent"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">androidx.constraintlayout.widget.ConstraintLayout</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>这样里有一个需要注意的点,就是高度和宽度其实是互换的关系了.</p>
<h3>进度背景</h3>
<p><span style="color: rgba(0, 0, 0, 1)">bg_seekbar.xml</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;?</span><span style="color: rgba(255, 0, 255, 1)">xml version="1.0" encoding="utf-8"</span><span style="color: rgba(0, 0, 255, 1)">?&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">layer-list </span><span style="color: rgba(255, 0, 0, 1)">xmlns:android</span><span style="color: rgba(0, 0, 255, 1)">="http://schemas.android.com/apk/res/android"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">item </span><span style="color: rgba(255, 0, 0, 1)">android:id</span><span style="color: rgba(0, 0, 255, 1)">="@android:id/background"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">shape </span><span style="color: rgba(255, 0, 0, 1)">android:shape</span><span style="color: rgba(0, 0, 255, 1)">="rectangle"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">size </span><span style="color: rgba(255, 0, 0, 1)">android:width</span><span style="color: rgba(0, 0, 255, 1)">="300dp"</span><span style="color: rgba(255, 0, 0, 1)"> android:height</span><span style="color: rgba(0, 0, 255, 1)">="70dp"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">solid </span><span style="color: rgba(255, 0, 0, 1)">android:color</span><span style="color: rgba(0, 0, 255, 1)">="#575757"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">corners </span><span style="color: rgba(255, 0, 0, 1)">android:radius</span><span style="color: rgba(0, 0, 255, 1)">="88dp"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">shape</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">item</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">item </span><span style="color: rgba(255, 0, 0, 1)">android:id</span><span style="color: rgba(0, 0, 255, 1)">="@android:id/secondaryProgress"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">clip</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">shape</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">solid </span><span style="color: rgba(255, 0, 0, 1)">android:color</span><span style="color: rgba(0, 0, 255, 1)">="#575757"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">shape</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">clip</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">item</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">item </span><span style="color: rgba(255, 0, 0, 1)">android:id</span><span style="color: rgba(0, 0, 255, 1)">="@android:id/progress"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">clip</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">shape </span><span style="color: rgba(255, 0, 0, 1)">android:shape</span><span style="color: rgba(0, 0, 255, 1)">="rectangle"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">size </span><span style="color: rgba(255, 0, 0, 1)">android:width</span><span style="color: rgba(0, 0, 255, 1)">="300dp"</span><span style="color: rgba(255, 0, 0, 1)"> android:height</span><span style="color: rgba(0, 0, 255, 1)">="70dp"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
                <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">solid </span><span style="color: rgba(255, 0, 0, 1)">android:color</span><span style="color: rgba(0, 0, 255, 1)">="#FFFFFF"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
                <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">corners </span><span style="color: rgba(255, 0, 0, 1)">android:radius</span><span style="color: rgba(0, 0, 255, 1)">="88dp"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">shape</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">clip</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">item</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">layer-list</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<h2>第二种</h2>
<p>思想上与第一种一样,<strong>关键区别是外部嵌套一个ConstraintLayout</strong>用于控制布局旋转后的位置大小.</p>
<p>这种方式更适合进度条的形式,SeekBar外面套ConstraintLayout,然后将SeekBar在旋转. 这种方式可以适合在一些许多View需要配合的页面中. ,关键参考如下:</p>
<p><strong>这里的关键代码就是:app:layout_constraintDimensionRatio="1:1"&nbsp; &nbsp;其中构思,请自行品味</strong></p>
<p><strong><span style="color: rgba(255, 0, 0, 1)">另外请注意</span>,这里的thumb也会被旋转你需要准备一个相反旋转的thumb图片</strong></p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">androidx.constraintlayout.widget.ConstraintLayout
      </span><span style="color: rgba(255, 0, 0, 1)">android:id</span><span style="color: rgba(0, 0, 255, 1)">="@+id/unlockLayout"</span><span style="color: rgba(255, 0, 0, 1)">
      android:layout_width</span><span style="color: rgba(0, 0, 255, 1)">="69dp"</span><span style="color: rgba(255, 0, 0, 1)">
      android:layout_height</span><span style="color: rgba(0, 0, 255, 1)">="0dp"</span><span style="color: rgba(255, 0, 0, 1)">
      app:layout_constraintBottom_toBottomOf</span><span style="color: rgba(0, 0, 255, 1)">="parent"</span><span style="color: rgba(255, 0, 0, 1)">
      app:layout_constraintEnd_toEndOf</span><span style="color: rgba(0, 0, 255, 1)">="parent"</span><span style="color: rgba(255, 0, 0, 1)">
      app:layout_constraintTop_toBottomOf</span><span style="color: rgba(0, 0, 255, 1)">="@id/layout"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">SeekBar
            </span><span style="color: rgba(255, 0, 0, 1)">android:id</span><span style="color: rgba(0, 0, 255, 1)">="@+id/seekBar"</span><span style="color: rgba(255, 0, 0, 1)">
            android:layout_width</span><span style="color: rgba(0, 0, 255, 1)">="0dp"</span><span style="color: rgba(255, 0, 0, 1)">
            android:layout_height</span><span style="color: rgba(0, 0, 255, 1)">="match_parent"</span><span style="color: rgba(255, 0, 0, 1)">
            android:rotationX</span><span style="color: rgba(0, 0, 255, 1)">="0.5"</span><span style="color: rgba(255, 0, 0, 1)">
            android:rotationY</span><span style="color: rgba(0, 0, 255, 1)">="0.5"</span><span style="color: rgba(255, 0, 0, 1)">
            android:rotation</span><span style="color: rgba(0, 0, 255, 1)">="90"</span><span style="color: rgba(255, 0, 0, 1)">
            android:paddingStart</span><span style="color: rgba(0, 0, 255, 1)">="50dp"</span><span style="color: rgba(255, 0, 0, 1)">
            android:paddingEnd</span><span style="color: rgba(0, 0, 255, 1)">="50dp"</span><span style="color: rgba(255, 0, 0, 1)">
            android:max</span><span style="color: rgba(0, 0, 255, 1)">="100"</span><span style="color: rgba(255, 0, 0, 1)">
            android:thumb</span><span style="color: rgba(0, 0, 255, 1)">="@drawable/snake_ic_lock_1"</span><span style="color: rgba(255, 0, 0, 1)">
            android:progressDrawable</span><span style="color: rgba(0, 0, 255, 1)">="@drawable/snake_seekbar_open_lock"</span><span style="color: rgba(255, 0, 0, 1)">
            app:layout_constraintDimensionRatio</span><span style="color: rgba(0, 0, 255, 1)">="1:1"</span><span style="color: rgba(255, 0, 0, 1)">
            app:layout_constraintTop_toTopOf</span><span style="color: rgba(0, 0, 255, 1)">="parent"</span><span style="color: rgba(255, 0, 0, 1)">
            app:layout_constraintBottom_toBottomOf</span><span style="color: rgba(0, 0, 255, 1)">="parent"</span><span style="color: rgba(255, 0, 0, 1)">
            app:layout_constraintRight_toRightOf</span><span style="color: rgba(0, 0, 255, 1)">="parent"</span><span style="color: rgba(255, 0, 0, 1)">
            app:layout_constraintLeft_toLeftOf</span><span style="color: rgba(0, 0, 255, 1)">="parent"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">androidx.constraintlayout.widget.ConstraintLayout</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<h1><span style="color: rgba(0, 128, 128, 1)">点子:实现一个多色阶渐变进度条(颜色取值进度条)</span></h1>
<p>颜色取值的部分不说明,这个可以根据progress的位置来自定义取颜色值,这里重点说明的是怎么实现多色阶的渐变的背景。</p>
<p>效果图:</p>
<p><img src="https://img2020.cnblogs.com/blog/1497956/202005/1497956-20200519154410867-578640977.png" alt=""></p>
<p>代码:</p>
<p><span style="color: rgba(0, 0, 0, 1)">说明下为什么用代码实现,因为xml的shape属性根本不支持这么多颜色的渐变,在xml上的shape的gradient属性只支持三色阶渐变。当然,如果你直接放矢量图作为渐变色背景,那就不需要参考下面代码了。但是渐变色背景不能是位图,只能是矢量图。</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">如果你不太了解<span style="color: rgba(0, 0, 0, 1)">GradientDrawable</span>,可以参考我这篇博客:https://www.cnblogs.com/guanxinjing/p/11142599.html</span></p>
<div class="cnblogs_code">
<pre>      <span style="color: rgba(0, 0, 255, 1)">int</span>[] colors = {Color.parseColor("#FF0000"<span style="color: rgba(0, 0, 0, 1)">)
                , Color.parseColor(</span>"#FF00FF"<span style="color: rgba(0, 0, 0, 1)">)
                , Color.parseColor(</span>"#0000FF"<span style="color: rgba(0, 0, 0, 1)">)
                , Color.parseColor(</span>"#00FFFF"<span style="color: rgba(0, 0, 0, 1)">)
                , Color.parseColor(</span>"#00FF00"<span style="color: rgba(0, 0, 0, 1)">)
                , Color.parseColor(</span>"#FFFF00"<span style="color: rgba(0, 0, 0, 1)">)
                , Color.parseColor(</span>"#FF0000"<span style="color: rgba(0, 0, 0, 1)">)};
      GradientDrawable gradientDrawable </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> GradientDrawable();
      gradientDrawable.setShape(GradientDrawable.RECTANGLE);
      gradientDrawable.setColors(colors); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">添加颜色组</span>
      gradientDrawable.setCornerRadius(UnitConversionUtil.dip2px(getContext(), 10<span style="color: rgba(0, 0, 0, 1)">));
      gradientDrawable.setOrientation(GradientDrawable.Orientation.LEFT_RIGHT);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置渐变方向</span>
      gradientDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置线性渐变</span>
      gradientDrawable.setDither(<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);

      Drawable[] drawables </span>=<span style="color: rgba(0, 0, 0, 1)"> {gradientDrawable};
      LayerDrawable layerDrawable </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> LayerDrawable(drawables);
      layerDrawable.setId(</span>0<span style="color: rgba(0, 0, 0, 1)">, android.R.id.background);
      mColorSeekBar.setProgressDrawable(layerDrawable);</span></pre>
</div>
<h1><span style="color: rgba(0, 128, 128, 1)">点子:Thumb带文字</span></h1>
<p>&nbsp;效果图:</p>
<p><img src="https://img2022.cnblogs.com/blog/1497956/202203/1497956-20220326151825836-1711471892.png" alt=""></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.annotation.SuppressLint;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.content.Context;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.graphics.Canvas;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.graphics.Paint;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.util.AttributeSet;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.util.TypedValue;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.view.MotionEvent;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.SeekBar;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> androidx.annotation.ColorRes;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.lwlx.ui.main.R;

@SuppressLint(</span>"AppCompatCustomView"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> TextSeekBar <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> SeekBar {
    </span><span style="color: rgba(0, 0, 255, 1)">private</span> Paint mPaint = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paint();
    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">float</span><span style="color: rgba(0, 0, 0, 1)"> x;
    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">float</span> mTextSize =<span style="color: rgba(0, 0, 0, 1)"> 18f;
    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span> mColorId =<span style="color: rgba(0, 0, 0, 1)"> R.color.main_color_9F621B;
    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">float</span> mWidth =<span style="color: rgba(0, 0, 0, 1)"> 0f;
    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">float</span> mHeight =<span style="color: rgba(0, 0, 0, 1)"> 0f;

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> TextSeekBar(Context context) {
      </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">(context);
      init();
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> TextSeekBar(Context context, AttributeSet attrs) {
      </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">(context, attrs);
      init();
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> TextSeekBar(Context context, AttributeSet attrs, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> defStyleAttr) {
      </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">(context, attrs, defStyleAttr);
      init();
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> TextSeekBar(Context context, AttributeSet attrs, <span style="color: rgba(0, 0, 255, 1)">int</span> defStyleAttr, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> defStyleRes) {
      </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">(context, attrs, defStyleAttr, defStyleRes);
      init();
    }

    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> init() {
      mPaint.setColor(getResources().getColor(mColorId));
      mPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mTextSize, getResources().getDisplayMetrics()));
      mPaint.setTextAlign(Paint.Align.CENTER);
      mPaint.setStrokeWidth(</span>15<span style="color: rgba(0, 0, 0, 1)">);
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setTextSize(<span style="color: rgba(0, 0, 255, 1)">float</span><span style="color: rgba(0, 0, 0, 1)"> textSize) {
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.mTextSize =<span style="color: rgba(0, 0, 0, 1)"> textSize;
      invalidate();
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setTextColor(@ColorRes <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> id) {
      mColorId </span>=<span style="color: rgba(0, 0, 0, 1)"> id;
      invalidate();
    }

    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">synchronized</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onMeasure(<span style="color: rgba(0, 0, 255, 1)">int</span> widthMeasureSpec, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> heightMeasureSpec) {
      </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onMeasure(widthMeasureSpec, heightMeasureSpec);
      mWidth </span>=<span style="color: rgba(0, 0, 0, 1)"> MeasureSpec.getSize(widthMeasureSpec);
      mHeight </span>=<span style="color: rgba(0, 0, 0, 1)"> MeasureSpec.getSize(heightMeasureSpec);
      x </span>= getProgress() * mWidth / 100 +<span style="color: rgba(0, 0, 0, 1)"> getX();
    }

    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> onTouchEvent(MotionEvent event) {
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (event.getAction() ==<span style="color: rgba(0, 0, 0, 1)"> MotionEvent.ACTION_DOWN) {
            x </span>=<span style="color: rgba(0, 0, 0, 1)"> event.getX();
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (event.getAction() ==<span style="color: rgba(0, 0, 0, 1)"> MotionEvent.ACTION_MOVE) {
            x </span>=<span style="color: rgba(0, 0, 0, 1)"> event.getX();
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (event.getAction() ==<span style="color: rgba(0, 0, 0, 1)"> MotionEvent.ACTION_UP) {
            x </span>=<span style="color: rgba(0, 0, 0, 1)"> event.getX();
      }
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (x &lt; 0<span style="color: rgba(0, 0, 0, 1)">) {
            x </span>= 0 +<span style="color: rgba(0, 0, 0, 1)"> getPaddingLeft();
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (x &gt;<span style="color: rgba(0, 0, 0, 1)"> mWidth) {
            x </span>= mWidth -<span style="color: rgba(0, 0, 0, 1)"> getPaddingRight();
      }

      </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onTouchEvent(event);
    }

    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">synchronized</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onDraw(Canvas canvas) {
      </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onDraw(canvas);
      </span><span style="color: rgba(0, 0, 255, 1)">float</span> y = (mHeight - (getPaddingTop() + getPaddingBottom())) / 2<span style="color: rgba(0, 0, 0, 1)">;
      canvas.drawText(String.valueOf(getProgress()), x, y </span>+ 5<span style="color: rgba(0, 0, 0, 1)">, mPaint);
    }
}</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>End</p>
</div>

</div>
<div id="MySignature" role="contentinfo">
    <div style="text-align: center">
    <p style="color:orange;font-size:16px;" >本文来自博客园,作者:观心静 ,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/11947457.html </p>
    <div style="color:orange;font-size:16px;">本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。 </div>
</div><br><br>
来源:https://www.cnblogs.com/guanxinjing/p/11947457.html
頁: [1]
查看完整版本: Android开发 SeekBar基本使用与开发点子记录