在实际开发中还没用到帧动画,但不代表帧动画不重要。帧动画的原理很简单,就是如小时候用胶片放电影一样,一帧一帧的画面连起来就如同动画一样。这里只是简单介绍下它的实现方法,具体步骤在代码注释部分。(本文与上一篇写在同一个项目中)
下面是fram_animation代码,放在res/drawable文件夹下:
<?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/girl_1"
android:duration="200"/>
<item
android:drawable="@drawable/girl_2"
android:duration="200"/>
<item
android:drawable="@drawable/girl_3"
android:duration="200"/>
<item
android:drawable="@drawable/girl_4"
android:duration="200"/>
<item
android:drawable="@drawable/girl_5"
android:duration="200"/>
<item
android:drawable="@drawable/girl_6"
android:duration="200"/>
<item
android:drawable="@drawable/girl_7"
android:duration="200"/>
<item
android:drawable="@drawable/girl_8"
android:duration="200"/>
<item
android:drawable="@drawable/girl_9"
android:duration="200"/>
<item
android:drawable="@drawable/girl_10"
android:duration="200"/>
<item
android:drawable="@drawable/girl_11"
android:duration="200"/>
</animation-list>
这里要说明一下:如果直接设置android:src="@drawable/
fram_animation",那么图片直接变成动画。
下面是布局的XML文件:
<?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:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/bt_start"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="开始" />
<Button
android:id="@+id/bt_stop"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="停止" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/girl_1" />
</RelativeLayout>
</LinearLayout>
这里要说明下:如果直接设置
接下来是JAVA代码:
package com.example.animationexercise;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
/**
* 具体步骤:
* 1、新建fram_animation文件,放在res/drawable文件夹下;
* 2、将fram_animation文件作为资源赋给imageview;
* mImageView.setImageResource(R.drawable.fram_animation);
* 3、获得AnimationDrawable类的对象实例,进而控制动画的启动和停止;
* animationDrawable = (AnimationDrawable) mImageView.getDrawable();
* animationDrawable.start();
* animationDrawable.stop();
*/
public class FramAnimationActivity extends Activity implements OnClickListener {
private Button bt_start;
private Button bt_stop;
private ImageView mImageView;
private AnimationDrawable animationDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fram_animation_layout);
initView();
}
private void initView() {
// TODO Auto-generated method stub
bt_start = (Button) findViewById(R.id.bt_start);
bt_stop = (Button) findViewById(R.id.bt_stop);
mImageView = (ImageView) findViewById(R.id.imageView1);
bt_start.setOnClickListener(this);
bt_stop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bt_start:
mImageView.setImageResource(R.drawable.fram_animation);
animationDrawable = (AnimationDrawable) mImageView.getDrawable();
animationDrawable.start();
break;
case R.id.bt_stop:
animationDrawable.stop();
break;
default:
break;
}
}
}
好了,代码就这些,是不是很简单,自己写写看吧!