Android development-Android common components-SeekBar drag bar

4.9 SeekBar drag bar

android:max

The maximum value of the slider

android:progress

the current value of the slider

android:secondaryProgress

The progress of the secondary slider

android:thumb

The drawable of the slider

 

Next, let’s talk about SeekBar events. For SeekBar.OnSeekBarChangeListener , we only need to rewrite three corresponding methods:

onProgressChanged

Fired when progress changes

onStartTrackingTouch

Triggered when the SeekBar is held down

onStopTrackingTouch

Triggered when the SeekBar is released

SeekBar customization

1. Slider state Drawable: sb_thumb.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@mipmap/weixin"/>
    <item android:state_pressed="false" android:drawable="@mipmap/wode"/>
</selector>

2. Drawable of bar bar: sb_bar.xml

A layer-list drawable resource is used here! In fact, it is a stack of pictures, in order: background, secondary progress bar, current progress:

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <solid android:color="@color/purple_200"/>
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <solid android:color="@color/dark_gray"/>
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="@color/black"/>
            </shape>
        </clip>
    </item>
</layer-list>

3. After the layout is introduced into the SeekBar, set the progressDrawable and thumb

 seek_bar.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
    <SeekBar
        android:id="@+id/sb_normal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/txt_cur"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textSize="30sp"/>

    <SeekBar
        android:id="@+id/sb_custom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxHeight="5dp"
        android:minHeight="5dp"
        android:progressDrawable="@drawable/sb_bar"
        android:thumb="@drawable/sb_thumb"/>

</LinearLayout>

 SeekBarActivity.java:

package com.example.myapplication;

import android.content.Context;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class SeekBarActivity extends AppCompatActivity {
    private SeekBar sb_normal,sb_custom;
    private TextView txt_cur;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.seek_bar);
        mContext = this;
        sb_normal = (SeekBar) findViewById(R.id.sb_normal);
        sb_custom = (SeekBar) findViewById(R.id.sb_custom);
        txt_cur = (TextView) findViewById(R.id.txt_cur);
        sb_normal.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                txt_cur.setText("当前进度值:"+progress+"/100");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                Toast.makeText(mContext,"触碰SeekBar",Toast.LENGTH_LONG).show();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Toast.makeText(mContext,"放开SeekBar",Toast.LENGTH_LONG).show();
            }
        });

    //二级缓冲条
    sb_custom.setSecondaryProgress(20);
} }

AndroidManifest.java:

 Start the test:

Guess you like

Origin blog.csdn.net/LYly_B/article/details/129943634