自定义加载ProgressDialog

java代码

package com.example.administrator.newspolice.view;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.newspolice.R;

/**
 * @desc: 自定义提示Dialog
 */
public class CustomProgressDialog extends ProgressDialog {

    /**
     * 提示信息
     */
    private String loadingMsg;
    private boolean outDismiss;

    public CustomProgressDialog(Context context, String loadingMsg, boolean outDismiss) {
        super(context, R.style.Custom_Progress);
        this.loadingMsg = loadingMsg;
        this.outDismiss = outDismiss;
        setShowing();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_progress_layout);
        setCanceledOnTouchOutside(outDismiss);
        setBackGround();
        initView();
    }

    /**
     * 初始化
     *
     * @author:xiaoj
     * @createTime:2017年8月22日 上午11:36:50
     */
    private void initView() {

        TextView tvMsg = (TextView) findViewById(R.id.tv_dialog_msg);
        tvMsg.setText(loadingMsg);
        if (TextUtils.isEmpty(loadingMsg) || "".equals(loadingMsg.trim())) {
            tvMsg.setVisibility(View.GONE);
        }
        ImageView ivLoading = (ImageView) findViewById(R.id.iv_dialog_loading);
        // 获取ImageView上的动画背景
        AnimationDrawable animationDrawable = (AnimationDrawable) ivLoading.getBackground();
        animationDrawable.start();
    }

    /**
     * 设置背景
     *
     * @author:xiaoj
     * @createTime:2017年5月23日 上午10:22:37
     */
    private void setBackGround() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.dimAmount = 0.2f;
        getWindow().setAttributes(lp);
    }

    /**
     * 消失
     *
     * @author:xiaoj
     * @createTime:2017年5月23日 上午9:21:27
     */
    public void setDismiss() {
        this.dismiss();
    }

    /**
     * 显示
     *
     * @author:xiaoj
     * @createTime:2017年5月23日 上午9:21:40
     */
    public void setShowing() {
        if (isShowing()) {
            this.dismiss();
        }
        this.show();
    }
}

style属性

<!-- 自定义加载Dialog -->
<style name="Custom_Progress" parent="@android:style/Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/bg_custom_progress"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingBottom="20dp"
    android:paddingLeft="30dp"
    android:paddingRight="30dp"
    android:paddingTop="20dp" >

    <ImageView
        android:id="@+id/iv_dialog_loading"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="@drawable/bg_loading_anim" />

    <TextView
        android:id="@+id/tv_dialog_msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textColor="@color/silver" />

</LinearLayout>
bg_loading_anim.xml
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false" >

    <item
        android:drawable="@mipmap/ic_loading_0"
        android:duration="60"/>
    <item
        android:drawable="@mipmap/ic_loading_1"
        android:duration="60"/>
    <item
        android:drawable="@mipmap/ic_loading_2"
        android:duration="60"/>
    <item
        android:drawable="@mipmap/ic_loading_3"
        android:duration="60"/>
    <item
        android:drawable="@mipmap/ic_loading_4"
        android:duration="60"/>
    <item
        android:drawable="@mipmap/ic_loading_5"
        android:duration="60"/>
    <item
        android:drawable="@mipmap/ic_loading_6"
        android:duration="60"/>
    <item
        android:drawable="@mipmap/ic_loading_7"
        android:duration="60"/>
    <item
        android:drawable="@mipmap/ic_loading_8"
        android:duration="60"/>
    <item
        android:drawable="@mipmap/ic_loading_9"
        android:duration="60"/>
    <item
        android:drawable="@mipmap/ic_loading_10"
        android:duration="60"/>
    <item
        android:drawable="@mipmap/ic_loading_11"
        android:duration="60"/>
</animation-list>
bg_custom_progress.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#ff404040" />
    <corners
        android:bottomLeftRadius="8dp"
        android:bottomRightRadius="8dp"
        android:topLeftRadius="8dp"
        android:topRightRadius="8dp" />

</shape>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想要在 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值