<?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/aa" android:duration="300"></item>
<item android:drawable="@drawable/bb" android:duration="300"></item>
<item android:drawable="@drawable/cc" android:duration="300"></item>
<item android:drawable="@drawable/dd" android:duration="300"></item>
<item android:drawable="@drawable/ee" android:duration="300"></item>
<item android:drawable="@drawable/ff" android:duration="300"></item>
</animation-list>
oneshot是为了设置一次动画 还是多次动画,true就是一次执行后就停止。
下面的代码就是在imageview中写入drawable中的xml,即上面的代码。
setContentView(R.layout.frame);
btn = (Button) findViewById(R.id.button1);
iv=(ImageView) findViewById(R.id.imageView1);
iv.setImageResource(R.drawable.frame_anim);
animationDrawable =(AnimationDrawable) iv.getDrawable();
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(animationDrawable.isRunning())animationDrawable.stop();
animationDrawable.start();
}
});
渐变效果:
在res下建文件夹 anim,然后在下面建alpha_anim.xml,
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="1"
android:repeatCount="3"
android:repeatMode="restart"
android:toAlpha="0" >
</alpha>
setContentView(R.layout.frame);
btn = (Button) findViewById(R.id.button1);
iv=(ImageView) findViewById(R.id.imageView1);
//iv.setImageResource(R.drawable.frame_anim);
animationDrawable =(AnimationDrawable) iv.getDrawable();
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Animation animation =
AnimationUtils.loadAnimation(FrameActivity.this, R.anim.alpha_anim);
//给iv设置一个图片
iv.setImageResource(R.drawable.aa);
//iv开始播放动画
iv.startAnimation(animation);
}
});