android開發筆記之materialish-progress

materialish-progress介紹

一個material 風格的滾動式進度條(progress wheel)
建議閱讀一下參考資料的二篇文章,你就知道這個控件的使用了.

materialish-progress Demo

主是要實現當我們點擊一個button時,在後臺啓動一個線程,從而實時的更新materialish-progress的進度條,進度條從0增加到100.

  1. 在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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章