当前位置:网站首页>Seekbar custom style details
Seekbar custom style details
2022-04-23 03:33:00 【Jon_ Lo】
SeekBar Customize style Detailed explanation
SeekBar brief introduction
SeekBar yes ProgressBar An extension of , Added a draggable slider .
When we use the progress bar , You can use the default progress bar ;
You can also customize the pictures of the progress bar and slider .
The user can touch his thumb and drag left or right to set the current progress level , Or use the arrow keys .
It is not recommended to place focusable widgets in SeekBar To the left or right of .
Custom effects :

<SeekBar
android:id="@+id/sb_screen_bri"
android:layout_width="match_parent"
android:layout_height="100dp"
android:progress="66"
android:progressDrawable="@drawable/poc_seek_bar_drawable"
android:splitTrack="false"
android:thumbTint="@android:color/transparent" />
<?xml version="1.0" encoding="utf-8"?><!-- Copyright (C) 2018 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@android:id/background"
android:gravity="center_vertical|fill_horizontal">
<shape android:shape="rectangle">
<size android:height="10dp" />
<solid android:color="#282b31" />
<corners android:radius="10dp" />
</shape>
</item>
<item
android:id="@android:id/progress"
android:gravity="center_vertical|fill_horizontal">
<scale android:scaleWidth="100%">
<shape android:shape="rectangle">
<size android:height="10dp" />
<!--<solid android:color="#707d93" />-->
<gradient
android:angle="0"
android:endColor="#aab8d4"
android:startColor="#707d93" />
<corners android:radius="10dp" />
</shape>
</scale>
</item>
</layer-list>
structural analysis :

attribute :
android:maxHeight // Background height
android:progressDrawable // Progress bar background
android:thumb // slider
android:splitTrack // hua block Whether to cut seekbar background , Default true, Will see thumb The surrounding area is cut
seekbar Expand :
1、 The slider cannot be on the far left of the progress bar
Solution : Set offset
android:thumbOffset="5dp"
2、 The slider cannot be centered in the progress bar
Solution : Set height adaptation , Will automatically fit the slider height
android:layout_height="wrap_content"
Then use the following properties to set the height
android:maxHeight="30dip"
android:minHeight="30dip"
3、 How to write on the slider
Solution : rewrite ondraw Method
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
Rect rect = new Rect();
this.mPaint.getTextBounds(this.text, 0, this.text.length(), rect);
int x = (getWidth() / 2) - rect.centerX();
int y = (getHeight() / 2) - rect.centerY();
canvas.drawText(this.text, x, y, this.mPaint);
}
stay seekbar Cover it with a TextView
<?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="wrap_content"
android:padding="16dp">
<SeekBar
android:id="@+id/sb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="0"
android:progressDrawable="@drawable/seekbar_bg"
android:thumb="@drawable/thumb"
android:thumbOffset="0dp" />
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text=" Please press and hold the slider , Drag to the far right "
android:textColor="#888888"
android:textSize="14dp" />
</RelativeLayout>
4、seekbar in thumb The opacity effect
Problem description :
stay Android5.0 The above will appear thumb There is a circle of unknown white things around , Occlusion to parent layout
Solution :
Set up splitTrack The attribute is false That's all right.
android:splitTrack="false"
5、seekbar There will be a certain margin around the distance
Solution :
Set up seekbar Of paddingleft And paddingright by 0dp
android:paddingStart="0dp"
android:paddingEnd="0dp"
6、 prohibit seekbar Click jump progress , Just drag and drop
Solutions :
Save an old location progress——oldsign, The default is 0, The user clicks seekbar In the rear , Record the user clicks progress
By judging the currently clicked progress whether < (oldsign+ A certain value ),
If yes, you can drag , If not, set seekbar Location progress by 0, So as to achieve the immobility effect .
Solution 1 :
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class ValidateSeekBar extends SeekBar implements OnSeekBarChangeListener{
private Context context;
// Record old location
private int oldsign;
// Write on the progress bar text My brush
private Paint mPaint;
private String textStr =" Please drag to the far right to complete the verification ";
//text Content
private String text=textStr;
//text font size
private int textSize =20;
//text Color
private String textColor ="#607B8B";
public interface ValidateSeekBarCallBack {
void onProgressChangedCallBack(SeekBar seekbar, int progress, boolean arg2);
void onStartTrackingTouchCallBack(SeekBar seekbar);
void onStopTrackingTouchCallBack(SeekBar seekbar);
}
private ValidateSeekBarCallBack callback;
public ValidateSeekBar(Context context) {
super(context);
this.context = context;
init();
}
public ValidateSeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
init();
}
public ValidateSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
}
private void init() {
this.mPaint = new Paint();
this.mPaint.setColor(Color.parseColor(textColor));
this.mPaint.setTextSize(textSize);
setMax(100);
setOnSeekBarChangeListener(this);
}
@Override
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
Rect rect = new Rect();
this.mPaint.getTextBounds(this.text, 0, this.text.length(), rect);
int x = (getWidth() / 2) - rect.centerX();
int y = (getHeight() / 2) - rect.centerY();
canvas.drawText(this.text, x, y, this.mPaint);
}
@Override
public void onProgressChanged(SeekBar seekbar, int progress, boolean arg2) {
// control textview It will flash bug
if(progress<(seekbar.getMax()/9+1)&&progress!=0){
text="";
}
// When the click position is greater than (0 + 100/9+1) Can't be dragged ,+ The larger the number after the number , The greater the range that allows the front to start dragging
// Why use “/”+ Certain value ? Because the maximum value of preventing progress bar may be 1000、500、300 etc. , So they all take the progress bar 9 Divided 1 + A custom value
// Why not change the judgment to the current position greater than 0? Because when you click the slider ,progerss Has been changed
if(progress>oldsign+(seekbar.getMax()/9+1)){
seekbar.setProgress(oldsign);
return;
}
seekbar.setProgress(progress);
oldsign = progress;
if(this.callback!=null){
this.callback.onProgressChangedCallBack(seekbar,progress,arg2);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekbar) {
seekbar.setProgress(oldsign);
if(this.callback!=null){
this.callback.onStartTrackingTouchCallBack(seekbar);
}
}
@Override
public void onStopTrackingTouch(SeekBar seekbar) {
if(seekbar.getProgress()!=seekbar.getMax()){
oldsign=0;
text =textStr;
seekbar.setProgress(oldsign);
}
else{
text =" To complete the verification ";
}
if(this.callback!=null){
this.callback.onStopTrackingTouchCallBack(seekbar);
}
}
/**
* seekbar Callback
* @param callback
*/
public void setValidateSeekBarCallBack(ValidateSeekBarCallBack callback){
this.callback =callback;
}
public void refreshText(){
text=textStr;
}
}
This plan , Because the drag range ahead is too difficult to control , If you just click the position near the slider button , The slider will move , It won't move until it's outside the drag range . If you click in the middle to slide ,progress And slide along
Solution 2 : It can solve the problem of dragging from the middle
public class MainSeekbar extends android.support.v7.widget.AppCompatSeekBar implements SeekBar.OnSeekBarChangeListener {
private static final int INDEX = 100;
private static final int FINISH = 100;
private boolean isMove = true;
private OnSeekBarListener mOnSeekBarListener;
public MainSeekbar(Context context) {
super(context);
setOnSeekBarChangeListener(this);
}
public MainSeekbar(Context context, AttributeSet attrs) {
super(context, attrs);
setOnSeekBarChangeListener(this);
}
public MainSeekbar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setOnSeekBarChangeListener(this);
}
public void setOnSeekBarListener(OnSeekBarListener onSeekBarListener) {
mOnSeekBarListener = onSeekBarListener;
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
int x = (int) event.getX();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
isMove = true;
if (x - INDEX > 0) {
isMove = false;
return true;
}
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (!isMove) {
return true;
}
}
return super.dispatchTouchEvent(event);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (seekBar.getProgress() != FINISH || !isMove) {
seekBar.setProgress(0);
mOnSeekBarListener.onStart();
} else {
mOnSeekBarListener.onFinish();
}
}
protected interface OnSeekBarListener {
void onFinish();
void onStart();
}
}
OVER ~! For reference only ~!
版权声明
本文为[Jon_ Lo]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220607064165.html
边栏推荐
- TCP three handshakes and four waves
- MySQL之explain关键字详解
- Advanced sorting - fast sorting
- The website JS in. Net core cefsharp chromium WebBrowser calls the C method in winfrom program
- Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses
- Database SQL -- simulate inserting a large amount of data, importing / exporting database scripts, timestamp conversion and database basics
- Application and definition of interface
- Query stored procedures in PostgreSQL
- Do you really understand hashcode and equals???
- Codeforces Round #784 (Div. 4)題解 (第一次AK cf (XD
猜你喜欢

Idea debug debugging tutorial

JS inheritance

PYMOL-note

淺學一下I/O流和File類文件操作

"Visual programming" test paper

The art of concurrent programming (6): explain the principle of reentrantlock in detail

Visual programming - Experiment 1

C set

AWS from entry to actual combat: creating accounts

Notes sur l'apprentissage profond (Ⅱ) - - Principe et mise en oeuvre de la fonction d'activation
随机推荐
2021-08-31
. net webapi access authorization mechanism and process design (header token + redis)
2022 group programming ladder simulation match 1-8 are prime numbers (20 points)
On the principle of concurrent programming and the art of notify / Park
JS calculates the display date according to the time
Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800
Database - MySQL -- Navicat import SQL error 1067 - invalid default value for 'paydate‘
你真的懂hashCode和equals吗???
Applet - canvas drawing Poster
Unity basics 2
QT learning summary
Build websocket server in. Net5 webapi
Batch download of files ---- compressed and then downloaded
2022 团体程序设计天梯赛 模拟赛 L2-4 哲哲打游戏 (25 分)
打卡:4.22 C语言篇 -(1)初识C语言 - (11)指针
深度学习笔记(二)——激活函数原理与实现
C interface
淺學一下I/O流和File類文件操作
C abstract class
Three types of jump statements