zoukankan      html  css  js  c++  java
  • android 自定义加载动画控件

    一、效果

     二、创建资源

    1、创建弹窗的背景

     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="#FFFFFF"/>
    </shape>

    2、添加加载图片(阿里图标库

     3、创建一个弹窗的样式

    <!-- 自定义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_blue</item>
            <item name="android:windowIsFloating">true</item>
            <item name="android:windowContentOverlay">@null</item>
        </style>

    4、创建一个动画文件

    <?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>

    三、控件设计

    1、布局文件

    <?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>

    2、逻辑代码

    package com.me.myqmui;
    
    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.setTextSize(20);
            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();
        }
    }

    四、使用

  • 相关阅读:
    2021-06-10 Summary: 阶段总结
    java中有符号和无符号数据类型发生转换
    关于数组和集合的冒泡排序中容易出现的IndexOutOfBoundsException
    Intellij IDEA打开多项目窗口
    使用Idea从github上获取项目
    用Intellij Idea从Github上获取代码
    Python-列表常用操作方法
    Python-字符串常用操作方法
    Python-不可变对象和可变对象的理解
    Python-内置数据类型
  • 原文地址:https://www.cnblogs.com/20183544-wangzhengshuai/p/14948955.html
Copyright © 2011-2022 走看看