frame-by-frame animation 是一个展示一连串图片的简单的动画
实现frame-by-frameanimation的步骤
A将一连串图片存放在drawable文件夹中
Bdefinethelistofframestobeanimated.thenameofthefileisaframe_animation.xml
<animation-listxmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<itemandroid:drawable="@drawable/circle1"android:duration="50"/>
<itemandroid:drawable="@drawable/circle2"android:duration="50"/>
<itemandroid:drawable="@drawable/circle3"android:duration="50"/>
</animation-list>上面这个xml的作用时将这三张图片一次放映,时间间隔是50mill second
create XML Layout File for the Frame Animation Example
main.xml
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/textViewId1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="DebugeScrathpad"
/>
<Button
android:id="@+id/startButtonId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="startanimation"/>
<ImageView
android:id="@+id/animationImage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>the main Activitypackage hust.ophoneclub.FrameAnimation;
import android.app.Activity;
importandroid.graphics.drawable.AnimationDrawable;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;
import android.widget.ImageView;public class FrameAnimation extends Activity {
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupButton();
}/**
*
*/
privatevoidsetupButton(){
Buttonbtn=(Button)findViewById(R.id.startButtonId);
btn.setOnClickListener(new Button.OnClickListener(){@Override
publicvoidonClick(Viewv){
startAnimate();
}
});
}
privatevoidstartAnimate(){
ImageViewimageView=setBackgroundOfImageView();
animate(imageView);
}/**
*@paramimageView
*/
privatevoidanimate(ImageViewimageView){
AnimationDrawableframeAnimation=
(AnimationDrawable)imageView.getBackground();
if(frameAnimation.isRunning()){
frameAnimation.stop();
}else{
frameAnimation.stop();
frameAnimation.start();
}
}
privateImageViewsetBackgroundOfImageView(){
ImageViewimageView=(ImageView)findViewById(R.id.animationImage);
imageView.setVisibility(ImageView.VISIBLE);
imageView.setBackgroundResource(R.drawable.frame_animation);
returnimageView;
}
}说明:frameAnimation是作为main.xml中的background
imageView.setBackgroundResource(R.drawable.frame_animation);
这句代码的作用是将定义frameAnimation的xmlrescourse文件转化为一个AnimationDrwable对象
加到imageView的背景上所以我们要取得这个对象来进行相关的操作
AnimationDrawableframeAnimation=
(AnimationDrawable)imageView.getBackground();
这句代码就取得了这个对象;
最后的效果是这样的
