Android使用滑动条SeekBar
Posted 油醋三椒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android使用滑动条SeekBar相关的知识,希望对你有一定的参考价值。
layout布局文件(clear_activity.xml):
SeekBar的属性值介绍:
android:max=“255” (最大的滑动值,从0开始)
android:progress=“255”(初始时滑动条的位置)
<?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"
android:background="#e0e0e0">
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:max="255"
android:progress="255"
android:padding="10dp"/>
<TextView
android:id="@+id/seekBar_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="透明度:255"
android:padding="20dp"/>
<ImageView
android:id="@+id/chapter3_imageView"
android:layout_width="wrap_content"
android:layout_height="472dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="70dp"
android:layout_gravity="center_horizontal"
android:scaleType="fitCenter" />
</LinearLayout>
Java活动文件(ClearActivity):
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import java.io.ByteArrayOutputStream;
import java.util.Random;
public class ClearActivity extends Activity
private SeekBar seekBar;
private TextView tv;
private ImageView imageView;
private Bitmap bm;
private Data application;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.clear_activity);
application = (Data)getApplicationContext();//保存全局变量
Bitmap bm = application.getBitmap_edit();//得到具体的全局变量
ImageView iv = (ImageView)this.findViewById(R.id.chapter3_imageView);
iv.setImageBitmap(bm);//为ImageView设置图片
init();
private void init()
imageView = (ImageView) findViewById(R.id.chapter3_imageView);
seekBar = (SeekBar) findViewById(R.id.seekBar);//得到Seek bar
tv = (TextView) findViewById(R.id.seekBar_tv); //得到TextView
//为滑动条Seekbar添加事件监听
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
//滑动时,执行此函数
//progress为当前滑动条的进度
imageView.setImageAlpha(progress);//根据滑动条进度改变图片的透明度
progress = progress-255;
tv.setText("透明度:"+ progress );// 当进度条改变时,改变文本框的显示内容
@Override
public void onStartTrackingTouch(SeekBar seekBar)
//开始滑动时,执行此函数
Toast.makeText(ClearActivity.this,"开始滑动",Toast.LENGTH_SHORT).show();
@Override
public void onStopTrackingTouch(SeekBar seekBar)
//结束滑动时,执行此函数
Toast.makeText(ClearActivity.this,"结束滑动",Toast.LENGTH_SHORT).show();
);
以上是关于Android使用滑动条SeekBar的主要内容,如果未能解决你的问题,请参考以下文章