一手遮天 Android - view(进度类): SeekBar 样式
一手遮天 Android - view(进度类): SeekBar 样式
示例如下:
/view/progress/SeekBarDemo2.java
/**
* SeekBar - 拖动条
* setProgressDrawable() - 设置进度条的样式
* setThumb() - 设置 thumb 的样式
*/
package com.webabcd.androiddemo.view.progress;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.SeekBar;
import com.webabcd.androiddemo.R;
import com.webabcd.androiddemo.utils.Helper;
import java.lang.reflect.Field;
public class SeekBarDemo2 extends AppCompatActivity {
private SeekBar _seekBar3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_progress_seekbardemo2);
_seekBar3 = (SeekBar) findViewById(R.id.seekBar3);
sample();
}
private void sample() {
_seekBar3.setProgressDrawable(Helper.id2drawable(this, R.drawable.layerlist_seekbar_progress));
_seekBar3.setThumb(Helper.id2drawable(this, R.drawable.selector_seekbar_thumb));
// 自定义 SeekBar 的样式时,默认 progressBar 会变成与 thumb 一样高,可以通过设置 SeekBar 的 minHeight, maxHeight 来避免这种情况
// 在 java 中无法直接修改 SeekBar 的 minHeight, maxHeight
// 所以要通过如下的反射的方法来修改 SeekBar 的 minHeight, maxHeight
try {
// 注:mMinHeight 和 mMaxHeight 在 SeekBar 的基类 ProgressBar 中
// 注:要看看当前 SeekBar 到底是 AppCompatSeekBar 还是 SeekBar 以便决定调用几次 getSuperclass() 从而找到 ProgressBar
// _seekBar3.getClass() 是 androidx.appcompat.widget.AppCompatSeekBar
// _seekBar3.getClass().getSuperclass() 是 android.widget.SeekBar
// _seekBar3.getClass().getSuperclass().getSuperclass() 是 android.widget.AbsSeekBar
// _seekBar3.getClass().getSuperclass().getSuperclass().getSuperclass() 是 android.widget.ProgressBar
Class<?> superclass = _seekBar3.getClass().getSuperclass().getSuperclass().getSuperclass();
Field mMaxHeight = superclass.getDeclaredField("mMaxHeight");
mMaxHeight.setAccessible(true);
mMaxHeight.set(_seekBar3, Helper.dp2px(this, 10));
Field mMinHeight = superclass.getDeclaredField("mMinHeight");
mMinHeight.setAccessible(true);
mMinHeight.set(_seekBar3, Helper.dp2px(this, 10));
} catch (Exception e) {
Log.e("SeekBarDemo2", e.toString());
}
}
}
/layout/activity_view_progress_seekbardemo2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--
SeekBar - 拖动条
注:自定义 SeekBar 的样式时,默认 progressBar 会变成与 thumb 一样高,可以通过设置 SeekBar 的 minHeight, maxHeight 来避免这种情况
-->
<!--
通过 style 自定义 SeekBar 的样式(参见 values/styles.xml 中的“MySeekBarStyle”)
-->
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:min="0"
android:max="100"
android:progress="10"
android:secondaryProgress="70"
style="@style/MySeekBarStyle" />
<!--
通过 progressDrawable, thumb 自定义 SeekBar 的样式(参见 drawable/layerlist_seekbar_progress, drawable/selector_seekbar_thumb)
-->
<SeekBar
android:id="@+id/seekBar2"
android:layout_marginTop="10dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:min="0"
android:max="100"
android:progress="10"
android:secondaryProgress="70"
android:minHeight="10dp"
android:maxHeight="10dp"
android:progressDrawable="@drawable/layerlist_seekbar_progress"
android:thumb="@drawable/selector_seekbar_thumb"/>
<!--
在 java 中设置 SeekBar 的样式
-->
<SeekBar
android:id="@+id/seekBar3"
android:layout_marginTop="10dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:min="0"
android:max="100"
android:progress="10"
android:secondaryProgress="70"/>
</LinearLayout>
/drawable/layerlist_seekbar_progress.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--
拖动条背景的样式
-->
<item android:id="@android:id/background">
<shape>
<solid android:color="@color/red" />
<corners android:radius="10dp" />
</shape>
</item>
<!--
拖动条第二进度的样式
-->
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<solid android:color="@color/green" />
<corners android:radius="10dp" />
</shape>
</clip>
</item>
<!--
拖动条进度的样式
-->
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="@color/blue" />
<corners android:radius="10dp" />
</shape>
</clip>
</item>
</layer-list>
/drawable/selector_seekbar_thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--
state_pressed="true" - 按下
state_pressed="false" - 抬起
-->
<item
android:state_pressed="true"
android:drawable="@drawable/shape_seekbar_thumb_pressed" />
<item
android:state_pressed="false"
android:drawable="@drawable/shape_seekbar_thumb_unpressed" />
</selector>