1. 程式人生 > >Android開發之更優雅的使用Loading

Android開發之更優雅的使用Loading

Loading 是很普遍的需求,比如請求的時候需要顯示 Loaing ,而一般的實現方式是在佈局xml裡新增一個 ProgressBar,但是這樣寫就有很多不便,每個頁面的 layout 都要寫一個 ProgressBar,顯示的位置也固定了,還耦合了很多程式碼。

而 LoadingBar 就是為了跟方便的操作 Loading 而生,高度解耦,樣式全部通過工廠類決定。

LoadingBar - 適合一些顯示資料的操作,比如請求列表,註冊,登入等

Factory - 決定了 loading 的樣式,自定義樣式只需實現 Factory

效果圖
預設進度條 顏色
幀動畫 圖片

1、準備工作
在 app 資料夾下 build.gradle 中引入,記得同步。

compile 'com.dyhdyh.loadingbar:loadingbar:1.4.4'

2、資原始檔
1)layout資料夾下的loading_process_dialog_color.xml

<?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:background="#50000000" android:gravity="center" android:orientation="horizontal">
<LinearLayout android:layout_width="match_parent" android:layout_height="100dp" android:gravity="center" android:background="@drawable/shap_progress_bg" android:layout_marginLeft
="50dp" android:layout_marginRight="50dp" android:orientation="horizontal">
<ProgressBar android:id="@+id/loading_process_dialog_progressBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:indeterminate="false" android:indeterminateDrawable="@drawable/dialog_style_xml_color" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:textSize="20sp" android:textColor="@android:color/white" android:text="正在登入..." /> </LinearLayout> </LinearLayout>

2)drawable資料夾下的dialog_style_xml_color.xml

<?xml version="1.0" encoding="utf-8"?>

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0"
    android:toDegrees="360">
    <shape android:shape="ring" android:innerRadiusRatio="3"
        android:thicknessRatio="8" android:useLevel="false">
        <gradient android:type="sweep" android:useLevel="false"
            android:startColor="#FFFFFF" android:centerColor="#FFDC35"
            android:centerY="0.50" android:endColor="#CE0000" />
    </shape>
</rotate>

3)drawable資料夾下的shap_progress_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <corners android:radius="12dp"/>
    <solid android:color="#88000000"/>
</shape>

3、邏輯程式碼
MainActivity.java

public class MainActivity extends AppCompatActivity {
    private View mParent;
    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mParent = findViewById(R.id.ll_container);
    }

    public void show(View view) {
        LoadingBar.make(mParent).show();

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                LoadingBar.cancel(mParent);
            }
        },2000);
    }

    public void showColor(View v) {
        CustomLoadingFactory factory = new CustomLoadingFactory();
        LoadingBar.make(mParent,factory).show();

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                LoadingBar.cancel(mParent);
            }
        },5000);
    }


}

CustomLoadingFactory.java

package com.gyq.loadingview;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dyhdyh.widget.loading.factory.LoadingFactory;

/**
 * Created by gyq on 2017/6/7 09:47
 */
public class CustomLoadingFactory implements LoadingFactory {
    @Override
    public View onCreateView(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_process_dialog_anim, parent, false);
        //View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_process_dialog_color, parent, false);
       // View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_process_dialog_icon, parent, false);

        return view;
    }
}