Android香露刀之SeekBar之双管齐下

传送门 ☞ Android兵器谱 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229


绿波香露刀 
        《天龙八部》乌老大:他一说完这番话,当即擎鬼头刀在手,绿光一闪,他身旁众人立时闻到“绿波香露刀”的腥臭之气。。。他料想这柄鬼头刀大有来历,但明明臭得厉害,偏偏叫什么“香露刀”,真是好笑。

        今天我们如何利用Android平台“绿波香露刀”SeekBar实现拖动一定范围内的变量值功能,现实生活中经常用于拖动流媒体文件的当前播放进度和显示网络文件加载缓冲区。下面给出该情景的案例:

一、案例技术要点

1.SeekBar布局设置
android:max="100":设置拖动条的最大值为100
android:progress="30":设置拖动条的一级进度值(初始进度)为30
android:secondaryProgress="60":设置拖动条的二级进度值(缓存)为60
2.为SeekBar所在的Activity添加拖动条滑竿改变当前值的监听OnSeekBarChangeListener,并且为OnSeekBarChangeListener引入该监听。
onStartTrackingTouch(...):滑竿开始滑动时回调该方法
onStopTrackingTouch(...):滑竿结束滑动时回调该方法

二、案例代码陈列

工程包目录


AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.seekbar"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".SeekBarMainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
strings.xml
<resources>
    <string name="app_name">SeekBar拖动滑竿改变当前值</string>
</resources>
main.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:orientation="vertical" >

    <TextView
        android:id="@+id/textview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/textview2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    
    <SeekBar 
        android:id="@+id/seekbar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="30" />
    
    <!-- android:secondaryProgress:标示二级进度(类似缓冲区)-->
    <SeekBar 
        android:id="@+id/seekbar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="30"
        android:secondaryProgress="60" />
</LinearLayout>
SeekBarMainActivity.java
package com.android.seekbar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

/**
 * SeekBar案例:拖动滑竿改变当前值
 * 可以设置一定范围内的变量
 * @author lynnli1229
 */
public class SeekBarMainActivity extends Activity implements OnSeekBarChangeListener{
    private TextView textView1, textView2;
    private SeekBar seekBar1, seekBar2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView1 = (TextView) findViewById(R.id.textview1);
        textView2 = (TextView) findViewById(R.id.textview2);
        seekBar1 = (SeekBar) findViewById(R.id.seekbar1);
        seekBar2 = (SeekBar) findViewById(R.id.seekbar2);
        seekBar1.setOnSeekBarChangeListener(this);
        seekBar2.setOnSeekBarChangeListener(this);
    }

    //滑竿滑动时触发
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if(seekBar.getId() == R.id.seekbar1) {
            textView1.setText("seekBar1的当前位置是:" + progress);
        } else {
            textView2.setText("seekBar2的当前位置是:" + progress);
        }
    }

    //从哪儿开始滑动
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        if(seekBar.getId() == R.id.seekbar1) {
            textView1.setText("seekBar1的开始滑动");
        } else {
            textView2.setText("seekBar2的开始滑动");
        }
    }

    //从哪儿结束滑动
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        if(seekBar.getId() == R.id.seekbar1) {
            textView1.setText("seekBar1的结束滑动");
        } else {
            textView2.setText("seekBar2的结束滑动");
        }
    }
}
三、案例效果展示

 

原文地址:https://www.cnblogs.com/innosight/p/3271187.html