/res/anim/loading.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list
android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item android:duration="150" android:drawable="@drawable/recording1" />
<item android:duration="150" android:drawable="@drawable/recording2" />
<item android:duration="150" android:drawable="@drawable/recording3" />
<item android:duration="150" android:drawable="@drawable/recording4" />
<item android:duration="150" android:drawable="@drawable/recording5" />
<item android:duration="150" android:drawable="@drawable/recording6" />
<item android:duration="150" android:drawable="@drawable/recording7" />
</animation-list>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
>
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="start"
/>
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="end"
/>
</LinearLayout>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:background="@anim/loading"
/>
</LinearLayout>
package com.carter.animationtest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.graphics.drawable.AnimationDrawable;
public class AnimationTestMain extends Activity{
Button btnStart, btnStop;
ImageView mImage;
AnimationDrawable anim;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnStart = (Button)findViewById(R.id.bt1);
btnStop = (Button)findViewById(R.id.bt2);
mImage = (ImageView)findViewById(R.id.image);
anim = (AnimationDrawable)mImage.getBackground();
btnStart.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
anim.start();
}
});
btnStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
anim.stop();
}
});
}
}
定义一个AnimationDrawable对象,通过ImageView的getBackground()方法来强制类型转换为一个AnimationDrawable。
这样就可已通过start()和stop()方法来进行控制动画播放了。