AvLoadingIndicatorView 的 封装

简介: AvLoadingIndicatorView 的 封装

AVLoadingIndicatorView 是非常炫酷的加载动画,使用起来也非常方法。今天对他进行一个封装。


1,添加依赖


//Loading 依赖
api 'com.wang.avi:library:2.1.3'


2, 设置一些加载的样式,这些在 github 上面都可以找到


public enum  LoaderStyle {
    /**
     *  加载样式
     */
    BallPulseIndicator,
    BallGridPulseIndicator,
    BallClipRotateIndicator,
    BallClipRotatePulseIndicator,
    SquareSpinIndicator,
    BallClipRotateMultipleIndicator,
    BallPulseRiseIndicator,
    BallRotateIndicator,
    CubeTransitionIndicator,
    BallZigZagIndicator,
    BallZigZagDeflectIndicator,
    BallTrianglePathIndicator,
    BallScaleIndicator,
    LineScaleIndicator,
    LineScalePartyIndicator,
    BallPulseSyncIndicator,
    BallBeatIndicator,
    LineScalePulseOutIndicator,
    LineScalePulseOutRapidIndicator,
    BallScaleRippleIndicator,
    BallScaleRippleMultipleIndicator,
    BallSpinFadeLoaderIndicator,
    LineSpinFadeLoaderIndicator,
    TriangleSkewSpinIndicator,
    PacmanIndicator,
    BallGridBeatIndicator,
    SemiCircleSpinIndicator
}


3, 通过反射获取到 需要使用 加载动画


public class LoaderCreator {
    //进行缓存
    private static final WeakHashMap<String,Indicator> LOADING_MAP = new WeakHashMap<>();
    static AVLoadingIndicatorView creawte(String type, Context context){
        final AVLoadingIndicatorView avLoadingIndicatorView = new AVLoadingIndicatorView(context);
        if (LOADING_MAP.get(type) == null){
            //根据传入的 type 进行反射 获取动画 
            final Indicator indicator = getIndicator(type);
            LOADING_MAP.put(type,indicator);
        }
        //设置 这个动画
        avLoadingIndicatorView.setIndicator(LOADING_MAP.get(type));
        return avLoadingIndicatorView;
    }
    private static Indicator getIndicator(String name){
        if (name == null || name.isEmpty()){
            return null;
        }
        final StringBuilder drawbleClassName = new StringBuilder();
        //如果类名不包含 . 就说明传入的是一个 整个的类名,否则就是包名
        if (!name.contains(".")){
            // 拼装一个完整的路径,这个路径就是 动画类所在的位置。
            final String defultPackageName = AVLoadingIndicatorView.class.getPackage().getName();
            drawbleClassName.append(defultPackageName)
                    .append(".indicators")
                    .append(".");
        }
        drawbleClassName.append(name);
        try {
            //通过反射 拿到给定的类
            final Class<?> drawableClass = Class.forName(drawbleClassName.toString());
            //创建 该对象并返回
            return (Indicator) drawableClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}


4,设置样式。我们要将loading 显示在 dialog 上面,在style.xml 中添加如下代码:


<resources>
    <style name="dialog" parent="@android:style/Theme.Dialog">
        <!--去掉边框-->
        <item name="android:windowFrame">@null</item>
        <!--悬浮-->
        <item name="android:windowIsFloating">true</item>
        <!--半透明-->
        <item name="android:windowIsTranslucent">false</item>
        <!--不需要标题-->
        <item name="android:windowNoTitle">true</item>
        <!--背景透明-->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!--允许模糊-->
        <item name="android:backgroundDimEnabled">true</item>
        <!--全屏幕-->
        <item name="android:windowFullscreen">true</item>
    </style>
</resources>


5,创建 一个测量的类。用于适应屏幕


public class DimenUtil {
    /**
     * @return 可用显示大小的绝对宽度(以像素为单位)
     */
    public static int getScreenWidth(){
        //这里的 getAppContext 是获取Context
        final Resources resources = Coke.getAppContext().getResources();
        final DisplayMetrics dm = resources.getDisplayMetrics();
        return dm.widthPixels;
    }
    /**
     * @return 可用显示大小的绝对高度(以像素为单位)
     */
    public static int getScreenHeight(){
        final Resources resources = Coke.getAppContext().getResources();
        final DisplayMetrics dm = resources.getDisplayMetrics();
        return dm.heightPixels;
    }
}


5, 最后一步,设置 AVLoading


public class CokeLoading {
    //缩放比,让load根据屏幕的大小 来调整大小。
    private static final int LOADER_SIZE_SCALE = 8;
    //屏幕的偏移量
    private static final int LOADER_OFFSET_SCALT = 10;
    private static final ArrayList<AppCompatDialog> LOADERS = new ArrayList<>();
    private static final String DEFULT_LOADER = LoaderStyle.BallClipRotatePulseIndicator.name();
    public static void showLoading(Context context, Enum<LoaderStyle> type) {
        showLoading(context, type.name());
    }
    public static void showLoading(Context context, String type) {
        // 使用 dialog 来承载 Loading .
        // 尽量 使用 v7包下的东西,这样兼容性比较好
        final AppCompatDialog dialog = new AppCompatDialog(context, R.style.dialog);
        //通过creawte方法 设置 样式 并返回对象
        final AVLoadingIndicatorView avLoadingIndicatorView = LoaderCreator.creawte(type, context);
        dialog.setContentView(avLoadingIndicatorView);
        int deviceWidth = DimenUtil.getScreenWidth();
        int deviceHeight = DimenUtil.getScreenHeight();
        final Window dialogWindow = dialog.getWindow();
        if (dialogWindow != null) {
            //设置 dialog 的属性
            final WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.alpha = 0.4f;
            lp.width = deviceWidth / LOADER_SIZE_SCALE;
            lp.height = deviceHeight / LOADER_SIZE_SCALE;
            //偏移量,会将上面的 height 个给覆盖掉
            lp.height = lp.height + deviceHeight / LOADER_OFFSET_SCALT;
            lp.gravity = Gravity.CENTER;
        }
        LOADERS.add(dialog);
        dialog.show();
    }
    public static void showLoading(Context context) {
         //自定义 一个默认的 加载动画
        showLoading(context, DEFULT_LOADER);
    }
    public static void stopLoading() {
        for (AppCompatDialog dialog : LOADERS) {
            if (dialog != null) {
                if (dialog.isShowing()) {
                    //  dismiss() 只是单纯的消失掉 dialog 而 cancel 会有一些回调,所以使用cancel
                    dialog.cancel();
                }
            }
        }
    }
}


7,使用如下:


//使用 指定的动画
CokeLoading.showLoading(getContext(),LoaderStyle.SemiCircleSpinIndicator);
//使用默认的
CokeLoading.showLoading(getContext());
//关闭如下
 CokeLoading.stopLoading();


效果如下:


0a2653c851af460fa595bd959398a8f1.png

2d65d23f6d4748949b924e4057485923.png


到这里就已经封装完成了。是不是使用起来非常 方便呢。赶紧试一试吧


相关文章
|
移动开发 前端开发 JavaScript
分享45个Bootstrap模板,总有一款适合您
分享45个Bootstrap模板,总有一款适合您
598 5
|
移动开发 前端开发 小程序
分享46个Bootstrap模板,总有一款适合您
分享46个Bootstrap模板,总有一款适合您
891 1
|
Android开发
IDEA编译gradle提示This version of the Android Support plugin for IntelliJ IDEA (or Android Studio) cannot open this project, please retry with version 2020.3.1 or newer.
IDEA编译gradle提示This version of the Android Support plugin for IntelliJ IDEA (or Android Studio) cannot open this project, please retry with version 2020.3.1 or newer.
1769 1
|
移动开发 JavaScript 开发工具
uniapp接入app
uniapp接入app
1653 156
|
5月前
|
人工智能 IDE API
阿里云Qoder对接使用完全指南:从安装配置到Agentic编码实战
本文提供了一份完整的阿里云Qoder对接使用指南。Qoder是阿里云推出的Agentic编码平台,支持桌面IDE、命令行CLI和JetBrains插件三种接入方式,可通过按量付费、Coding Plan或Token Plan团队版接入阿里云百炼大模型。文章系统讲解了Qoder的产品家族与核心能力、三种接入方式的安装配置与模型接入凭证设置、Editor与Quest双模式切换与使用、Cloud Agents的API对接方案(含PAT令牌获取、环境创建、Agent定义、Session管理与SSE事件流接收及完整curl命令示例),以及MCP服务扩展、Skills自定义技能、企业版配置等高级特性。最后
|
6月前
|
安全 Java API
Java在Android应用开发中的持久生命力(除Kotlin之外)
虽然Google在2017年宣布Kotlin成为Android官方开发语言,但Java从未退出舞台。现有数十万个Android应用由Java编写,包括许多大型应用(如Twitter、Spotify早期版本)
614 0
|
缓存 Ubuntu Java
‘settings.xml’ has syntax errors less… 和Parent ‘org.springframework.boot’has problems less…的问题解决
‘settings.xml’ has syntax errors less… 和Parent ‘org.springframework.boot’has problems less…的问题解决
1013 0
‘settings.xml’ has syntax errors less… 和Parent ‘org.springframework.boot’has problems less…的问题解决
|
SQL 数据可视化 Java
JimuReport 积木报表 v1.9.5 发布,免费的可视化报表
JimuReport是一款免费的数据可视化报表工具,支持复杂报表、打印设计、图表报表、门户设计和大屏展示等功能。其Web版设计器操作简单,类似Excel风格,通过拖拽即可完成设计,支持SQL与API数据源绑定,具备表达式计算和合计功能。此外,还支持ECharts 28种图表、分组交叉报表、打印设计以及炫酷大屏制作,满足各类企业需求。重要的是,它永久免费,傻瓜式操作,是DataV和帆软的优秀开源替代方案。
1383 0
|
SQL 安全 数据库
Jetpack Room 从入门到精通
Jetpack Room 是 Android 官方推荐的持久化库,基于 SQLite 提供类型安全、编译时验证的数据库操作。通过 @Entity、@Dao、@Database 三大组件,简化增删改查,支持协程、LiveData、Flow,实现响应式数据访问,助力构建高效、稳定的本地数据存储方案。
669 0
|
Java Linux Maven
Maven下载与安装详细教程
Maven下载与安装详细教程
1303 0