最近公司需要做一个APP,自己也是学习了一下,下面是做的网络加载的loading
话不多说,直接上代码
是用dialog写的
后台 LoadingDialog.java
public class LoadingDialog extends Dialog {
private ImageView iv_ing;
private AnimationSet animationSet;
private static LoadingDialog instance;
public static LoadingDialog getInstance(Context context) {
if(instance == null) {
instance = new LoadingDialog(context);
}
return instance;
}
private LoadingDialog(@NonNull Context context) {
super(context);
}
private LoadingDialog(@NonNull Context context, @StyleRes int themeResId) {
super(context, themeResId);
}
private LoadingDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//背景透明处理
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getWindow().setDimAmount(0f);
this.setContentView(R.layout.dialog_loading);
//设置dialog属性
setCancelable(true);
setCanceledOnTouchOutside(false);
iv_ing = findViewById(R.id.iv_ing);
//加载动画
loadIng();
}
@Override
protected void onStart() {
super.onStart();
iv_ing.startAnimation(animationSet);//开始播放
}
@Override
protected void onStop() {
super.onStop();
}
//加载动画
private void loadIng() {
animationSet = new AnimationSet(true);
RotateAnimation animation_rotate = new RotateAnimation(0, +359,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
animation_rotate.setRepeatCount(-1);
animation_rotate.setStartOffset(0);
animation_rotate.setDuration(1000);
LinearInterpolator lir = new LinearInterpolator();
animationSet.setInterpolator(lir);
animationSet.addAnimation(animation_rotate);
}
}
前端XML :dialog_loading.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:alpha="0.4"
android:background="#000000"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="20dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp">
<RelativeLayout
android:layout_width="200dp"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_ing"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:src="@drawable/loading" />
<TextView
android:paddingTop="5dp"
android:gravity="center"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:textColor="@color/teal_200"
android:text="正在加载中..."
android:layout_below="@+id/iv_ing"
android:layout_marginTop="20dp"/>
</RelativeLayout>
</LinearLayout >
调用方法
LoadingDialog.getInstance(this).show();//显示
LoadingDialog.getInstance(getApplicationContext()).dismiss();//隐藏
下面附上运行截图

本文介绍了如何在Android应用中使用Dialog实现一个定制化的加载动画,包括LoadingDialog的创建、XML布局和调用方法,展示了从代码到实际效果的完整过程。
9935





