Android控件与布局——基础控件SeekBar

        最近在用原生的控件和布局绘制一些界面并使用,虽然这些都是Android基本知识,但是有的时候真的感觉力不从心,感觉有必要对Android常用的控件和布局做一个系统的了解。后续一个月甚至更多的时间都会围绕这个主题展开,毕竟这里面还是有不少高级控件的,我也会尽量结合应用深入的进行了解。

上一篇:ProgressBar    下一篇:RatingBar

今天,我们的主题是SeekBar,下面看一下官方文档的部分介绍:它是带有可拖拽图标的ProgressBar,用户可以通过这个图标的滑动来说设置当前的进度;不建议在它的两侧放置可以获取焦点的控件。

* A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch
* the thumb and drag left or right to set the current progress level or use the arrow keys.
* Placing focusable widgets to the left or right of a SeekBar is discouraged.
* <p>
* Clients of the SeekBar can attach a {@link SeekBar.OnSeekBarChangeListener} to
* be notified of the user's actions.

下面就看一下它最原始的样子:

 <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

这里为了展示效果,我特意把宽度设置成和父布局同宽,在上面的官方API介绍中,我们还忘了翻译一句,就是客户端可以通过对其添加监听事件来获取进度,下面我们就来尝试一下:

 progressSetting.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
               if(progress==0){
                   progressResult.setText("");
               }
               else {
                   progressResult.setText(progress+"%");
               }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

由上图可知,我们通过对其添加监听是可以通过回调实时获取进度数据的,我们可以结合这个功能做一些参数设置的场景需求

在上面的介绍中我们知道,它是继承至ProgressBar的,所以ProgressBar的属性和功能它都是具备的,关于ProgressBar的详细介绍可以参照上一篇博客。说到这里,其实SeekBar我们常用的主要就是通过监听实时获取用户可控的进度值是多少。下面我们就结合上一篇中说到的展示样式简单再延伸一下,在上一篇中,我们用到了:

<?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 android:shape="rectangle">
            <corners android:radius="12dp" />
            <solid android:color="#1E90DD" />
            <stroke android:width="5dp" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="12dp" />
                <solid android:color="#00CCEA" />
                <stroke android:width="5dp" />
            </shape>
        </clip>
    </item>
</layer-list>

这里我们同样通过属性android:progressDrawable对其进行设置

 android:progressDrawable="@drawable/progress_bar_back"

样式看起来稍微好一点点,当然你还可以设计出更好的效果出来。我们还可以通过其android:thumb属性设置移动图标的样式:

 android:thumb="@drawable/seek_bar_thumb"

说实话,我这里的图标以及背景都没有精心的绘制,所以较丑,在实际的使用过程中我们可以结合需求设计出各种好看的背景以及图标来使用。下面给出逻辑和布局代码(很简单),这个控件就算结束了。

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="aoto.com.commonwidgetandlayout.basic_widget.seekBar.SeekBarActivity">

    <TextView
        android:layout_marginTop="20dp"
        android:text="请调节温度控制阀调节温度:"
        android:textColor="#000000"
        android:textSize="25sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <SeekBar
        android:progressDrawable="@drawable/progress_bar_back"
        android:thumb="@drawable/seek_bar_thumb"
        android:id="@+id/progress_setting"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" />

    <TextView
        android:layout_marginTop="10dp"
        android:textColor="@color/colorPrimary"
        android:id="@+id/progress"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" />

</LinearLayout>

逻辑代码:

package aoto.com.commonwidgetandlayout.basic_widget.seekBar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;

import aoto.com.commonwidgetandlayout.R;

/**
 * @author why
 * @date 2019-5-24 18:21:07
 */
public class SeekBarActivity extends AppCompatActivity {

    TextView progressResult;
    SeekBar progressSetting;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seek_bar);
        progressSetting=findViewById(R.id.progress_setting);
        progressResult=findViewById(R.id.progress);

        progressSetting.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
               if(progress==0){
                   progressResult.setText("");
               }
               else {
                   progressResult.setText("已调至"+progress+"℃");
               }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
}

在SeekBar的style属性中,我们可以设置为如下:

 <SeekBar
        android:layout_marginTop="15dp"
        android:id="@+id/seek_bar_discrete"
        style="@style/Widget.AppCompat.SeekBar.Discrete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="10"
        android:progress="3" />

这是一种离散效果的SeekBar,演示效果如下:

这个之所以单独拿出来说一下是因为,在我们的布局Design界面中有这样一个单独的选项SeekBar(Discrete),同样的,我们也可以对其进行进度数据回调监听,这里就不在演示了。

关于这个控件,还有一个用得较多的开源项目DiscreteSeekBar,效果图如下:

screenshot

screenshot

关于使用方式就不多介绍了,git上介绍的已经很详细了。

注:欢迎扫码关注

 

 

DiscreteSeekBar是一个Material Design风格的、带有气泡指示器的拖动条。可以自定义样式,非常不错的东西。项目地址:https://github.com/AnderWeb/discreteSeekBar效果图:使用起来也非常简单如何使用xml中创建<org.adw.library.widgets.discreteseekbar.DiscreteSeekBar         android:id="@ id/discrete"         android:layout_width="match_parent"         android:layout_height="wrap_content"         app:dsb_min="1"         app:dsb_max="100"         app:dsb_value="20"         app:dsb_indicatorFormatter="d"         />2. javaDiscreteSeekBar discreteSeekBar = (DiscreteSeekBar) findViewById(R.id.discrete); //设置浮动气泡的值,每次进度值改变时,都会触发 discreteSeekBar.setNumericTransformer(new DiscreteSeekBar.NumericTransformer() {             @Override             public int transform(int value) {                 return value * 10;             }         }); //进度值变化监听 discreteSeekBar.setOnProgressChangeListener(new DiscreteSeekBar.OnProgressChangeListener(){             public void onProgressChanged(DiscreteSeekBar seekBar, int value, boolean fromUser){             }             public void onStartTrackingTouch(DiscreteSeekBar seekBar){             }             public void onStopTrackingTouch(DiscreteSeekBar seekBar){             }         });属性说明属性名类型使用说明dsb_mininteger|dimension最小进度值dsb_maxinteger|dimension最大进度值dsb_valueinteger|dimension当前进度值dsb_mirrorForRtlboolean由于某些语言,比如阿拉伯语、希伯来语,是“从右往左”显示的,和中文、英语……是相反的。所以该属性的含义是,是否根据本地语言进行反向。dsb_allowTrackClickToDragboolean是否允许通过点击进度条上某一点,来改变进度值。默认是truedsb_progressColorcolor|reference设置进度条及滑块(thumb)的颜色dsb_trackColorcolor|reference设置背景条(track)的颜色dsb_indicatorTextAppearancereference设置气泡(bubble)的样式dsb_indicatorColorcolor|reference设置气泡(bubble)的背景色dsb_indicatorElevationdimension很抱歉,这个属性我也不知道怎么用。related to android:elevation. Will only be used on API level 21 dsb_indicatorFormatterstring|reference设置气泡指示器文字的格式dsb_rippleColorcolor|reference设置触碰点波动的颜色dsb_indicatorPopupEnabledboolean是否显示气泡指示器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值