materialish-progress介绍
一个material 风格的滚动式进度条(progress wheel)
建议阅读一下参考资料的二篇文章,你就知道这个控件的使用了.
materialish-progress Demo
主是要实现当我们点击一个button时,在后台启动一个线程,从而实时的更新materialish-progress的进度条,进度条从0增加到100.
- 在build.gradle添加库的依赖
dependencies {
implementation 'com.pnikosis:materialish-progress:1.7'
}
2.布局文件:
<?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"
xmlns:material="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<com.pnikosis.materialishprogress.ProgressWheel
android:id="@+id/progress_wheel"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
material:matProg_barColor="#EF5350"
material:matProg_progressIndeterminate="true" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ProgressWhellDemo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/progress_wheel" />
</android.support.constraint.ConstraintLayout>
注意要加入:
xmlns:material="http://schemas.android.com/apk/res-auto"
或者会报错
3.逻辑实现:
import com.pnikosis.materialishprogress.ProgressWheel;
public class MainActivity extends AppCompatActivity {
private final static String TAG = "ProgressWhellDemo";
private ProgressWheel progress_wheel;
private Button button;
private int progress = 0;
private static final int UPDATE_PROGRESS_WHELL = 0;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case UPDATE_PROGRESS_WHELL:
Log.i(TAG,"handleMessage--progress:"+progress);
Log.i(TAG,"handleMessage--(float) (progress/100.0):"+(float) (progress/100.0));
progress_wheel.setProgress((float) (progress/100.0));
//progress_wheel.setInstantProgress((float) (progress/100.0));
break;
}
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
progress_wheel = (ProgressWheel)findViewById(R.id.progress_wheel);
progress_wheel.setBarColor(Color.BLUE);
progress_wheel.setVisibility(View.VISIBLE);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
updateProgress();
}
});
}
private void updateProgress() {
new Thread(new Runnable() {
@Override
public void run() {
while (progress <= 99){
progress++;
Log.i(TAG,"sendEmptyMessageDelayed--"+progress);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(UPDATE_PROGRESS_WHELL);
}
}
}).start();
}
}
参考资料
1.github materialish-progress
https://github.com/pnikosis/materialish-progress
2.materialish-progress
https://www.cnblogs.com/oshub/articles/4377497.html