拖动条(SeekBar)使用
2019-10-21 本文已影响0人
Lee_5566
image.png
image.png
目录
SeekBar
SeekBar 是水平进度条 ProgressBar 的间接子类,相当于一个可以拖动的水平进度条。
使用时需要在xml文件中添加:
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"/>
基本使用方式和进度条差不多.
使用实例
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("当前进度为:" + "0%");
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
/*拖动条停止拖动时调用 */
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
/*拖动条开始拖动时调用*/
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
/* 拖动条进度改变时调用*/
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
textView.setText("当前进度为:" + progress + "%");
}
});
}
}
实例效果:
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(){
/*拖动条停止拖动时调用 */
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
/*拖动条开始拖动时调用*/
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
/* 拖动条进度改变时调用*/
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
imageView.setImageAlpha(progress);
}
});
}
}
执行效果:
image.png
只是对上述代码做了一点点修改. O(∩_∩)O