
image.png
目錄
SeekBar
SeekBar 是水平進(jìn)度條 ProgressBar 的間接子類,相當(dāng)于一個(gè)可以拖動的水平進(jìn)度條。
使用時(shí)需要在xml文件中添加:
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"/>
基本使用方式和進(jìn)度條差不多.
使用實(shí)例
activity_main.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"/>
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="89dp"
android:layout_marginBottom="385dp"
android:max="100"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView1"
tools:layout_editor_absoluteX="0dp" />
</android.support.constraint.ConstraintLayout>
代碼文件:
package com.example.user.seekbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private SeekBar seekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView1);
seekBar = (SeekBar)findViewById(R.id.seekBar1);
textView.setText("當(dāng)前進(jìn)度為:" + "0%");
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
/*拖動條停止拖動時(shí)調(diào)用 */
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
/*拖動條開始拖動時(shí)調(diào)用*/
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
/* 拖動條進(jìn)度改變時(shí)調(diào)用*/
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
textView.setText("當(dāng)前進(jìn)度為:" + progress + "%");
}
});
}
}
實(shí)例效果:

image.png
使用SeekBar改變圖片透明度
activity_main.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView1"
android:layout_width="400dp"
android:layout_height="400dp"
android:src="@drawable/ppp"/>
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="89dp"
android:layout_marginBottom="385dp"
android:max="100"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView1"
app:layout_constraintVertical_bias="0.28"
tools:layout_editor_absoluteX="0dp" />
</android.support.constraint.ConstraintLayout>
代碼文件:
package com.example.user.seekbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private SeekBar seekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.imageView1);
seekBar = (SeekBar)findViewById(R.id.seekBar1);
seekBar.setMax(255);
imageView.setImageAlpha(255);
seekBar.setProgress(255);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
/*拖動條停止拖動時(shí)調(diào)用 */
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
/*拖動條開始拖動時(shí)調(diào)用*/
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
/* 拖動條進(jìn)度改變時(shí)調(diào)用*/
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
imageView.setImageAlpha(progress);
}
});
}
}
執(zhí)行效果:

image.png
只是對上述代碼做了一點(diǎn)點(diǎn)修改. O(∩_∩)O