Android自定义的仿ios的loading弹窗(二)
2019-04-18 本文已影响345人
马木木_1991
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://img.haomeiwen.com/i14906070/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,对应的布局文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_loading_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="90dp"
android:background="@drawable/loading_bg"
android:gravity="center"
android:orientation="vertical"
android:minWidth="100dp"
android:paddingLeft="15dp"
android:paddingTop="10dp"
android:paddingRight="15dp"
android:paddingBottom="10dp">
<ProgressBar
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center_horizontal"
android:indeterminateBehavior="repeat"
android:indeterminateDrawable="@drawable/dialog_loading"
android:indeterminateOnly="true" />
<TextView
android:id="@+id/tipTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="加载中..."
android:textColor="#f0f0f0"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
3,对应的style样式。这注意:backgroundDimAmount:调节蒙层的透明度0-1。
<resources>
<!--loading样式-->
<!--backgroundDimAmount:调节蒙层的透明度0-1-->
<style name="MyDialogStyle">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:backgroundDimAmount">0.6</item>
</style>
</resources>
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
如果有发现错误欢迎指正我及时修改,如果有好的建议欢迎留言。如果觉得对你有帮助欢迎给小星星,谢谢。