首先注明下:本文章来自于网络,我只是按照人家的例子写代码运行了一下地址现在找不到了以后找到后给加上去。
animation_list用户补间动画是用的,直接上例子了。
第一步创建Android工程把用到的图片添加到项目中图片资源也给大家贴出来。
第二步:编写animation_list文件
<?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/a" android:duration="150"></item>
<item android:drawable="@drawable/c" android:duration="150"></item>
<item android:drawable="@drawable/d" android:duration="150"></item>
<item android:drawable="@drawable/e" android:duration="150"></item>
<item android:drawable="@drawable/f" android:duration="150"></item>
</animation-list>
下面的是反向的
<?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/f" android:duration="150"></item>
<item android:drawable="@drawable/e" android:duration="150"></item>
<item android:drawable="@drawable/d" android:duration="150"></item>
<item android:drawable="@drawable/c" android:duration="150"></item>
<item android:drawable="@drawable/a" android:duration="150"></item>
</animation-list>
编写按钮处理事件:
package com.ygc.android.animation;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener {
private ImageView mImageView;
private Button mShow, mStop, mDaoxu;
private AnimationDrawable animationDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* Description:初始化控件的对象
*/
private void initView() {
mImageView = (ImageView) findViewById(R.id.animationIV);
mShow = (Button) findViewById(R.id.buttonA);
mStop = (Button) findViewById(R.id.buttonB);
mDaoxu = (Button) findViewById(R.id.buttonC);
mDaoxu.setOnClickListener(this);
mShow.setOnClickListener(this);
mStop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == mShow) {
mImageView.setImageResource(R.drawable.animstion_test1);
animationDrawable = (AnimationDrawable) mImageView.getDrawable();
animationDrawable.start();
} else if (v == mStop) {
animationDrawable = (AnimationDrawable) mImageView.getDrawable();
animationDrawable.stop();
} else if (v == mDaoxu) {
mImageView.setImageResource(R.drawable.animstion_test2);
animationDrawable = (AnimationDrawable) mImageView.getDrawable();
animationDrawable.start();
}
}
}
xml文件
<?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">
<ImageView android:id="@+id/animationIV"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5px"
/>
<Button android:id="@+id/buttonA"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5px"
android:text="顺序显示" />
<Button android:id="@+id/buttonB"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5px"
android:text="停止" />
<Button android:id="@+id/buttonC"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5px"
android:text="倒序显示" />
</LinearLayout>
Demo下载:Demo下载