您的位置:首页 > 移动开发 > Android开发

Animation特效——Drawable Animation

2016-03-03 16:24 447 查看

Animation特效——Drawable Animation

Drawable Animation又被称为Frame Animation,译为帧动画,就像GIF图片,通过一系列Drawable依次显示来模拟动画的效果。

一、Drawable Animation 实现

我们以Android XML形式进行特效实现。

1)在eclipse创建XML文件时,将Resource Type选择Drawable类型,Root Element选为animation-list。
2)具体实现示例如下:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true" >

<item
android:drawable="@drawable/pic_i"
android:duration="200"/>
<item
android:drawable="@drawable/pic_love"
android:duration="200"/>
<item
android:drawable="@drawable/pic_you"
android:duration="200"/>

</animation-list>
XML文件必须以<animation-list>为根目录,以<item>表示要轮换显示的图片。
oneshot
表示动画循环播放。如果设置为false,表示动画只播放一次停止在最后一帧上; 如果为true,表示帧动画的循环播放
duration
表示各项显示的时间

二、调用显示

示例如下:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
imageView.setBackgroundResource(R.drawable.drawable_love);
anim = (AnimationDrawable) imageView.getBackground();
}

public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
anim.stop();
anim.start();
return true;
}
return super.onTouchEvent(event);
}


不过在调用的时候需要注意以下的问题。

1、动画特效的赋值,选择在代码中调用Imageview的setBackgroundResource方法,而不要直接在XML布局文件中设置其src属性。
原因:说明文档中说明



不要在onCreate中调用start,因为AnimationDrawable还没有完全跟Window相关联,如果想要界面显示时就开始动画的话,可以在onWindowFoucsChanged()中调用start()。

2、在动画start()之前要先stop(),不然在第一次动画之后会停在最后一帧,这样动画就只会触发一次。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 布局 xml 小鱼