Android 自定义加载动画Dialog弹窗效果的示例代码

所属分类: 软件编程 / Android 阅读数: 85
收藏 0 赞 0 分享

效果图

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

首先是创建弹窗的背景

在这里插入图片描述

这是上面用到的
shape_bg_5_blue.xml为例,其他的三个无非就是里面的颜色不一样而已

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
 <corners android:radius="5dp"/>
 <solid android:color="#1C285B"/>
</shape>

然后是图片

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

因为有一个是白色的所以你看不见,但是依然可以保存到你本地文件夹下。

然后就是创建一个弹窗的样式

在这里插入图片描述

<!-- 自定义loading dialog -->
 <style name="loading_dialog" parent="android:style/Theme.Dialog">
 <item name="android:windowFrame">@null</item>
 <item name="android:windowNoTitle">true</item>
 <item name="android:windowBackground">@drawable/shape_bg_5_yellow</item>
 <item name="android:windowIsFloating">true</item>
 <item name="android:windowContentOverlay">@null</item>
 </style>

通过这个android:windowBackground的值改变不同的弹窗背景。
然后就是一个动画文件

在这里插入图片描述

这个文件一定要放在anim文件夹下(PS:什么?你说你没有这个文件夹?没有你就创建一个啊,我的天!)
loading_animation.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android">
 <rotate
 android:interpolator="@android:anim/linear_interpolator"
 android:pivotX="50%"
 android:pivotY="50%"
 android:fromDegrees="0"
 android:toDegrees="+360"
 android:duration="1500"
 android:startOffset="-1"
 android:repeatMode="restart"
 android:repeatCount="-1"/>
</set>

下面就要创建一个现实内容的布局

在这里插入图片描述

布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/dialog_view"
 android:orientation="vertical"
 android:layout_width="120dp"
 android:layout_height="120dp"
 android:gravity="center"
 android:padding="10dp">

 <ImageView
 android:id="@+id/iv_loading"
 android:layout_width="40dp"
 android:layout_height="40dp"
 android:src="@mipmap/icon_loading_5" />

 <TextView
 android:id="@+id/tv_loading_tx"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginTop="10dp"
 android:maxLines="1"
 android:text="玩命加载中..."
 android:textColor="#FFF"
 android:textSize="14sp" />
</LinearLayout>

接下来就是自定义Dialog

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 自定义弹窗
 */
public class CustomDialog extends Dialog {

 TextView tvLoadingTx;
 ImageView ivLoading;

 public CustomDialog(Context context) {
 this(context, R.style.loading_dialog, "玩命加载中...");

 }

 public CustomDialog(Context context, String string) {
 this(context, R.style.loading_dialog, string);
 }

 protected CustomDialog(Context context, int theme, String string) {
 super(context, theme);
 setCanceledOnTouchOutside(true);//点击其他区域时 true 关闭弹窗 false 不关闭弹窗
 setContentView(R.layout.loading_dialog);//加载布局
 tvLoadingTx = findViewById(R.id.tv_loading_tx);
 tvLoadingTx.setText(string);
 ivLoading = findViewById(R.id.iv_loading);
 // 加载动画
 Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(
  context, R.anim.loading_animation);
 // 使用ImageView显示动画
 ivLoading.startAnimation(hyperspaceJumpAnimation);

 getWindow().getAttributes().gravity = Gravity.CENTER;//居中显示
 getWindow().getAttributes().dimAmount = 0.5f;//背景透明度 取值范围 0 ~ 1
 }

	//关闭弹窗
 @Override
 public void dismiss() {
 super.dismiss();
 }

使用

在这里插入图片描述

这应该能看懂吧,写完收工。

更多精彩内容其他人还在看

Android框架学习之Volley和Glide详解

这篇文章主要给大家介绍了关于Android框架学习之Volley和Glide的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android中Fragment的基本用法示例总结

Fragment是activity的界面中的一部分或一种行为,下面这篇文章主要给大家介绍了关于Android中Fragment的基本用法的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
收藏 0 赞 0 分享

Android.mk引入第三方jar包和so库文件的方法

这篇文章主要介绍了Android.mk引入第三方jar包和so库文件的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android仿微信录制小视频

这篇文章主要为大家详细介绍了Android仿微信录制小视频,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

android实现一键锁屏和一键卸载的方法实例

这篇文章主要给大家介绍了关于android如何实现一键锁屏和一键卸载的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
收藏 0 赞 0 分享

Android手势密码--设置和校验功能的实现代码

这篇文章主要介绍了Android手势密码--设置和校验功能的实现代码,非常不错,具有一定的参考校验价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Kotlin学习笔记之const val与val

这篇文章主要给大家介绍了关于Kotlin学习笔记之const val与val的相关资料,并给大家介绍了const val和val区别以及Kotlin中var和val的区别,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android实现调用系统分享功能示例的总结

这篇文章主要介绍了通过Android调用系统分享文本信息、单张图片、多个文件和指定分享到微信、QQ,同时分享图片和文字的功能示例,小编觉得挺不错,一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android自定义view实现输入控件

这篇文章主要为大家详细介绍了Android自定义view实现输入控件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android使用Intent.ACTION_SEND分享图片和文字内容的示例代码

这篇文章主要介绍了Android使用Intent.ACTION_SEND分享图片和文字内容的示例代码的实例代码,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多