Android 自定义加载等待动画

在项目开发中,网络请求是需要的加载动画是必备的.下面来详解介绍如何完成自定义加载动画效果.

首先在drawable文件夹下新建 frame_loading.xml文件, 设置图片每隔0.1s切换, 并循环播放.

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">

    <item
        android:drawable="@mipmap/loading01"
        android:duration="100" />
    <item
        android:drawable="@mipmap/loading02"
        android:duration="100" />
    <item
        android:drawable="@mipmap/loading03"
        android:duration="100" />
    <item
        android:drawable="@mipmap/loading04"
        android:duration="100" />
    <item
        android:drawable="@mipmap/loading05"
        android:duration="100" />
    <item
        android:drawable="@mipmap/loading06"
        android:duration="100" />
    <item
        android:drawable="@mipmap/loading07"
        android:duration="100" />
    <item
        android:drawable="@mipmap/loading08"
        android:duration="100" />
    <item
        android:drawable="@mipmap/loading09"
        android:duration="100" />
    <item
        android:drawable="@mipmap/loading10"
        android:duration="100" />

</animation-list>

drawable文件夹下新建dialog_bg.xml文件, 显示半透明背景边框

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

layout文件夹下新建loading_view.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/dialog_bg"
    android:orientation="vertical"
    >

    <ImageView
        android:id="@+id/loadingIv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="30dp"
        android:background="@drawable/frame_loading"/>

</RelativeLayout>

下面开始写Dialog代码

package com.ls.loadingviewdemo;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;

/**
 * Created by Ls on 2017/2/28.
 */

public class LoadingDialog extends ProgressDialog {

    private Context mContext;

    // animationlist的 id
    private int mResid;
    private ImageView mImageView;

    private AnimationDrawable animationDrawable;


    public LoadingDialog(Context context) {
        super(context);
    }

    /**
     * @param context 上下文对象
     * @param id      动画的id
     */
    public LoadingDialog(Context context, int id) {
        super(context, R.style.dialog);
        this.mContext = context;
        this.mResid = id;
        //点击窗口外边界之外,是否消失  true为消失
        setCanceledOnTouchOutside(false);
    }

    // 设置在窗口的边界之外时,该对话框是否被取消(true-消失)
    public void setOnOutSide(boolean state) {
        setCanceledOnTouchOutside(state);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化页面
        initView();
        //初始化数据
        initData();
    }

    //初始化页面
    private void initData() {

        mImageView.setImageResource(mResid);

        animationDrawable = (AnimationDrawable) mImageView.getBackground();

        mImageView.post(new Runnable() {
            @Override
            public void run() {
                animationDrawable.start();
            }
        });

    }

    // 初始化dialog显示内容
    private void initView() {
        //显示的界面
        setContentView(R.layout.loading_view);
        //显示的图片
        mImageView = (ImageView) findViewById(R.id.loadingIv);
    }

}

在需要显示的地方调用

LoadingDialog  dialog = new LoadingDialog(this, R.drawable.frame_loading);
dialog.setOnOutSide(true);
dialog.show();

这样就完成了加载动画~~~

这里写图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
如果你想要在 Android 应用中使用自定义加载动画,可以通过创建自定义的布局和使用 Animation 类来实现。以下是一个简单的示例,演示如何在 Android 应用中自定义加载动画: 首先,在 res/layout 目录下创建一个新的布局文件,例如 custom_loading.xml,其中包含自定义加载动画布局,例如: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center"> <ImageView android:id="@+id/loading_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/custom_loading_animation" android:layout_marginBottom="16dp"/> <TextView android:id="@+id/loading_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Loading..." android:textColor="@android:color/white"/> </LinearLayout> ``` 其中,loading_image 是一个自定义加载动画的 ImageView,loading_text 是一个文本视图,用于显示加载消息。 接下来,在 res/drawable 目录下创建一个新的动画文件,例如 custom_loading_animation.xml,其中包含自定义加载动画动画,例如: ```xml <?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/custom_loading_frame1" android:duration="100" /> <item android:drawable="@drawable/custom_loading_frame2" android:duration="100" /> <item android:drawable="@drawable/custom_loading_frame3" android:duration="100" /> <item android:drawable="@drawable/custom_loading_frame4" android:duration="100" /> </animation-list> ``` 其中,custom_loading_frame1、custom_loading_frame2、custom_loading_frame3、custom_loading_frame4 是自定义加载动画的帧。 最后,在你的 Activity 中,使用 LayoutInflater 类将 custom_loading.xml 布局文件实例化为 View 对象,并使用 Animation 类将 custom_loading_animation.xml 动画文件加载到 ImageView 中,例如: ```java LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.custom_loading, null); ImageView imageView = (ImageView) view.findViewById(R.id.loading_image); Animation animation = AnimationUtils.loadAnimation(this, R.drawable.custom_loading_animation); imageView.startAnimation(animation); ProgressDialog progressDialog = new ProgressDialog(this); progressDialog.setCancelable(false); progressDialog.show(); progressDialog.setContentView(view); ``` 其中,使用 LayoutInflater 类将 custom_loading.xml 布局文件实例化为 View 对象,并使用 findViewById() 方法获取 loading_image ImageView 对象。然后,使用 AnimationUtils.loadAnimation() 方法将 custom_loading_animation.xml 动画文件加载到 ImageView 中,并调用 startAnimation() 方法开始播放动画。最后,将 View 对象设置为 ProgressDialog 的内容视图,调用 show() 方法显示 ProgressDialog。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值