Android-AnimationDrawable自定义加载动画dialog

说明:

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;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android Studio中的逐帧动画是指将一系列静态图像按照一定的顺序快速播放,形成动画效果。在Android Studio中,可以使用AnimationDrawable类来实现逐帧动画。具体步骤如下: 1. 在res/drawable目录下创建一个XML文件,用于定义逐帧动画。例如,可以创建一个名为animation.xml的文件。 2. 在XML文件中,使用<animation-list>标签定义逐帧动画。在<animation-list>标签中,使用<item>标签定义每一帧的图像。例如,可以使用以下代码定义一个逐帧动画,其中包含三张图像: <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/frame1" android:duration="100" /> <item android:drawable="@drawable/frame2" android:duration="100" /> <item android:drawable="@drawable/frame3" android:duration="100" /> </animation-list> 3. 在Java代码中,使用AnimationDrawable加载XML文件,并将其设置为ImageView的背景。例如,可以使用以下代码实现逐帧动画: ImageView imageView = findViewById(R.id.imageView); AnimationDrawable animationDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.animation); imageView.setBackground(animationDrawable); animationDrawable.start(); 4. 运行应用程序,即可看到逐帧动画效果。 需要注意的是,逐帧动画可能会占用较多的内存和CPU资源,因此应该谨慎使用。如果需要实现复杂的动画效果,建议使用属性动画或帧动画

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Amrecs

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值