Android自定義控件實(shí)現(xiàn)帶文字提示的SeekBar
1.寫在前面
SeekBar控件在開發(fā)中還是比較常見的,比如音視頻進(jìn)度、音量調(diào)節(jié)等,但是原生控件有時(shí)還不能滿足我們的需求,今天就來學(xué)習(xí)一下如何自定義SeekBar控件,本文主要實(shí)現(xiàn)了一個(gè)帶文字指示器效果的SeekBar控件
看下最終效果:

IndicatorSeekBar
2.實(shí)現(xiàn)
IndicatorSeekBar
public class IndicatorSeekBar extends AppCompatSeekBar {
// 畫筆
private Paint mPaint;
// 進(jìn)度文字位置信息
private Rect mProgressTextRect = new Rect();
// 滑塊按鈕寬度
private int mThumbWidth = dp2px(50);
// 進(jìn)度指示器寬度
private int mIndicatorWidth = dp2px(50);
// 進(jìn)度監(jiān)聽
private OnIndicatorSeekBarChangeListener mIndicatorSeekBarChangeListener;
public IndicatorSeekBar(Context context) {
this(context, null);
}
public IndicatorSeekBar(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.seekBarStyle);
}
public IndicatorSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mPaint = new TextPaint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.parseColor("#00574B"));
mPaint.setTextSize(sp2px(16));
// 如果不設(shè)置padding,當(dāng)滑動(dòng)到最左邊或最右邊時(shí),滑塊會(huì)顯示不全
setPadding(mThumbWidth / 2, 0, mThumbWidth / 2, 0);
// 設(shè)置滑動(dòng)監(jiān)聽
this.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// NO OP
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
if (mIndicatorSeekBarChangeListener != null) {
mIndicatorSeekBarChangeListener.onStartTrackingTouch(seekBar);
}
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (mIndicatorSeekBarChangeListener != null) {
mIndicatorSeekBarChangeListener.onStopTrackingTouch(seekBar);
}
}
});
}
@Override
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
String progressText = getProgress() + "%";
mPaint.getTextBounds(progressText, 0, progressText.length(), mProgressTextRect);
// 進(jìn)度百分比
float progressRatio = (float) getProgress() / getMax();
// thumb偏移量
float thumbOffset = (mThumbWidth - mProgressTextRect.width()) / 2 - mThumbWidth * progressRatio;
float thumbX = getWidth() * progressRatio + thumbOffset;
float thumbY = getHeight() / 2f + mProgressTextRect.height() / 2f;
canvas.drawText(progressText, thumbX, thumbY, mPaint);
if (mIndicatorSeekBarChangeListener != null) {
float indicatorOffset = getWidth() * progressRatio - (mIndicatorWidth - mThumbWidth) / 2 - mThumbWidth * progressRatio;
mIndicatorSeekBarChangeListener.onProgressChanged(this, getProgress(), indicatorOffset);
}
}
/**
* 設(shè)置進(jìn)度監(jiān)聽
*
* @param listener OnIndicatorSeekBarChangeListener
*/
public void setOnSeekBarChangeListener(OnIndicatorSeekBarChangeListener listener) {
this.mIndicatorSeekBarChangeListener = listener;
}
/**
* 進(jìn)度監(jiān)聽
*/
public interface OnIndicatorSeekBarChangeListener {
/**
* 進(jìn)度監(jiān)聽回調(diào)
*
* @param seekBar SeekBar
* @param progress 進(jìn)度
* @param indicatorOffset 指示器偏移量
*/
public void onProgressChanged(SeekBar seekBar, int progress, float indicatorOffset);
/**
* 開始拖動(dòng)
*
* @param seekBar SeekBar
*/
public void onStartTrackingTouch(SeekBar seekBar);
/**
* 停止拖動(dòng)
*
* @param seekBar SeekBar
*/
public void onStopTrackingTouch(SeekBar seekBar);
}
/**
* dp轉(zhuǎn)px
*
* @param dp dp值
* @return px值
*/
public int dp2px(float dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
getResources().getDisplayMetrics());
}
/**
* sp轉(zhuǎn)px
*
* @param sp sp值
* @return px值
*/
private int sp2px(float sp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp,
getResources().getDisplayMetrics());
}
}
重點(diǎn)看下onDraw方法:
@Override
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
String progressText = getProgress() + "%";
mPaint.getTextBounds(progressText, 0, progressText.length(), mProgressTextRect);
// 進(jìn)度百分比
float progressRatio = (float) getProgress() / getMax();
// thumb偏移量
float thumbOffset = (mThumbWidth - mProgressTextRect.width()) / 2 - mThumbWidth * progressRatio;
float thumbX = getWidth() * progressRatio + thumbOffset;
float thumbY = getHeight() / 2f + mProgressTextRect.height() / 2f;
canvas.drawText(progressText, thumbX, thumbY, mPaint);
if (mIndicatorSeekBarChangeListener != null) {
float indicatorOffset = getWidth() * progressRatio - (mIndicatorWidth - mThumbWidth) / 2 - mThumbWidth * progressRatio;
mIndicatorSeekBarChangeListener.onProgressChanged(this, getProgress(), indicatorOffset);
}
}
再看一遍效果圖:

IndicatorSeekBar
可以看到,進(jìn)度百分比文字是跟著進(jìn)度變化在平移的,所以X軸坐標(biāo)根據(jù)進(jìn)度動(dòng)態(tài)計(jì)算就可以了【總寬度 * 進(jìn)度百分比】(getWidth() * progressRatio),文字需要居中顯示,所以需要向右平移【(滑塊寬度 - 文字寬度)/ 2】( (mThumbWidth - mProgressTextRect.width()) / 2)。
為了避免滑塊滑動(dòng)到終點(diǎn)時(shí)布局被隱藏,需要為SeekBar設(shè)置左右padding,距離分別為滑塊寬度的一半,,所以【控件總長度 = 控件實(shí)際長度 + 滑塊寬度】,向右平移的過程中就要?jiǎng)討B(tài)減去滑塊寬度【滑塊寬度 * 進(jìn)度百分比】(mThumbWidth * progressRatio),到這里文字的X軸坐標(biāo)就計(jì)算完成了。
文字在平移的過程中始終是垂直居中的,所以Y軸坐標(biāo)可以這樣計(jì)算【控件高度 / 2 + 文字高度 / 2】(getHeight() / 2f + mProgressTextRect.height() / 2f),注意drawText方法默認(rèn)是從左下角開始繪制文字的,如果對(duì)繪制文字還不太了解,可以看下這篇文章《Android 圖解Canvas drawText文字居中的那些事》
指示器跟隨滑塊移動(dòng)
在IndicatorSeekBar中,向外提供了一個(gè)setOnSeekBarChangeListener方法用來回調(diào)SeekBar的狀態(tài),其中onProgressChanged方法中的indicatorOffset參數(shù)就是指示器控件的X坐標(biāo),計(jì)算方式與上文中進(jìn)度百分比文字的計(jì)算方式一致:
// 【總寬度 * 進(jìn)度百分比 -(指示器寬度 - 滑塊寬度)/ 2 - 滑塊寬度 * 進(jìn)度百分比】 float indicatorOffset = getWidth() * progressRatio - (mIndicatorWidth - mThumbWidth) / 2 - mThumbWidth * progressRatio; mIndicatorSeekBarChangeListener.onProgressChanged(this, getProgress(), indicatorOffset);
看下如何使用:
public class MainActivity extends AppCompatActivity {
private TextView tvIndicator;
private IndicatorSeekBar indicatorSeekBar;
private Paint mPaint = new Paint();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvIndicator = findViewById(R.id.tv_indicator);
indicatorSeekBar = findViewById(R.id.indicator_seek_bar);
initData();
}
private void initData() {
final LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tvIndicator.getLayoutParams();
indicatorSeekBar.setOnSeekBarChangeListener(new IndicatorSeekBar.OnIndicatorSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, float indicatorOffset) {
String indicatorText = progress + "%";
tvIndicator.setText(indicatorText);
params.leftMargin = (int) indicatorOffset;
tvIndicator.setLayoutParams(params);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
tvIndicator.setVisibility(View.VISIBLE);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
tvIndicator.setVisibility(View.INVISIBLE);
}
});
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_marginStart="20dp" android:layout_marginEnd="20dp" android:orientation="vertical"> <TextView android:id="@+id/tv_indicator" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_indicator" android:gravity="center" android:textColor="#FFFFFF" android:textSize="16sp" android:visibility="invisible" /> <com.yl.indicatorseekbar.IndicatorSeekBar android:id="@+id/indicator_seek_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:background="@null" android:max="100" android:maxHeight="2dp" android:minHeight="2dp" android:progress="50" android:progressDrawable="@drawable/seekbar_progress_drawable" android:thumb="@drawable/seekbar_thumb" /> </LinearLayout> </RelativeLayout>
3.寫在最后
代碼已上傳至GitHub,歡迎Star、Fork!
GitHub地址:https://github.com/alidili/Demos/tree/master/IndicatorSeekBarDemo
本文Demo的Apk下載地址:
https://github.com/alidili/Demos/raw/master/IndicatorSeekBarDemo/IndicatorSeekBarDemo.apk
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- Android自定義控件ListView下拉刷新的代碼
- Android組合式自定義控件實(shí)現(xiàn)購物車加減商品操作
- Android自定義彈窗提醒控件使用詳解
- Android自定義控件實(shí)現(xiàn)按鈕滾動(dòng)選擇效果
- Android自定義動(dòng)畫根據(jù)控件Y軸旋轉(zhuǎn)動(dòng)畫(仿紅包)
- Android自定義控件實(shí)現(xiàn)顏色選擇器
- Android自定義控件實(shí)現(xiàn)不規(guī)則區(qū)域點(diǎn)擊事件
- Android自定義日歷滑動(dòng)控件
- Android自定義圖片輪播Banner控件使用解析
- Android自定義控件的步驟
相關(guān)文章
Android中Binder詳細(xì)學(xué)習(xí)心得
這篇文章主要介紹了Android中Binder詳細(xì)學(xué)習(xí)心得,并分析了Binder的詳細(xì)用法,需要的朋友參考下吧。2018-01-01
Android自定義加載控件實(shí)現(xiàn)數(shù)據(jù)加載動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了Android自定義加載控件實(shí)現(xiàn)數(shù)據(jù)加載動(dòng)畫的相關(guān)資料,仿美團(tuán)、京東數(shù)據(jù)加載動(dòng)畫、小人奔跑動(dòng)畫,感興趣的小伙伴們可以參考一下2016-04-04
Android 實(shí)例開發(fā)一個(gè)學(xué)生管理系統(tǒng)流程詳解
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java實(shí)現(xiàn)一個(gè)學(xué)生管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11
超實(shí)用的Android手勢(shì)鎖制作實(shí)例教程
這篇文章主要介紹了一個(gè)超實(shí)用的Android手勢(shì)鎖制作實(shí)例教程,普通的圓環(huán)形圖標(biāo)變換,在App和系統(tǒng)的鎖屏界面中都可以調(diào)用,需要的朋友可以參考下2016-04-04
react native中的聊天氣泡及timer封裝成的發(fā)送驗(yàn)證碼倒計(jì)時(shí)
這篇文章主要介紹了react native中的聊天氣泡及timer封裝成的發(fā)送驗(yàn)證碼倒計(jì)時(shí)的相關(guān)資料,需要的朋友可以參考下2017-08-08
利用Kotlin實(shí)現(xiàn)破解Android版的微信小游戲--跳一跳
這篇文章主要給大家介紹了關(guān)于利用Kotlin實(shí)現(xiàn)破解Android版微信小游戲--跳一跳的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
android開發(fā)環(huán)境中SDK文件夾下的所需內(nèi)容詳解
在本篇文章里小編給大家整理的是關(guān)于android開發(fā)環(huán)境中SDK文件夾下的所需內(nèi)容詳解,有興趣的朋友們參考學(xué)習(xí)下。2019-09-09

