Android-AnimationDrawable自定义加载动画dialog

来源:互联网 发布:淘宝的差评有什么影响 编辑:程序博客网 时间:2024/05/17 04:04

说明:

Drawable Animation,即帧动画。
opertyAnimation,即属性动画,稍后将会介绍)
Drawable animation允许我们一张一张的加载Drawable资源。这是一种传统的动画方式,通过一系列不同图片的顺序播放,可以制造出电影一样的效果。AnimationDrawable类是实现这种动画效果的基类。
用AnimationDrawable提供的API,我们当然可以在代码中定义想要展示的每一帧的图片,但是使用xml来列出我们想要展现的图片的方式更加的方便。如果采用xml的方式,我们需要在res/drawable文件下面创建,然后在xml文件里面指定我们要展示的每一帧的图片资源和持续的时间。

具体实现

首先为帧动画dialog自定义一个布局文件,该布局文件中包含了dialog的文本以及所要加载的图片动画

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/no_color"    android:gravity="center_horizontal"    android:orientation="vertical" >    <ImageView        android:id="@+id/dialog_progress_img"        android:layout_width="100dp"        android:layout_height="100dp"        android:background="@anim/gif_orange" />    <TextView        android:id="@+id/dialog_progress_text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:text="玩命加载中……"        android:textColor="@color/dark_white"        android:textSize="20dp" /></LinearLayout>

图片动画使用的是加载一个资源文件@anim/gif_orange ,资源文件里边是定义的帧动画所要播放的图片的顺序以及每个图片所有加载的时间

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android" >    <item        android:drawable="@drawable/icon_orange_1"        android:duration="60"/>    <item        android:drawable="@drawable/icon_orange_2"        android:duration="60"/>    <item        android:drawable="@drawable/icon_orange_3"        android:duration="60"/>    <item        android:drawable="@drawable/icon_orange_4"        android:duration="60"/>    <item        android:drawable="@drawable/icon_orange_5"        android:duration="60"/>    <item        android:drawable="@drawable/icon_orange_6"        android:duration="60"/>    <item        android:drawable="@drawable/icon_orange_7"        android:duration="60"/>    <item        android:drawable="@drawable/icon_orange_8"        android:duration="60"/></animation-list>

最后由布局文件进行帧动画dialog的自定义实现,分别加载布局文件以及将dialog所要加载的图片定义为AnimationDrawable格式,实现帧动画的播放。

public class DialogProgress extends Dialog {    protected Context context;    protected TextView tv_text;    protected ImageView iv_img;    protected String title = "", text = "";    protected int img_id = 0;    public DialogProgress(Context context) {        super(context, R.style.MyDialogTheme);        this.context = context;        this.title = "";    }    public DialogProgress(Context context, String title) {        super(context, R.style.MyDialogTheme);        this.context = context;        this.title = title;    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        if (title != null && !title.equals(""))            setTitle(title);        else            requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.dialog_progress);        initView();    }    @Override    protected void onStart() {        super.onStart();        initViewSet();    }    private void initView() {        tv_text = (TextView) findViewById(R.id.dialog_progress_text);        iv_img = (ImageView) findViewById(R.id.dialog_progress_img);        AnimationDrawable animationDrawable = (AnimationDrawable) iv_img                .getBackground();        animationDrawable.start();    }    protected void initViewSet() {        if (!"".equals(text)) {            tv_text.setText(text);        }        if (img_id != 0) {            iv_img.setBackgroundResource(img_id);            try {                AnimationDrawable animationDrawable = (AnimationDrawable) iv_img                        .getBackground();                animationDrawable.start();            } catch (Exception e) {            }        }    }    public DialogProgress setTitleNew(String title) {        this.title = title;        return this;    }    public DialogProgress setText(String text) {        this.text = text;        return this;    }    public DialogProgress setImageRes(int id) {        this.img_id = id;        return this;    }}