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

當滑動時:數(shù)值顯示,滑動停止時顯示數(shù)字,使用FrameLayout結合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ù)是否有改變,和觸摸進行判斷字和圖片的顯示。
@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);
}
}
廢話也不多說,原理很簡單。
工程地址:鏈接地址
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- android開發(fā)之橫向滾動/豎向滾動的ListView(固定列頭)
- android實現(xiàn)上下滾動的TextView
- android TextView不用ScrollViewe也可以滾動的方法
- android 實現(xiàn)ScrollView自動滾動的實例代碼
- Android中實現(xiàn)多行、水平滾動的分頁的Gridview實例源碼
- Android GridView實現(xiàn)滾動到指定位置的方法
- android開發(fā)教程之文本框加滾動條scrollview
- Android SeekBar實現(xiàn)禁止滑動
- Android SeekBar實現(xiàn)滑動條效果
- Android SeekBar實現(xiàn)平滑滾動
相關文章
Android仿今日頭條多個fragment懶加載的實現(xiàn)
我們在做應用開發(fā)的時候,一個Activity里面可能會以viewpager(或其他容器)與多個Fragment來組合使用,下面這篇文章主要給大家介紹了關于利用Android仿今日頭條多個fragment懶加載的相關資料,需要的朋友可以參考下。2017-12-12
Android OpenGL入門之GLSurfaceView
這篇文章主要介紹了OpenGL入門知識,如何在Android中使用GLSurfaceView,如果對OpenGL感興趣的同學,可以參考下2021-04-04
Android動態(tài)模糊效果的快速實現(xiàn)方法
這篇文章主要介紹了Android動態(tài)模糊效果的快速實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01

