first
This commit is contained in:
115
app/src/main/java/com/ckkj/water/dialog/LoadingDialog.java
Normal file
115
app/src/main/java/com/ckkj/water/dialog/LoadingDialog.java
Normal file
@@ -0,0 +1,115 @@
|
||||
package com.ckkj.water.dialog;
|
||||
|
||||
import android.animation.ObjectAnimator;
|
||||
import android.animation.ValueAnimator;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.view.WindowManager;
|
||||
import android.view.animation.LinearInterpolator;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
|
||||
import com.ckkj.water.R;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
|
||||
/**
|
||||
* author:
|
||||
* time: 2019/9/18 9:34
|
||||
* desc:
|
||||
* version: 1.0.0
|
||||
*/
|
||||
public class LoadingDialog extends Dialog {
|
||||
private String string="";
|
||||
private TextView textView;
|
||||
private Context context;
|
||||
private String [] strLoadContent = {"加载中.","加载中..","加载中..."};
|
||||
private int mIndex = 0;
|
||||
private TextView tvLoading;
|
||||
private Handler handler = new Handler();
|
||||
|
||||
// 创建一个Runnable对象,用于定时执行任务
|
||||
Runnable runnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// 在这里编写需要定时执行的操作
|
||||
// 比如更新UI、发送网络请求等
|
||||
if(tvLoading != null){
|
||||
tvLoading.setText(strLoadContent[mIndex]);
|
||||
mIndex ++;
|
||||
if(mIndex == strLoadContent.length){
|
||||
mIndex = 0;
|
||||
}
|
||||
}
|
||||
handler.postDelayed(this, 300);
|
||||
}
|
||||
};
|
||||
|
||||
public LoadingDialog(Context context) {
|
||||
super(context); // 应用透明背景样式
|
||||
this.context=context;
|
||||
}
|
||||
public LoadingDialog(Context context, String s) {
|
||||
super(context);
|
||||
this.string=s;
|
||||
this.context=context;
|
||||
}
|
||||
|
||||
public LoadingDialog(Context context, int theme) {
|
||||
super(context, theme);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
init();
|
||||
}
|
||||
|
||||
@SuppressLint("ResourceAsColor")
|
||||
private void init() {
|
||||
// 设置对话框背景为透明
|
||||
getWindow().setBackgroundDrawable(context.getResources().getDrawable(R.color.translucent_black_95));
|
||||
getWindow().setDimAmount(0.2f);
|
||||
setCancelable(true);
|
||||
setCanceledOnTouchOutside(false);
|
||||
setContentView(R.layout.dialog_loading_my);//loading的xml文件
|
||||
tvLoading = findViewById(R.id.tv_load_dialog);
|
||||
ImageView imgLoading = findViewById(R.id.img_loading);
|
||||
// 初始化 ObjectAnimator
|
||||
ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(imgLoading, "rotation", 0f, 360f);
|
||||
rotateAnimator.setDuration(1500);
|
||||
rotateAnimator.setRepeatCount(ValueAnimator.INFINITE);
|
||||
rotateAnimator.setInterpolator(new LinearInterpolator()); // 匀速旋转
|
||||
rotateAnimator.start();
|
||||
//加载动画资源
|
||||
// Drawable animation = (AnimationDrawable) imgLoading.getDrawable();
|
||||
// ((AnimationDrawable) animation).start();
|
||||
WindowManager.LayoutParams params = Objects.requireNonNull(getWindow()).getAttributes();
|
||||
params.width = WindowManager.LayoutParams.MATCH_PARENT;
|
||||
params.height = WindowManager.LayoutParams.MATCH_PARENT;
|
||||
getWindow().setAttributes(params);
|
||||
// 可选: 设置窗口大小
|
||||
getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show() {//开启
|
||||
// if (context==null){
|
||||
// return;
|
||||
// }
|
||||
super.show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dismiss() {//关闭
|
||||
// if (context==null){
|
||||
// return;
|
||||
// }
|
||||
super.dismiss();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user