Android实用视图动画及工具系列之二:Toast对话框和加载载入对话框

实现效果



功能说明

类似Toast底色的弹出对话框和加载对话框,主要实现弹出和提示消息的功能,对话框可以实现不被取消,主要功能原理利用了安卓逐帧动画和继承对话框接口来实现,适用于新手及新学习Android的码友们,老玩家当然也可以看看,这个还是挺简单挺实用的,在后面会简略介绍实现方法及源代码,同时博客的最后还提供源代码和图片等资源github下载地址。

这篇文章的逐帧动画实现就不做详细介绍,前面的文章我有详细的介绍:
--------------------------------------------------------------------------------------------------------------------
Android实用视图动画及工具系列之一:简单的载入视图和载入动画:
http://blog.csdn.net/jaikydota163/article/details/52098833
--------------------------------------------------------------------------------------------------------------------

实现步骤

步骤一:添加逐帧动画资源和帧布局Drawable,以及对话框背景样式

顾名思义,逐帧动画就是一帧一帧的播放,在Android原生组件不主持gif的情况下,我们要实现逐帧动画只能使用一张一张图片来逐帧播放以达到效果,如下面的几张图(其他图片资源在源代码内,需要的自行下载):


将所有帧图片导入到Android项目目录的drawable文件夹下:
Android实用视图动画及工具系列之二:Toast对话框和加载载入对话框_第1张图片

在drawable目录下新建xml,取名loading_progress_white_rotate.xml,输入如下代码(附属性说明):
animation-list:Android动画列表 ; oneshot:true播放一次,false循环播放; item:每项动画; android:drawable:图片索引; android:duration:每帧持续时间。
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false" >

    <item android:duration="100" android:drawable="@drawable/loading_white_01">
    </item>
    <item android:duration="100" android:drawable="@drawable/loading_white_02">
    </item>
    <item android:duration="100" android:drawable="@drawable/loading_white_03">
    </item>
    <item android:duration="100" android:drawable="@drawable/loading_white_04">
    </item>
    <item android:duration="100" android:drawable="@drawable/loading_white_05">
    </item>
    <item android:duration="100" android:drawable="@drawable/loading_white_06">
    </item>
    <item android:duration="100" android:drawable="@drawable/loading_white_07">
    </item>
    <item android:duration="100" android:drawable="@drawable/loading_white_08">
    </item>
    <item android:duration="100" android:drawable="@drawable/loading_white_09">
    </item>
    <item android:duration="100" android:drawable="@drawable/loading_white_10">
    </item>
    <item android:duration="100" android:drawable="@drawable/loading_white_11">
    </item>
    <item android:duration="100" android:drawable="@drawable/loading_white_12">
    </item>

</animation-list>

新建toast_bgshape.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">

<gradient
    android:angle="90.0"
    android:endColor="#99363636"
    android:startColor="#99363636"/>

<corners android:radius="5dp"/>

</shape>


步骤二:新建自定义动画类

新建类ListAniImageView,代码如下,此类主要继承自ImageView,实现了基本动画播放,暂停和停止功能,注意包名改为自己的:
package com.jaiky.test.loadinganimation;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * Author by Jaiky, Email [email protected], Date on 9/22/2016.
 * PS: Not easy to write code, please indicate.
 */
public class ListAniImageView extends ImageView {

    private AnimationDrawable animationDrawable;

    public ListAniImageView(Context context) {
        super(context);
        inti();

    }

    public ListAniImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inti();
    }

    public ListAniImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inti();
    }

    public void inti() {
        animationDrawable = (AnimationDrawable) getDrawable();
        animationDrawable.start();
    }

    public void startAnimation() {
        animationDrawable.start();
    }

    public void stopAnimation() {
        animationDrawable.setVisible(true, true);
        animationDrawable.stop();
    }

    public void pauseAnimation() {
        animationDrawable.stop();
    }

}


步骤三:添加对话框样式和Toast对话框布局

在Android项目values文件下的styles.xml文件中添加如下代码(主要用于对话框显示样式):
<!-- 无标题无黑幕透明背景对话框样式 -->
    <style name="NoFrameNoDim_Dialog" parent="android:Theme.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>

在layout文件下新建布局文件dialog_toast.xml:
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_gravity="center"
    android:background="@drawable/toast_bgshape"
    android:gravity="center"
    android:orientation="vertical">

    <com.jaiky.test.toastdialog.ListAniImageView
        android:layout_width="42dp"
        android:layout_height="42dp"
        android:layout_gravity="center"
        android:src="@drawable/loading_progress_white_rotate"
        android:visibility="visible"/>

    <TextView
        android:id="@+id/dialogToast_tvLoadInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:text="正在提交..."
        android:textColor="#ffffff"
        android:textSize="12sp"
        />
</LinearLayout>

步骤四:新建Toast对话框类

新建类ToastDialog,继承自Dialog,代码如下:
setMsg(String msg):设置显示的文本。
setIsAllowClose(String msg):是否允许关闭对话框。
package com.jaiky.test.toastdialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;

/**
 * Author by Jaiky, Email [email protected], Date on 8/15/2016.
 * PS: Not easy to write code, please indicate.
 */
public class ToastDialog extends Dialog {

    private TextView tvLoadInfo;
    private boolean isAllowClose = true;

    public ToastDialog(Context context) {
        this(context, R.style.NoFrameNoDim_Dialog);
    }

    private ToastDialog(Context context, int theme) {
        super(context, theme);
        init();
    }

    private void init() {
        setContentView(R.layout.dialog_toast);
        tvLoadInfo = (TextView) findViewById(R.id.dialogToast_tvLoadInfo);
        setCanceledOnTouchOutside(true);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    /**
     * 设置加载消息内容
     *
     * @param msg
     */
    public ToastDialog setMsg(String msg) {
        tvLoadInfo.setText(msg);
        return this;
    }

    /**
     * 是否允许关闭对话框
     *
     * @param isAllowClose
     * @return
     */
    public ToastDialog setIsAllowClose(boolean isAllowClose) {
        setCanceledOnTouchOutside(isAllowClose);
        this.isAllowClose = isAllowClose;
        return this;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (!isAllowClose) {
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }
}


步骤五:Demo测试修改布局和主类

修改activity_main.xml内容如下(注意自定义控件包名):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.jaiky.test.toastdialog.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn1"
            android:text="弹出加载对话框"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <Button
            android:id="@+id/btn2"
            android:text="不允许关闭对话框"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

</RelativeLayout>

修改MainActiivty内容如下(注意包名):
package com.jaiky.test.toastdialog;

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

public class MainActivity extends AppCompatActivity {

    Button btn1, btn2;
    ToastDialog mToastDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);


        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mToastDialog == null)
                    mToastDialog = new ToastDialog(MainActivity.this);
                mToastDialog.setMsg("正在加载...")
                .show();
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mToastDialog == null)
                    mToastDialog = new ToastDialog(MainActivity.this);
                mToastDialog.setIsAllowClose(false);
            }
        });
    }
}

--------------------------------------------------------------------------------------------------------------------
获取源代码及资源文件:
https://github.com/jaikydota/Android-ToastDialog
--------------------------------------------------------------------------------------------------------------------

声明

欢迎转载,但请保留文章原始出处
作者:Jaiky_杰哥 
出处:http://blog.csdn.net/jaikydota163/article/details/52098841


你可能感兴趣的:(android帧动画,android对话框,Toast对话框,载入对话框,Android入门对话框)