laoding是每个app必备的。一般公司都会自定义。使用自己公司的吉祥物来展示。今天来写一个仿ios效果的loading。下一期会写一个自定义的toast。然后和前面写的弹窗一起组合成一个公共的依赖库。
说明:
一,使用的Androidstudio版本为3.3.2
二,使用说明。直接增加依赖。然后看本文的第四大条即可。
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.mamumu:mmLoading:1.1'
}
展示效果:
https://upload-images.jianshu.io/upload_images/14906070-756f8ad844a3516f.gif?imageMogr2/auto-orient/strip
现在正式开始
1,核心activity。
package com.mumu.loading;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
/**
* @author : zlf
* date : 2019/4/17
* github : https://github.com/mamumu
* blog : https://www.jianshu.com/u/281e9668a5a6
*/
public class MMLoading extends Dialog {
public MMLoading(Context context) {
super(context);
}
public MMLoading(Context context, int themeResId) {
super(context, themeResId);
}
public static class Builder {
private Context context;
private String message;
private boolean isShowMessage = true;
private boolean isCancelable = false;
private boolean isCancelOutside = false;
public Builder(Context context) {
this.context = context;
}
/**
* 设置提示信息
* @param message
* @return
*/
public Builder setMessage(String message) {
this.message = message;
return this;
}
/**
* 设置是否显示提示信息
* @param isShowMessage
* @return
*/
public Builder setShowMessage(boolean isShowMessage) {
this.isShowMessage = isShowMessage;
return this;
}
/**
* 设置是否可以按返回键取消
*
* @param isCancelable
* @return
*/
public Builder setCancelable(boolean isCancelable) {
this.isCancelable = isCancelable;
return this;
}
/**
* 设置是否可以点击外部取消
* @param isCancelOutside
* @return
*/
public Builder setCancelOutside(boolean isCancelOutside) {
this.isCancelOutside = isCancelOutside;
return this;
}
public MMLoading create() {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.dialog_loading, null);
MMLoading mmLoading = new MMLoading(context, R.style.MyDialogStyle);
TextView msgText = (TextView) view.findViewById(R.id.tipTextView);
if (isShowMessage) {
msgText.setText(message);
} else {
msgText.setVisibility(View.GONE);
}
mmLoading.setContentView(view);
mmLoading.setCancelable(isCancelable);
mmLoading.setCanceledOnTouchOutside(isCancelOutside);
//实现loading的透明度
// WindowManager.LayoutParams lp=mmLoading.getWindow().getAttributes();
// lp.alpha = 0.6f;
// mmLoading.getWindow().setAttributes(lp);
return mmLoading;
}
}
}
2,对应的布局文件。
3,对应的style样式。这注意:backgroundDimAmount:调节蒙层的透明度0-1。
4,如何使用。一,直接在自己的BaseActivity中增加showLoading()和hideLoading()方法。如下第一段代码所示。二,MainActivity继承BaseActivity,直接使用对应的方法。
package com.mumu.mmloading.base;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;
import com.mumu.loading.MMLoading;
/**
* @author : zlf
* date : 2019/4/17
* github : https://github.com/mamumu
* blog : https://www.jianshu.com/u/281e9668a5a6
*/
public class BaseActivity extends AppCompatActivity {
private MMLoading mmLoading;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
}
protected void showLoading() {
if (mmLoading == null) {
MMLoading.Builder builder = new MMLoading.Builder(this)
.setMessage("加载中...")
.setCancelable(false)
.setCancelOutside(true);
mmLoading = builder.create();
}else {
mmLoading.dismiss();
MMLoading.Builder builder = new MMLoading.Builder(this)
.setMessage("加载中...")
.setCancelable(false)
.setCancelOutside(true);
mmLoading = builder.create();
}
mmLoading.show();
}
protected void showLoading(String msg) {
if (mmLoading == null) {
MMLoading.Builder builder = new MMLoading.Builder(this)
.setMessage(msg)
.setCancelable(false)
.setCancelOutside(true);
mmLoading = builder.create();
}else {
mmLoading.dismiss();
MMLoading.Builder builder = new MMLoading.Builder(this)
.setMessage(msg)
.setCancelable(false)
.setCancelOutside(false);
mmLoading = builder.create();
}
mmLoading.show();
}
protected void hideLoading() {
if (mmLoading != null && mmLoading.isShowing()) {
mmLoading.dismiss();
}
}
}
package com.mumu.mmloading;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import com.mumu.mmloading.base.BaseActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends BaseActivity {
@BindView(R.id.button1)
Button button1;
@BindView(R.id.button2)
Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initView();
}
private void initView() {
}
@OnClick({R.id.button1, R.id.button2})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.button1:
showLoading();
//延迟3秒关闭
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
hideLoading();
}
},3000);
break;
case R.id.button2:
showLoading("正在校验信息");
//延迟3秒关闭
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
hideLoading();
}
},5000);
break;
}
}
}
5,对应github地址
demo地址:https://github.com/mamumu/mmLoading
6,我后面将dialog(一),loading(二)和toast(三)做了统一封装链接如下,推荐使用:
https://www.jianshu.com/p/9259ad7f857b
如果有发现错误欢迎指正我及时修改,如果有好的建议欢迎留言。如果觉得对你有帮助欢迎给小星星,谢谢。