Android 開発 - Android 共通コンポーネント - SeekBar ドラッグ バー

4.9 SeekBar ドラッグバー

アンドロイド:マックス

スライダーの最大値

アンドロイド:進捗状況

スライダーの現在の値

アンドロイド:セカンダリの進行状況

二次スライダーの進行状況

アンドロイド:親指

スライダーのドローアブル

 

次に、SeekBar イベントについて話しましょう。SeekBar.OnSeekBarChangeListener の場合、対応する 3 つのメソッドを書き直すだけで済みます。

進行状況が変更されました

進行状況が変化したときに発生します

onスタートトラッキングタッチ

SeekBar を押し続けるとトリガーされます

オンストップトラッキングタッチ

SeekBar が解放されるとトリガーされます

シークバーのカスタマイズ

1. スライダー状態のドローアブル: 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.バーバーのドローアブル:sb_bar.xml

ここではレイヤーリスト描画可能リソースが使用されています。実際、これは背景、二次進行状況バー、現在の進行状況の順に画像のスタックになっています。

<?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. レイアウトが SeekBar に導入された後、progressDrawable とサムを設定します。

 シークバー.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:

 テストを開始します。

おすすめ

転載: blog.csdn.net/LYly_B/article/details/129943634