Android自定義滑桿控件SeekBar多功能版本

nzmo1770 7年前發布 | 13K 次閱讀 安卓開發 Android開發 移動開發

在應用開發中有沒有遇到過通過滑桿控件選擇一些區間條件實現參數變化?今天我們就來自定義View實現一個多功能又實用的版本SeekBar。

Paste_Image.png](http://upload-images.jianshu.io/upload_images/3982371-6bc53a9b5d0878fc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

1.在布局中直接使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:padding="20dp"
    tools:context="com.tangyx.seekbar.MainActivity"
    android:orientation="vertical">
    <RelativeLayout
        android:layout_marginTop="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <com.seekbar.sliding.text.TextSeekBar
            android:id="@+id/only_back"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            app:tickCount="2"/>
    ...
    </RelativeLayout>
    <LinearLayout
        android:id="@+id/time_layout"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="5dp"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <com.seekbar.sliding.SlidingSeekBar
            android:id="@+id/time_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tickCount="5"
            app:tickHeight="3dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            app:connectingLineColor="@android:color/black"
            app:barColor="@color/color_e0e8ee"
            app:barWeight="1.5dp"/>
    </LinearLayout>
</LinearLayout>

2.自定義的SeekBar一共有兩種

第一種在滑球上面顯示當前選擇模式,就是設置文字顯示在滑球上(com.seekbar.sliding.text.TextSeekBar)。

第二種兩邊都可以滑動操作,比如選擇一個時間區域(com.seekbar.sliding.SlidingSeekBar)。

支持的屬性:

<declare-styleable name="SeekBar">
        <!--可滑動選擇區間段-->
        <attr name="tickCount" format="integer" />
        <!--每個間隔的標示的高度-->
        <attr name="tickHeight" format="dimension" />
        <attr name="barWeight" format="dimension" />
        <!--被滑動線條顏色-->
        <attr name="barColor" format="reference|color" />
        <attr name="connectingLineWeight" format="dimension" />
        <!--線條的顏色-->
        <attr name="connectingLineColor" format="reference|color" />
        <!--自定義滑球的圓形大小-->
        <attr name="thumbRadius" format="dimension" />
        <!--自定義滑球的圖片 沒有觸摸的樣式-->
        <attr name="thumbImageNormal" format="reference" />
        <!--自定義滑球的圖片 手觸摸按下的樣式-->
        <attr name="thumbImagePressed" format="reference" />
        <!--如果不設置圖片,可以配合thumbRadius用程序實現的滑球-->
        <attr name="thumbColorNormal" format="reference|color"/>
        <attr name="thumbColorPressed" format="reference|color"/>
</declare-styleable>

3.監聽滑桿事件,滑桿滑動變化通過實現OnRangeBarChangeListener接口來實現對應的邏輯。

Paste_Image.png

seekbar.gif

當然類似這方面的自定義View都是通過監聽手勢實現對應的邏輯,你可以繼承BaseSeekBar來定義自己的樣式。

 

 

 本文由用戶 nzmo1770 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!