下面例子混合使用了逐帧动画和补间动画,还有一个缺点,就是画面闪烁,一直没有找到解决办法
上面点击ImageView,上面的人物就开始走路跟移动
定义动画文件
<?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/one" android:duration="10" />
<item android:drawable="@drawable/two" android:duration="10" />
<item android:drawable="@drawable/three" android:duration="10" />
<item android:drawable="@drawable/four" android:duration="10" />
<item android:drawable="@drawable/five" android:duration="10" />
<item android:drawable="@drawable/six" android:duration="10" />
<item android:drawable="@drawable/seven" android:duration="10" />
<item android:drawable="@drawable/eight" android:duration="10" />
<item android:drawable="@drawable/nine" android:duration="10" />
<item android:drawable="@drawable/ten" android:duration="10" />
<item android:drawable="@drawable/eleven" android:duration="10" />
<item android:drawable="@drawable/twelve" android:duration="10" />
<item android:drawable="@drawable/thirteen" android:duration="10" />
<item android:drawable="@drawable/fourteen" android:duration="10" />
<item android:drawable="@drawable/fifteen" android:duration="10" />
<item android:drawable="@drawable/sixteen" android:duration="10" />
<item android:drawable="@drawable/seventeen" android:duration="10" />
<item android:drawable="@drawable/eighteen" android:duration="10" />
<item android:drawable="@drawable/nineteen" android:duration="10" />
<item android:drawable="@drawable/twenty" android:duration="10" />
<item android:drawable="@drawable/twentyone" android:duration="10" />
<item android:drawable="@drawable/twentytwo" android:duration="10" />
<item android:drawable="@drawable/twentythree" android:duration="10" />
<item android:drawable="@drawable/twentyfour" android:duration="10" />
<item android:drawable="@drawable/twentyfive" android:duration="10" />
<item android:drawable="@drawable/twentysix" android:duration="10" />
<item android:drawable="@drawable/twentyseven" android:duration="10" />
<item android:drawable="@drawable/twentyeight" android:duration="10" />
<item android:drawable="@drawable/twentynine" android:duration="10" />
<item android:drawable="@drawable/thirty" android:duration="10" />
<item android:drawable="@drawable/thirtyone" android:duration="10" />
<item android:drawable="@drawable/thirtytwo" android:duration="10" />
<item android:drawable="@drawable/thirtythree" android:duration="10" />
<item android:drawable="@drawable/thirtyfour" android:duration="10" />
<item android:drawable="@drawable/thirtyfive" android:duration="10" />
<item android:drawable="@drawable/thirtysix" android:duration="10" />
</animation-list>
主界面引用动画
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#fff">
<ImageView android:id="@+id/walk" android:layout_width="150dp"
android:layout_height="150dp"
android:background ="@anim/walk" />
</LinearLayout>
主界面代码
package WangLi.Graphics.Walk;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class Walk extends Activity {
//记录行走人ImageView当前的位置
private float curX = 0;
private float curY = 30;
//记录行走人ImageView下一个位置的坐标
float nextX = 0;
float nextY = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取显示行走人的ImageView组件
final ImageView imageView = (ImageView)findViewById(R.id.walk);
final Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
if(msg.what == 0x123)
{
//横向上一直向右走
if(nextX > 320)
{
curX = nextX = 0;
}
else
{
nextX += 8;
}
//纵向上随机上下走
nextY = curY + (float)(Math.random() * 10 - 5);
//设置显示行走人的ImageView位置发生位移改变
TranslateAnimation anim = new TranslateAnimation(curX,
nextX,
curY,
nextY);
curX = nextX;
curY = nextY;
anim.setDuration(200);
anim.setFillEnabled(true);
//开始位移动画
imageView.startAnimation(anim);
}
}
};
final AnimationDrawable walk = (AnimationDrawable)imageView.getBackground();
imageView.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//开始播放人行走的逐帧动画
walk.start();
//通过定制器控制每0.2秒运行一次TanslateAnimation动画
new Timer().schedule(new TimerTask()
{
public void run()
{
handler.sendEmptyMessage(0x123);
}
}, 0, 200);
}
});
}
}