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

所属分类: 软件编程 / Android 阅读数: 72
收藏 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 动画之AlphaAnimation应用详解

本节讲解AlphaAnimation 动画,窗口的动画效果,淡入淡出什么的,有些游戏的欢迎动画,logo的淡入淡出效果就使用AlphaAnimation,具体的祥看本文,需要的朋友可以参考下
收藏 0 赞 0 分享

Android 动画之TranslateAnimation应用详解

本节讲解TranslateAnimation动画,TranslateAnimation比较常用,比如QQ,网易新闻菜单条的动画,就可以用TranslateAnimation实现,本文将详细介绍通过TranslateAnimation 来定义动画,需要的朋友可以参考下
收藏 0 赞 0 分享

Android 动画之ScaleAnimation应用详解

本节讲解ScaleAnimation 动画在应用中的实现,有需要的朋友可以参考下
收藏 0 赞 0 分享

Android 动画之RotateAnimation应用详解

本节讲解旋转动画效果RotateAnimation方法的应用,有需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之文件操作模式深入理解

本文将介绍Android开发之文件操作模式,需要了解的朋友可以参考下
收藏 0 赞 0 分享

Android应用程序窗口(Activity)窗口对象(Window)创建指南

本文将详细介绍Android应用程序窗口(Activity)的窗口对象(Window)的创建过程,需要了解的朋友可以参考下
收藏 0 赞 0 分享

android activity设置无标题实现全屏

本文将详细介绍Android如何设置Activity全屏和无标题的实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android启动模拟器报错解决方法

本文将详细介绍Android模拟器报"Failed To Allocate memory 8"错误的解决办法,需要了解的朋友可以参考下
收藏 0 赞 0 分享

Android如何实现非本地图片的点击态

Android如何实现非本地图片的点击态,本文提供了详细的实现代码,需要了解的朋友可以参考下
收藏 0 赞 0 分享

android viewpaper实例探讨

本文将提供一个android viewpaper实例实现过程,需要了解更多的朋友可以参考下
收藏 0 赞 0 分享
查看更多