一、创建动画文件并放到res/drawable目录下
animation_circle.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:visible="true" android:oneshot="true">
<item android:drawable="@drawable/white_circle" android:duration="250" />
<item android:drawable="@drawable/gray_circle" android:duration="250" />
<item android:drawable="@drawable/black_circle" android:duration="250" />
</animation-list>
white_circle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#FFFFFF"/>
<size android:height="100dp" android:width="100dp"/>
</shape>
gray_circle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">"
<solid android:color="#555555"/>
<size android:height="100dp" android:width="100dp"/>
</shape>
black_circle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">"
<solid android:color="#000000"/>
<size android:height="100dp" android:width="100dp"/>
</shape>
二、创建布局xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidtest.DrawableAnimationActivity" >
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center"
android:src="@drawable/animation_circle" />
</RelativeLayout>
三、创建活动
package com.example.androidtest;
import android.support.v7.app.ActionBarActivity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
public class DrawableAnimationActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawable_animation);
ImageView iv = (ImageView)findViewById(R.id.image_view);
iv.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView iv = (ImageView)v;
AnimationDrawable ad = (AnimationDrawable)iv.getDrawable();
ad.stop();
ad.start();
return true;
}
});
}
}
运行效果