frame Animation 的小例子

frame-by-frame animation 是一个展示一连串图片的简单的动画
实现frame-by-frame animation 的步骤
A将一连串图片存放在drawable文件夹中
B define the list of frames to be animated.  the name of the file is aframe_animation.xml
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/circle1" android:duration="50" />
<item android:drawable="@drawable/circle2" android:duration="50" />
<item android:drawable="@drawable/circle3" android:duration="50" />
</animation-list>

上面这个xml的作用时将这三张图片一次放映,时间间隔是50mill second

create XML Layout File for the Frame Animation Example
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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="Debuge Scrath pad"
    />
    <Button
    android:id = "@+id/startButtonId"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="start animation"/>
    <ImageView
    android:id = "@+id/animationImage"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>

the main Activity
 
package hust.ophoneclub.FrameAnimation;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class FrameAnimation extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setupButton();
    }

 /**
  *
  */
 private void setupButton() {
  Button btn =(Button)findViewById(R.id.startButtonId);
  btn.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View v) {
    startAnimate();
   }
  });
 }
 
 private void startAnimate() {
  ImageView imageView = setBackgroundOfImageView();
  animate(imageView);
 }

 /**
  * @param imageView
  */
 private void animate(ImageView imageView) {
  AnimationDrawable frameAnimation =
   (AnimationDrawable)imageView.getBackground();
  if(frameAnimation.isRunning()){
   frameAnimation.stop();
  }else{
   frameAnimation.stop();
   frameAnimation.start();
  }
 }
 
 private ImageView setBackgroundOfImageView() {
  ImageView imageView =(ImageView)findViewById(R.id.animationImage);
  imageView.setVisibility(ImageView.VISIBLE);
  imageView.setBackgroundResource(R.drawable.frame_animation);
  return imageView;
 }
}

说明:frameAnimation是作为main.xml中的background
imageView.setBackgroundResource(R.drawable.frame_animation);
这句代码的作用是将定义frame Animation 的 xml rescourse 文件转化为一个AnimationDrwable 对象
加到imageView的背景上  所以我们要取得这个对象来进行相关的操作
  AnimationDrawable frameAnimation =
   (AnimationDrawable)imageView.getBackground();
   这句代码就取得了这个对象;
   
最后的效果是这样的


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值