animation-list实现android帧动画

Posted dahaiChang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了animation-list实现android帧动画相关的知识,希望对你有一定的参考价值。

首先在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/ic_loading_more_1"></item>
    <item android:duration="130" android:drawable="@drawable/ic_loading_more_2"></item>
    <item android:duration="130" android:drawable="@drawable/ic_loading_more_3"></item>
    <item android:duration="130" android:drawable="@drawable/ic_loading_more_4"></item>
    <item android:duration="130" android:drawable="@drawable/ic_loading_more_5"></item>
</animation-list>

备注:ic_loading_more_1到5为美工提供切图。

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

新建LoadingDialog类,实现dialog的调用:

package com.cyh.wisdombuddhism.utils;

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

import com.cyh.wisdombuddhism.R;


/**
 * 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);
        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 = view.findViewById(R.id.tv_reminder);
        loadingText.setText("验证中");

        mLoadingDialog = new Dialog(context);
        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中调用:

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,"验证中...",false);
            
        );

    

 

以上是关于animation-list实现android帧动画的主要内容,如果未能解决你的问题,请参考以下文章

android 帧动画的实现及图片过多时OOM解决方案

Android 基础的三种动画 帧动画补间动画属性动画。

Android动画之进度条

android如何设置Drawable Animation逐帧动画每一帧的持续时间

怎么自定义android 下拉刷新动画

Android 动画总结