1. 程式人生 > >Android Seekbar間隔和自定義

Android Seekbar間隔和自定義

最近在專案中使用到了seekbar和progressbar,且必須按照設計要求來進行設定,如下圖。要實現這個效果就必須對這兩個控制元件進行自定義。
 
 一,SeekBar
  
   一開始要實現這個效果參考網上的自定義方法根本無法達到這個效果,沒辦法只能投機取巧了。

 1,背景刻度的圖片我是用了一個ImageView,然後在ImageView上放一個SeekBar。因為是個定製的平板應用,解析度是限定的1280*768,所以我使用的是AbsoluteLayout這樣ImageView和SeekBar的位置和大小都是固定的了,估計在其他佈局中這樣使用會有問題。

2,在佈局檔案中的程式碼如下:

  1.    <ImageView  
  2.     android:layout_width="400dip"  
  3.     android:layout_height="95dip"  
  4.     android:layout_x="830dip"  
  5.     android:layout_y="484dip"  
  6.     android:src="@drawable/seekbar_background_5" //刻度圖片  
  7.     android:scaleType="centerCrop"  
  8.     android:background="@null"  
  9.     />  
  10.    <SeekBar
      
  11.     android:id="@+id/sensor_sensitivity"  
  12.     android:layout_width="360dip"  
  13.     android:layout_height="64dip"  
  14.     android:layout_x="850dip"  
  15.     android:layout_y="498dip"  
  16.     android:progressDrawable="@drawable/suretouch_seekbar_img"  
  17.     android:thumb="@drawable/suretouch_seekbar_thumb"  
  18.     style
    ="?android:attr/progressBarStyleHorizontal"  
  19.     android:paddingLeft="5dip"  
  20.     android:paddingRight="5dip"  
  21.     android:paddingBottom="2dip"  
  22.     android:maxHeight="1dip"  //注意:一定得設定進度條的高度,不然進度條會很高。  
  23.     android:minHeight="1dip"  
  24.         android:max="100"  
  25. android:progress="0"          
  26.    />  
  1. <ImageView
  2.     android:layout_width="400dip"
  3.     android:layout_height="95dip"
  4.     android:layout_x="830dip"
  5.     android:layout_y="484dip"
  6.     android:src="@drawable/seekbar_background_5" //刻度圖片  
  7.     android:scaleType="centerCrop"
  8.     android:background="@null"
  9.     />
  10.    <SeekBar
  11.     android:id="@+id/sensor_sensitivity"
  12.     android:layout_width="360dip"
  13.     android:layout_height="64dip"
  14.     android:layout_x="850dip"
  15.     android:layout_y="498dip"
  16.     android:progressDrawable="@drawable/suretouch_seekbar_img"
  17.     android:thumb="@drawable/suretouch_seekbar_thumb"
  18.     style="?android:attr/progressBarStyleHorizontal"
  19.     android:paddingLeft="5dip"
  20.     android:paddingRight="5dip"
  21.     android:paddingBottom="2dip"
  22.     android:maxHeight="1dip"  //注意:一定得設定進度條的高度,不然進度條會很高。  
  23.     android:minHeight="1dip"
  24.         android:max="100"
  25. android:progress="0"
  26.    />


 3,自定義滑塊,在drawable檔案中加入自定義的xml檔案。

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">         
  3.     <!-- 按下狀態 -->      
  4.     <item         
  5.         android:state_pressed="true"         
  6.         android:drawable="@drawable/seekbar_block" />        
  7.     <!-- 普通無焦點狀態 -->      
  8.     <item         
  9.         android:state_focused="false"         
  10.         android:state_pressed="false"       
  11.         android:drawable="@drawable/seekbar_block" />     
  12. </selector>   
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <selectorxmlns:android="http://schemas.android.com/apk/res/android">
  3.     <!-- 按下狀態 -->
  4.     <item
  5.         android:state_pressed="true"
  6.         android:drawable="@drawable/seekbar_block"/>
  7.     <!-- 普通無焦點狀態 -->
  8.     <item
  9.         android:state_focused="false"
  10.         android:state_pressed="false"
  11.         android:drawable="@drawable/seekbar_block"/>
  12. </selector>


4,自定義進度條的顏色,同樣在drawable中加入自定義需要的xml檔案。

  1. <?xml version="1.0" encoding="UTF-8"?>       
  2. <layer-list  
  3.   xmlns:android="http://schemas.android.com/apk/res/android">  
  4.      <item android:id="@android:id/progress">    
  5.          <clip>    
  6.              <shape>    
  7.                  <gradient    
  8.                          android:startColor="@color/big_title"    
  9.                          android:centerColor="@color/big_title"    
  10.                          android:endColor="@color/big_title"    
  11.                  />    
  12.              </shape>    
  13.          </clip>    
  14.      </item>    
  15. </layer-list>  
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <layer-list
  3.   xmlns:android="http://schemas.android.com/apk/res/android">
  4.      <itemandroid:id="@android:id/progress">
  5.          <clip>
  6.              <shape>
  7.                  <gradient
  8.                          android:startColor="@color/big_title"
  9.                          android:centerColor="@color/big_title"
  10.                          android:endColor="@color/big_title"
  11.                  />
  12.              </shape>
  13.          </clip>
  14.      </item>
  15. </layer-list>


5,設定滑塊的位置,也就是當滑動滑塊後只能讓其停在刻度上,要現實這個效果我採用的方法是當滑塊停止的時候判斷當前的值,比如第二個刻度是25,這裡在0到25中去箇中間數比如13,也就是當滑塊滑到大於13小於25到50的中間數時就setProgress(25),這樣就設定在25的位置也就是第二個刻度位置。後面的以此類推。seekbar的事件中有個OnStopTrackingTouch,程式碼如下:

  1. public void onStopTrackingTouch(SeekBar seekBar) {  
  2.         // TODO Auto-generated method stub   
  3.         int seekProgress = mSeekBar.getProgress();  
  4.         if(seekProgress<13){  
  5.             mSeekBar.setProgress(0);  
  6.         }else if(seekProgress>=13 && seekProgress<38){  
  7.             mSeekBar.setProgress(25);  
  8.         }else if(seekProgress>=38 && seekProgress<63){  
  9.             mSeekBar.setProgress(50);  
  10.         }else if(seekProgress>=63 && seekProgress<88){  
  11.             mSeekBar.setProgress(75);  
  12.         }else if(seekProgress>=88){  
  13.             mSeekBar.setProgress(100);  
  14.         }  
  15.     }  
  1. 相關推薦

    Android Seekbar間隔定義

    最近在專案中使用到了seekbar和progressbar,且必須按照設計要求來進行設定,如下圖。要實現這個效果就必須對這兩個控制元件進行自定義。    一,SeekBar       一開始要實現這個效果參考網上的自定義方法根本無法達到這個效果,沒辦法只能投機取巧了。

    android設定主題定義主題的方法

    嘿嘿,書接上回,android有兩種設定主題到方法,一種就是通過修改manifest檔案,一種就是在通過修改我們的java程式碼;主題的自定義設定和樣式的自定義設定類似。 首先我們先來自定兩個主題檔案。 在res/values/style.xml新增如下程式碼: <style name="My

    Android之ToolBar定義ToolBar實現沉浸式狀態列

    沉浸式狀態列確切的說應該叫做透明狀態列。一般情況下,狀態列的底色都為黑色,而沉浸式狀態列則是把狀態列設定為透明或者半透明。 沉浸式狀態列是從android Kitkat(Android 4.4)開始出

    獲取android的拍照定義多選相簿

        獲取系統的相機功能拍照這個不難,但是需要注意的是,拍照返回後的照片如果沒有指定儲存的路徑,那麼系統將自動儲存到sd卡中,得到的是拍完照的縮圖,會失幀,顯示有些模糊,所以在呼叫系統相機拍完照後我們要指定一個路徑,將它存起來,需要的時候再去拿,呼叫相簿這裡當時需要的是相

    Android零基礎入門第39節:ListActivity定義列表項

    arraylist component save 高速 ram 如果 view設置 ren 屬性 相信通過前兩期的學習,以及會開發最簡單的一些列表界面了吧,那麽本期接著來學習更多方法技巧。 一、使用ListActivity 如果程序的窗口僅僅需要

    Android中引入佈局定義控制元件

    首先是引入佈局: 1.我們自己新建一個layout,就是一個標題欄。 2.然後在我們的mainactivity_layout中使用一個語句就可以實現。 <?xml version="1.0" encoding="utf-8"?> <LinearLayout

    Android RecyclerView 詳解 RecyclerView的動畫實現(移除、新增、改變、移動)定義動畫的實現

    一丶新增刪除時候的重新整理問題 先上一下效果圖吧 1.為了方便起見我們還是先新增三個按鈕分別實現新增刪除和改變 2.在Adapter中寫呼叫方法並進行重新整理 public void remove(int position){ list.re

    Android的基本元件定義檢視

    Android的基本元件 1 Activity 1.1 Activity代表手機的一個螢幕 1.2 一個Android程式由多個Activity組成,即:一個Android程式由多屏內容組成 1.3 Activity相當於一個展板

    android 註解學習筆記二: 元註解定義註解

    首先看一個自定義的註解: 1、自定義註解 public @interface MyAnnotation { int age(); } 可見定義一個註解非常簡單,只需要使用@interface關鍵字來定義即可。 同時我們可以看到,註解的內部可以定義變

    Android使用開源框架完成城市列表三級聯動(從服務端獲取資料來源定義json資料來源)

    Android-PickerView使用步驟:1.新增Jcenter倉庫 Gradle依賴:compile 'com.contrarywind:Android-PickerView:4.1.4'2.在Activity中新增如下程式碼:package com.xueqing.r

    android藍芽4.0BLE及2.0 2.1 apk 串列埠助手帶16個定義按鍵定義指令 字元接收 十六進位制或字元傳送

    android藍芽4.0BLE apk 帶16個自定義按鍵和自定義指令 字元接收 https://pan.baidu.com/s/1eRSfprO android藍芽2.0 2.1 apk 帶16個自定義按鍵和自定義指令 字元接收  帶自動連線 https://pan.b

    Android中Dialog的常用方法彙總定義Dialog的步驟.txt

    一、系統自帶Dialog對話方塊的使用: 警告框(AlertDialog)是在專案中出現的最簡單的一種對話方塊,主要的目的是為使用者顯示一條警告資訊,AlertDialog也是在對話方塊中使用最多的一個類,而且是Dialog的直接子類,此類繼承結構如下: j

    Android中Spinner下拉列表(使用ArrayAdapter定義Adapter實現)

         今天學習了Spinner元件,使用Spinner相當於從下拉列表中選擇專案,下面演示一下Spinner的使用(分別使用ArrayAdapter和自定義Adapter實現) (一):使用Arr

    Android百度地圖開發學習筆記(二)之定位當前位置定義控制元件返回

    在完成HelloMap後,接來完成的重要功能是如何定位當前位置和如何一鍵返回。效果圖如下: 這裡的控制元件就是一個ImageView,自己去百度一個好看的圖片就可以了。 一 定位當前位置和自定義控制元件返回 1.官方技術文件 可以先點選百度地圖定位技術文件,仔細看一下相關

    Android中的通知定義通知佈局

    Android中的通知(Notification)是Android中的重要一部分,應用程式通過通知來提醒使用者或者向用戶傳達資訊,下面讓我們來看一下怎麼在我們的程式中使用通知和自定義通知的佈局。 首先我們來看一下怎麼向通知欄中傳送一個通知。由於各個版本的And

    Android監聽系統廣播 定義廣播遇到的問題

    現在有一個專案需要做一個開機自啟動的功能,這個怎麼實現呢?一頓咔咔咔百度,發現很多推薦 使用監聽系統開機廣播BOOT_COMPLETED,CONNECTIVITY_CHANGE 來實現。那好吧  我也這麼來做。先寫一個RootReceiver  extends Broadc

    android 屬性動畫(view普通使用 定義view使用)

    1、概述 普通的動畫主要是Animation 和   Animator(與5.0的切換動畫,介面共享元素動畫區分開)  Animation 分 TranslateAnimation 移動 scaleAnimation 縮放 RotateAnimation 旋轉 AlphaA

    Android WebView Cookie的相關設定定義錯誤頁面載入

    這個是WebView的後篇吧,一拖又不知道拖了多久了,言歸正傳,上一次大概翻譯了一些WebView的官方介紹,然後剩下了WebView的Cookie相關另外就是載入頁面錯誤那個醜醜的頁面的處理。 內容簡介: WebView Cookie的相關使用! 錯誤頁

    Android中ClipDrawable的使用定義ProgressBar

    package com.example.ztest2; import java.lang.ref.WeakReference; import android.content.Context; import android.content.res.TypedArray; import android.gra

    [Android] 仿IOS實現定義Dialog,底部彈窗中間彈窗工具

    用過Android的預設對話方塊的都懂,不管是哪個版本的對話方塊,都醜到爆!就算是Google推崇的Material Design風格的彈窗一樣不好看,基本每款APP都不會去使用預設的對話方塊樣式,他們都有自己的風格,怎樣去改變預設的對話方塊樣式呢?只能自定義了,將系統對話方