magic00 2020-02-01
(一)进度条
常用属性详解:
android:max:进度条的最大值
android:progress:进度条已完成进度值
android:progressDrawable:设置轨道对应的Drawable对象
android:indeterminate:如果设置成true,则进度条不精确显示进度
android:indeterminateDrawable:设置不显示进度的进度条的Drawable对象
android:indeterminateDuration:设置不精确显示进度的持续时间
android:secondaryProgress:二级进度条,类似于视频播放的一条是当前播放进度,一条是缓冲进度,前者通过progress属性进行设置!
对应的再Java中我们可调用下述方法:
getMax():返回这个进度条的范围的上限
getProgress():返回进度
getSecondaryProgress():返回次要进度
incrementProgressBy(int diff):指定增加的进度
isIndeterminate():指示进度条是否在不确定模式下
setIndeterminate(boolean indeterminate):设置不确定模式下
style:
@android:style/Widget.ProgressBar.Small
@android:style/Widget.ProgressBar.Large
@android:style/Widget.ProgressBar.Horizontal(水平进度条)
在布局文件中加上一个ProgressBar,设置它的id和style样式和进度条的最大值
接下来就是主程序代码来控制进度条的移动
package com.example.progressbar; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.WindowManager; import android.widget.ProgressBar; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private ProgressBar progressBar; private int mProgress=0; private Handler mHanler;//消息处理对象 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏显示 progressBar=(ProgressBar)findViewById(R.id.progressbar); mHanler=new Handler(){ @Override public void handleMessage(@NonNull Message msg) { if(msg.what==0x111){ progressBar.setProgress(mProgress); }else{ Toast.makeText(MainActivity.this,"耗时完成",Toast.LENGTH_SHORT).show(); progressBar.setVisibility(View.GONE); } } }; new Thread(new Runnable() { @Override public void run() { while(true){ mProgress=doWork(); Message m=new Message(); if(mProgress<100) { m.what=0x111; mHanler.sendMessage(m); }else{ m.what=0x110; mHanler.sendMessage(m); break; } } } private int doWork(){ mProgress+=Math.random()*10; try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } return mProgress; } }).start(); } }
程序截图:
(二)拖动条SeekBar
常用属性:
android:max=”100” //滑动条的最大值
android:progress=”60” //滑动条的当前值
android:secondaryProgress=”70” //二级滑动条的进度
android:thumb = “@mipmap/sb_icon” //滑块的drawable
接着要说下SeekBar的事件了,SeekBar.OnSeekBarChangeListener 我们只需重写三个对应的方法:
onProgressChanged:进度发生改变时会触发
onStartTrackingTouch:按住SeekBar时会触发
onStopTrackingTouch:放开SeekBar时触发
在布局界面上放置一个seekbar,并设置id,最大数和当前进度
在主程序中获取seekbar变量,并且设置监听器,来控制拖动的改变
package com.example.seekbar; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.SeekBar; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private SeekBar seekBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); seekBar=(SeekBar)findViewById(R.id.seekBar); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { Toast.makeText(MainActivity.this,"进度改变",Toast.LENGTH_SHORT).show(); } @Override public void onStartTrackingTouch(SeekBar seekBar) { Toast.makeText(MainActivity.this,"开始触摸",Toast.LENGTH_SHORT).show(); } @Override public void onStopTrackingTouch(SeekBar seekBar) { Toast.makeText(MainActivity.this,"停止触摸",Toast.LENGTH_SHORT).show(); } }); } }
截图: