Android 动画之逐帧动画(Frame Animation)

概述

Android 3.0 之前,有两种类型:逐帧动画和补间动画;
Android 3.0 发布时,Android SDK 又提供了更加简单的属性动画;
Android 4.4 发布时,Android SDK 又提供了 android.transition 框架,更直观实现动画。

逐帧动画

也叫 Drawable Animation。有两种方式可以定义 XML 资源文件和代码实现。

XML 资源文件方式

  • 根标签为animation-list
  • 属性 oneshot true - 展示一遍,false - 循环播放动画
  • item标签对动画中的每一个图片进行声明
  • 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:drawable="@drawable/wifi_01"  
        android:duration="200"/>  
    <item  
        android:drawable="@drawable/wifi_02"  
        android:duration="200"/>  
    <item  
        android:drawable="@drawable/wifi_03"  
        android:duration="200"/>  
    <item  
        android:drawable="@drawable/wifi_04"  
        android:duration="200"/>  
    <item  
        android:drawable="@drawable/wifi_05"  
        android:duration="200"/>  
    <item  
        android:drawable="@drawable/wifi_06"  
        android:duration="200"/> 
</animation-list>  

案例

<?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="match_parent"  
    android:gravity="center"  
    android:orientation="vertical">  
  
    <Button  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:onClick="setStart"  
        android:text="开始" />  
  
    <Button  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginBottom="20dp"  
        android:onClick="setEnd"  
        android:text="结束" />  
  
    <ImageView  
        android:id="@+id/iv_anim"  
        android:layout_width="100dp"  
        android:layout_height="100dp"  
        android:padding="5dp"  
        android:src="@drawable/anim_fram" />  
</LinearLayout>  
private ImageView imageView;  
private AnimationDrawable animDrawable;  
  
@Override  
protected void onCreate(@Nullable Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_yao);  
    imageView = (ImageView) findViewById(R.id.iv_anim);  
    animDrawable = (AnimationDrawable) imageView.getDrawable();  
}  
  
public void setStart(View view) {  
    animDrawable.start();  
}  
  
public void setEnd(View view) {  
    animDrawable.stop();  
}  

代码

    private ImageView imageView;  
    private AnimationDrawable mAnimDrawable;  
  
    @Override  
    protected void onCreate(@Nullable Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_yao);  
        imageView = (ImageView) findViewById(R.id.iv_anim);  
        initDrawable();  
    }  
  
    private void initDrawable() {  
        mAnimDrawable = new AnimationDrawable();  
        for (int i = 1; i <= 6; i++) {  
            int id = getResources().getIdentifier("wifi_0" + i, "drawable", getPackageName());  
            Drawable drawable = getResources().getDrawable(id);  
            // 添加一帧,并设置该帧显示的持续时间  
            mAnimDrawable.addFrame(drawable, 200);  
        }  
        mAnimDrawable.setOneShot(false);  
        imageView.setImageDrawable(mAnimDrawable);  
    }  
  
    public void setStart(View view) {  
        AnimationDrawable animDrawable = (AnimationDrawable) imageView.getDrawable();  
        animDrawable.start();  
    }  
  
    public void setEnd(View view) {  
        AnimationDrawable animDrawable = (AnimationDrawable) imageView.getDrawable();  
        animDrawable.stop();  
    } 
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,094评论 25 709
  • Android框架提供了两种类型的动画:View Animation(也称视图动画)和Property Anima...
    RxCode阅读 1,864评论 1 5
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 15,405评论 4 61
  • 从小到大,没别的本事。只练就了两个本事,一个是忍,一个是哭。 可能骨子里天生就是有被人欺负的基因吧?还没上学的时候...
    天上的船阅读 401评论 2 1
  • 最近有几个小朋友很矫情的找我聊了一些有的没的乱七八糟的东西。想想突然觉得似乎该写点什么给你,以前总是很零散的讲一些...
    第十页阅读 262评论 0 0

友情链接更多精彩内容