Android 自定义加载动画Dialog弹窗

效果图

在这里插入图片描述

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

首先是创建弹窗的背景
在这里插入图片描述
这是上面用到的
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

Java版本

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;

/**
 * 自定义弹窗 - Java
 */
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();
    }
}

Kotlin版本

package com.llw.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

/**
 * 自定义弹窗 - Kotlin
 */
class CustomDialog protected constructor(context: Context, theme: Int, string: String) :
    Dialog(context, theme) {

    var tvLoadingTx: TextView
    var ivLoading: ImageView

    constructor(context: Context) : this(context, R.style.loading_dialog, "玩命加载中...") {

    }

    constructor(context: Context, string: String) : this(context, R.style.loading_dialog, string) {}

    init {
        setCanceledOnTouchOutside(true)//点击其他区域时   true  关闭弹窗  false  不关闭弹窗
        setOnCancelListener { dismiss() }
        setContentView(R.layout.loading_dialog)
        tvLoadingTx = findViewById(R.id.tv_loading_tx)
        tvLoadingTx.text = string
        ivLoading = findViewById(R.id.iv_loading)
        // 加载动画
        val hyperspaceJumpAnimation = AnimationUtils.loadAnimation(
            context, R.anim.loading_animation
        )
        // 使用ImageView显示动画
        ivLoading.startAnimation(hyperspaceJumpAnimation)

        window!!.attributes.gravity = Gravity.CENTER//居中显示
        window!!.attributes.dimAmount = 0.5f//背景透明度  取值范围 0 ~ 1
    }


    override fun dismiss() {
        super.dismiss()
    }


}

使用 java
在这里插入图片描述
使用Kotlin
在这里插入图片描述

这应该能看懂吧,写完收工。
源码-Java

源码-Kotlin

  • 3
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 16
    评论
要在 Android加载弹窗,可以使用对话框(Dialog)或者弹出窗口(PopupWindow)。 使用对话框: 1. 创建一个AlertDialog.Builder对象: AlertDialog.Builder builder = new AlertDialog.Builder(this); 2. 设置对话框的标题、消息和按钮: builder.setTitle("标题") .setMessage("消息") .setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // 用户点击确定按钮后的操作 } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // 用户点击取消按钮后的操作 } }); 3. 创建并显示对话框: AlertDialog dialog = builder.create(); dialog.show(); 使用弹出窗口: 1. 创建一个PopupWindow对象: PopupWindow popupWindow = new PopupWindow(this); 2. 设置弹窗的布局和大小: View contentView = LayoutInflater.from(this).inflate(R.layout.popup_layout, null); popupWindow.setContentView(contentView); popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); 3. 设置弹窗的背景色和动画效果: popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); popupWindow.setAnimationStyle(R.style.popup_anim); 4. 显示弹窗: View anchorView = findViewById(R.id.anchor_view); // 弹窗要显示在哪个视图的下方 int xOff = 0; // 弹窗的横向偏移量 int yOff = 10; // 弹窗的纵向偏移量 popupWindow.showAsDropDown(anchorView, xOff, yOff);

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

初学者-Study

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值