SeekBar拖动条

时间:2022-04-18 19:50:57

seekbar拖动条,通过滑块的位置值的改变,来对某些数据进行调节,比如音量调节; 改变滑块的外观可以通过android:thumb属性完成

实例:拖动滑块改变图片透明度

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <SeekBar
android:id="@+id/sb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max=""
android:progress="" /> <ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" /> </LinearLayout>
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seek); SeekBar sb = (SeekBar) findViewById(R.id.sb);
final ImageView iv = (ImageView) findViewById(R.id.iv);
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override
public void onStopTrackingTouch(SeekBar seekBar) {
} @Override
public void onStartTrackingTouch(SeekBar seekBar) {
} @Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
iv.setAlpha(progress);
}
});
} }