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;
}
}