Android自定義SeekBar滑動(dòng)顯示數(shù)字
本文實(shí)例為大家分享了Android自定義SeekBar滑動(dòng)顯示數(shù)字的具體代碼,供大家參考,具體內(nèi)容如下
先來上個(gè)效果圖:

當(dāng)滑動(dòng)時(shí):數(shù)值顯示,滑動(dòng)停止時(shí)顯示數(shù)字,使用FrameLayout結(jié)合SeekBar。
首先我們看看。
Layout:
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <RelativeLayout android:id="@+id/wrapper_seekbar_indicator" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/img_seekbar_indicator" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" /> <TextView android:id="@+id/txt_seekbar_indicated_progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:textColor="#333333" android:textSize="@dimen/space_12" tools:text="100" /> </RelativeLayout> <RelativeLayout android:id="@+id/wrapper_seekbar" android:layout_width="wrap_content" android:layout_height="wrap_content"> <SeekBar android:id="@+id/seekbar" style="@style/Widget.SeekBar.Normal" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout> </merge>
需要自定義可再上面修改圖片問題顏色等,或者自己封裝起來。
初始化函數(shù)。
private void init(Context context, AttributeSet attrs, int defStyle) {
View view = LayoutInflater.from(context).inflate(
R.layout.view_seekbar_indicated, this);
bindViews(view);
if (attrs != null)
setAttributes(context, attrs, defStyle);
mSeekBar.setOnSeekBarChangeListener(this);
mTextViewProgress.setText(String.valueOf(mSeekBar.getProgress()));
getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onGlobalLayout() {
mMeasuredWidth = mSeekBar.getWidth()
- mSeekBar.getPaddingLeft()
- mSeekBar.getPaddingRight();
mSeekBar.setPadding(
mSeekBar.getPaddingLeft(),
mSeekBar.getPaddingTop()
+ mWrapperIndicator.getHeight(),
mSeekBar.getPaddingRight(),
mSeekBar.getPaddingBottom());
setIndicator();
getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
}
});
// mWrapperIndicator.setVisibility(View.GONE);
}
主要是根據(jù)是否有改變,和觸摸進(jìn)行判斷字和圖片的顯示。
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
setIndicator();
if (mOnSeekBarChangeListener != null)
mOnSeekBarChangeListener.onProgressChanged(seekBar, progress,
fromUser);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
if (mOnSeekBarChangeListener != null) {
mOnSeekBarChangeListener.onStartTrackingTouch(seekBar);
mWrapperIndicator.setVisibility(View.VISIBLE);
}
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (mOnSeekBarChangeListener != null) {
mOnSeekBarChangeListener.onStopTrackingTouch(seekBar);
mWrapperIndicator.setVisibility(View.GONE);
}
}
廢話也不多說,原理很簡單。
工程地址:鏈接地址
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android開發(fā)之橫向滾動(dòng)/豎向滾動(dòng)的ListView(固定列頭)
- android實(shí)現(xiàn)上下滾動(dòng)的TextView
- android TextView不用ScrollViewe也可以滾動(dòng)的方法
- android 實(shí)現(xiàn)ScrollView自動(dòng)滾動(dòng)的實(shí)例代碼
- Android中實(shí)現(xiàn)多行、水平滾動(dòng)的分頁的Gridview實(shí)例源碼
- Android GridView實(shí)現(xiàn)滾動(dòng)到指定位置的方法
- android開發(fā)教程之文本框加滾動(dòng)條scrollview
- Android SeekBar實(shí)現(xiàn)禁止滑動(dòng)
- Android SeekBar實(shí)現(xiàn)滑動(dòng)條效果
- Android SeekBar實(shí)現(xiàn)平滑滾動(dòng)
相關(guān)文章
在Android設(shè)備上搭建Web服務(wù)器的方法
本篇文章主要介紹了在Android設(shè)備上搭建Web服務(wù)器的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
Android延遲實(shí)現(xiàn)的幾種解決方法及原理分析
這篇文章主要給大家介紹了關(guān)于Android延遲實(shí)現(xiàn)的幾種解決方法以及其中的原理分析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
淺析Kotlin使用infix函數(shù)構(gòu)建可讀語法流程講解
這篇文章主要介紹了淺析Kotlin使用infix函數(shù)構(gòu)建可讀語法,我們在Kotlin中就多次使用A to B這樣的語法結(jié)構(gòu)構(gòu)建鍵值對,包括Kotlin自帶的mapOf()函數(shù),這種語法結(jié)構(gòu)的優(yōu)點(diǎn)是可讀性強(qiáng)2023-01-01
基于Android實(shí)現(xiàn)自動(dòng)滾動(dòng)布局
在平時(shí)的開發(fā)中,有時(shí)會(huì)碰到這樣的場景,設(shè)計(jì)上布局的內(nèi)容會(huì)比較緊湊,導(dǎo)致部分機(jī)型上某些布局中的內(nèi)容顯示不完全,或者在數(shù)據(jù)內(nèi)容多的情況下,單行無法顯示所有內(nèi)容,這里給大家簡單介紹下布局自動(dòng)滾動(dòng)的一種實(shí)現(xiàn)方式,感興趣的朋友可以參考下2023-12-12
android開發(fā)教程之卸載sd卡對MediaServer的處理
Android中如果MediaServer訪問SD卡上的音頻文件,卸載SD卡的時(shí)候,就會(huì)kill掉MediaServer,卸載SD卡上必要條件就是沒有進(jìn)程訪問SD卡上的資源文件。Kill掉MediaServer的進(jìn)程后,MediaServer會(huì)重新啟動(dòng)。2014-02-02
詳解Android提交數(shù)據(jù)到服務(wù)器的兩種方式四種方法
本篇文章主要介紹了Android提交數(shù)據(jù)到服務(wù)器的兩種方式四種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-11-11
淺談AnDroidDraw+DroidDraw實(shí)現(xiàn)Android程序UI設(shè)計(jì)的分析說明
本篇文章是對AnDroidDraw+DroidDraw實(shí)現(xiàn)Android程序UI設(shè)計(jì)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android啟動(dòng)屏實(shí)現(xiàn)左右滑動(dòng)切換查看功能
這篇文章主要介紹了Android啟動(dòng)屏實(shí)現(xiàn)左右滑動(dòng)切換查看功能的相關(guān)資料,針對新功能屬性介紹和啟動(dòng)屏進(jìn)行詳細(xì)講解,感興趣的小伙伴們可以參考一下2016-01-01

