android中SeekBar自定义

效果图如下:

步骤:

1、重新绘制SeekBar的背景等

seekbar_horizontal.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" android:drawable="@color/colorWhite" />
    <!--第二层进度颜色(缓存之类的)-->
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="2.0dip" />
                <gradient android:startColor="#80ffd300" android:endColor="#a0ffcb00" android:angle="270.0" android:centerY="0.75" android:centerColor="#80ffb600" />
            </shape>
        </clip>
    </item>
    <!--进度的颜色或者图片-->
    <item android:id="@android:id/progress">
        <clip android:drawable="@color/colorAccent" />
    </item>
</layer-list>

2、自定义SeekBar的style。

<style name="Widget.SeekBar.Normal" parent="@android:style/Widget.SeekBar">
    <item name="android:maxHeight">3dp</item>
    <item name="android:indeterminateOnly">false</item>
    <item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
    <item name="android:progressDrawable">@drawable/seekbar_horizontal</item>
    <item name="android:minHeight">3dp</item>
    <item name="android:thumb">@mipmap/icon_seekbar_btn</item>
    <item name="android:thumbOffset">2dp</item>
</style>

3、在布局文件引用

<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/colorDeepGray"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <SeekBar
        android:id="@+id/Main_sBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:padding="10dp"
        style="@style/Widget.SeekBar.Normal"/>

</LinearLayout>

备注:ProgressBar进度条自定义和SeekBar类型。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android,可以通过自定义SeekBar实现数字滑动功能。首先,在布局文件定义自定义SeekBar的样式,可以使用ProgressBar来实现。如下所示: ``` <ProgressBar android:id="@+id/customSeekBar" style="@style/Widget.AppCompat.ProgressBar.Horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="50" android:progressDrawable="@drawable/custom_seekbar_progress" android:thumb="@drawable/custom_seekbar_thumb" /> ``` 在drawable文件夹下创建custom_seekbar_progress.xml和custom_seekbar_thumb.xml来定义SeekBar的背景和滑块样式。在custom_seekbar_progress.xml,可以使用shape和gradient标签来定义进度条的背景样式。在custom_seekbar_thumb.xml,可以使用shape标签来定义滑块的样式。 接下来,在Activity或Fragment找到SeekBar的实例,并设置OnSeekBarChangeListener监听器。在监听器,通过getProgress方法获取SeekBar的进度值,并根据需要进行相应的处理。例如,可以在TextView显示SeekBar的进度值,如下所示: ``` SeekBar customSeekBar = findViewById(R.id.customSeekBar); final TextView progressTextView = findViewById(R.id.progressTextView); customSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { progressTextView.setText(String.valueOf(progress)); } @Override public void onStartTrackingTouch(SeekBar seekBar) { // 当开始滑动SeekBar时执行的操作 } @Override public void onStopTrackingTouch(SeekBar seekBar) { // 当结束滑动SeekBar时执行的操作 } }); ``` 通过设置OnSeekBarChangeListener监听器,可以在SeekBar滑动时实时更新进度值,并进行相应的处理操作。根据自己的需求,可以在onProgressChanged、onStartTrackingTouch和onStopTrackingTouch方法添加自定义的逻辑。 以上就是使用自定义SeekBar实现数字滑动的简单方法。可以根据自己的需求进行进一步的定制和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值