欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android 实现加载动画

程序员文章站 2024-03-24 15:34:52
...

首先在drawable下建一个anim_loading.xml,代码如下:

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

    <item android:duration="130" android:drawable="@drawable/image_loading_01"></item>
    <item android:duration="130" android:drawable="@drawable/image_loading_02"></item>
    <item android:duration="130" android:drawable="@drawable/image_loading_03"></item>
    <item android:duration="130" android:drawable="@drawable/image_loading_04"></item>
    <item android:duration="130" android:drawable="@drawable/image_loading_05"></item>
    <item android:duration="130" android:drawable="@drawable/image_loading_06"></item>

</animation-list>

在layout文件下建一个layout_dialog.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/aa_dialog_bg"
    android:gravity="center"
    android:padding="10dp"
    android:orientation="vertical">

    <ProgressBar
        android:id="@+id/loading_progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5.0dip"
        android:indeterminateDrawable="@drawable/animation_loading" />

    <TextView
        android:id="@+id/tv_reminder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:text="加载中..."
        android:textColor="@color/colorTextGray" />

</LinearLayout>

实现dialog的调用:

package youhua.com.mytestapp;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


/**
 * description:弹窗浮动加载进度条
 */
public class LoadingDialog {
    /** 加载数据对话框 */
    private static Dialog mLoadingDialog;
    /**
     * 显示加载对话框
     * @param context 上下文
     * @param msg 对话框显示内容
     * @param cancelable 对话框是否可以取消
     */
    public static Dialog showDialogForLoading(Activity context, String msg, boolean cancelable) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_dialog, null);
        TextView loadingText = (TextView)view.findViewById(R.id.tv_reminder);
        loadingText.setText(msg);

        mLoadingDialog = new Dialog(context, R.style.CustomProgressDialog);
        mLoadingDialog.setCancelable(cancelable);
        mLoadingDialog.setCanceledOnTouchOutside(false);
        mLoadingDialog.setContentView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        mLoadingDialog.show();
        return  mLoadingDialog;
    }

    public static Dialog showDialogForLoading(Activity context) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_dialog, null);
        TextView loadingText = (TextView)view.findViewById(R.id.tv_reminder);
        loadingText.setText("加载中...");

        mLoadingDialog = new Dialog(context, R.style.CustomProgressDialog);
        mLoadingDialog.setCancelable(true);
        mLoadingDialog.setCanceledOnTouchOutside(false);
        mLoadingDialog.setContentView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        mLoadingDialog.show();
        return  mLoadingDialog;
    }

    /**
     * 关闭加载对话框
     */
    public static void cancelDialogForLoading() {
        if(mLoadingDialog != null) {
            mLoadingDialog.cancel();
        }
    }
}

最后在Activity中调用:

package youhua.com.mytestapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

/**
 * developer: $(wei)
 * date:created by 2018/9/4  17:32
 * email:[email protected]
 */
public class MainActivity extends AppCompatActivity {
   private TextView btn_loading;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_loading = findViewById(R.id.btn_loading);
        btn_loading.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoadingDialog.showDialogForLoading(MainActivity.this);
            }
        });

    }

}

备注说明:图片换成美工切的幁图就行了。