Android Drawable Animation

作者: smart_dev | 来源:发表于2016-04-14 17:20 被阅读2257次
帧动画

上面这个效果是某软件上的一个loading效果,怎么实现呢?

概述:


像电影一样,以一个顺序来逐帧播放图片资源,这种动画故称作帧动画。Android同样提供了两种实现方式,代码中和xml中实现:

  • 在XML中使用标签<animation-list/>来定义
  • 在代码中与之对应的类是AnimationDrawable

使用

动画文件存放位置:

res/drawable/filename.xml

资源引用:

In Java: R.drawable.filename
In XML: @[package:]drawable.filename

XML中语义

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot=["true" | "false"] >
    <item
        android:drawable="@[package:]drawable/drawable_resource_name"
        android:duration="integer" />
</animation-list>
  • <animation-list/> 这个标签是必须有的根节点,用它来包括一个或者多个<item />节点
  • android:oneshot为true表示仅仅执行一次,false为循环执行这个动画,默认false
  • <item />一个这样的标签表示动画的一帧,
    • 其中android:drawable来指定图片资源,
    • android:duration来指定这一帧执行时间

OK,直接贴出上面的loading效果实现代码

动画文件

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

    <item
        android:drawable="@drawable/a76"
        android:duration="70" />
    <item
        android:drawable="@drawable/a77"
        android:duration="70" />
    <item
        android:drawable="@drawable/a78"
        android:duration="70" />
   
</animation-list>

对应的三张资源文件

a76.png
a77.png
a78.png

布局文件

<?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"
    tools:context="com.smart.myapplication.animation.FrameActivity">

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/loading1"
        android:scaleType="center" />

    <Button
        android:id="@+id/start_anim"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开启动画" />

</RelativeLayout>

activity中:

private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_frame);
    imageView = (ImageView) findViewById(R.id.imageview);
    findViewById(R.id.start_anim).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // 获取AnimationDrawable对象
            AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();

            if (animationDrawable.isRunning()) { //判断这个动画是否正在运行
                animationDrawable.stop(); // 停止
            } else {
                animationDrawable.start(); // 启动
            }
        }
    });
}

至此上面那个效果实现了。

如果在Activity中我不想用按钮触发这个动画,要程序运行即播放动画,怎么做呢?
最好是在onWindowFocusChanged这个方法中启动动画。因为在onCreate中启动动画, AnimationDrawable有可能还没有完全attach 到Window上

另外AnimationDrawable其他几个重要的方法

  • addFrame(Drawable frame, int duration)
    添加一帧动画,图片资源和时间,这样就可以在代码中动态的增加某一帧动画了

  • setOneShot(boolean oneShot)
    设置动画是否仅执行一次

关于Android其他Drawable资源的使用

相关文章

  • 属性动画

    Android提供了几种动画:View Animation 、Drawable Animation 、Proper...

  • 动画

    Android包含三种动画:View Animation(Tween Animation)、 Drawable A...

  • Android属性动画

    1、概述Android常用的动画有View Animation、Drawable Animation、Proper...

  • android动画及用法

    android动画及用法 android中三种动画:view animation, drawable animat...

  • Android Drawable Animation

    上面这个效果是某软件上的一个loading效果,怎么实现呢? 概述: 像电影一样,以一个顺序来逐帧播放图片资源,这...

  • Android动画

    Android 动画大致可以分为view animation(视图动画,补间动画) ,drawable anima...

  • Android动画

    Android动画主要包含补间动画(Tween)View Animation、帧动画(Frame)Drawable...

  • Android 帧动画用来加载

    首先我们在drawable下新建一个animation-list 其中的 android:oneshot="fal...

  • Android动画四:逐帧、视图、属性动画使用教程详解

    Android 动画主要分为3种: 逐帧动画(Drawable Animation) 视图动画/补间动画(View...

  • Android-动画

    Android动画 可以分为3类,分别是: View Animation(视图动画也叫补间动画) Drawable...

网友评论

    本文标题:Android Drawable Animation

    本文链接:https://www.haomeiwen.com/subject/ccpglttx.html