Android动画完整小实例完善
针对http://android.yaohuiji.com/archives/795博客的动画小实例完善xml
项目名Animation,源代码:
package animation.com;
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;
/**
* @author Lambert
*/
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button start = (Button)findViewById(R.id.start);
Button stop = (Button)findViewById(R.id.stop);
final ImageView iv = (ImageView)findViewById(R.id.image);//注意要添加关键字final修饰
OnClickListener ocl = new OnClickListener() {
@Override
public void onClick(View v) {
AnimationDrawable ad = (AnimationDrawable)iv.getBackground();//创建AnimationDrawable对象
switch(v.getId()){
case R.id.start:
ad.start();//开始播放动画
break;
case R.id.stop:
ad.stop();停止播放动画
break;
}
}
};
start.setOnClickListener(ocl);//设置播放按钮监听
stop.setOnClickListener(ocl);//设置停止按钮监听
}
}
res文件夹/layout文件夹/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"
>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@anim/firefox_animation"//把图片的背景设为动画
android:id="@+id/image"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="play_animation"
android:id="@+id/start"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/stop"
android:text="stop_animation"/>
</LinearLayout>
res文件夹/anim文件夹/firefox_animation.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<animation-list android:oneshot="false"//设置动画是否只展示一次,false即持续动画。
xmlns:android="http://schemas.android.com/apk/res/android">//前缀不能少
<itme android:duration="200" android:drawable="@drawable/firefox_animation_0_thumb" />
//设置一帧画面显示的时间为200毫秒,并找到drawable文件夹下对应的图片
<item android:duration="200" android:drawable="@drawable/firefox_animation_1_thumb" />
<item android:duration="200" android:drawable="@drawable/firefox_animation_2_thumb" />
<item android:duration="200" android:drawable="@drawable/firefox_animation_3_thumb" />
<item android:duration="200" android:drawable="@drawable/firefox_animation_4_thumb" />
<item android:duration="200" android:drawable="@drawable/firefox_animation_5_thumb" />
<item android:duration="200" android:drawable="@drawable/firefox_animation_6_thumb" />
<item android:duration="200" android:drawable="@drawable/firefox_animation_7_thumb" />
<item android:duration="200" android:drawable="@drawable/firefox_animation_8_thumb" />
<item android:duration="200" android:drawable="@drawable/firefox_animation_9_thumb" />
<item android:duration="200" android:drawable="@drawable/firefox_animation_10_thumb" />
<item android:duration="200" android:drawable="@drawable/firefox_animation_11_thumb" />
<item android:duration="200" android:drawable="@drawable/firefox_animation_12_thumb" />
<item android:duration="200" android:drawable="@drawable/firefox_animation_13_thumb" />
<item android:duration="200" android:drawable="@drawable/firefox_animation_14_thumb" />
<item android:duration="200" android:drawable="@drawable/firefox_animation_15_thumb" />
<item android:duration="200" android:drawable="@drawable/firefox_animation_16_thumb" />
<item android:duration="200" android:drawable="@drawable/firefox_animation_17_thumb" />
<item android:duration="200" android:drawable="@drawable/firefox_animation_18_thumb" />
<item android:duration="200" android:drawable="@drawable/firefox_animation_19_thumb" />
<item android:duration="200" android:drawable="@drawable/firefox_animation_20_thumb" />
<item android:duration="200" android:drawable="@drawable/firefox_animation_21_thumb" />
<item android:duration="200" android:drawable="@drawable/firefox_animation_22_thumb" />
<item android:duration="200" android:drawable="@drawable/firefox_animation_23_thumb" />
<item android:duration="200" android:drawable="@drawable/firefox_animation_24_thumb" />
</animation-list>