最近项目需要用到帧动画,简单总结下:
效果图:
使用方法1:
在android/drawable中创建animlist
<?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/image1"
android:duration="400"/>
<item android:drawable="@drawable/image2"
android:duration="400"/>
<item android:drawable="@drawable/image2"
android:duration="400"/>
<item android:drawable="@drawable/image3"
android:duration="400"/>
<item android:drawable="@drawable/image4"
android:duration="400"/>
<item android:drawable="@drawable/image5"
android:duration="400"/>
</animation-list>
布局中给个image控件就不写了
主要是想在activity启动时动画就开始,那么就需要在activity中重写onWindowFocusChanged方法
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
//给图片添加动画背景
imageView.setImageResource(R.drawable.animlist);
//获取动画可以方便控制停止
frameAnim = (AnimationDrawable) imageView.getDrawable();
}
使用方法2:
在activity中动态添加图片
private void initFrameAnim() {
frameAnim = new AnimationDrawable();
// 为AnimationDrawable添加动画帧
frameAnim.addFrame(getResources().getDrawable(R.drawable.image1), 100);
frameAnim.addFrame(getResources().getDrawable(R.drawable.image2), 100);
frameAnim.addFrame(getResources().getDrawable(R.drawable.image3), 100);
frameAnim.addFrame(getResources().getDrawable(R.drawable.image4), 100);
frameAnim.addFrame(getResources().getDrawable(R.drawable.image5), 100);
// 设置ImageView的背景为AnimationDrawable
imageView.setBackgroundDrawable(frameAnim);
}
控制动画停止或开始
/**
* 开始播放
*/
protected void start() {
if (frameAnim != null && !frameAnim.isRunning()) {
frameAnim.start();
}
}
/**
* 停止播放
*/
protected void stop() {
if (frameAnim != null && frameAnim.isRunning()) {
frameAnim.stop();
}
}
扩展:
如果想要帧动画和补间动画一起用,须在android/anim文件夹下添加补间动画。
例:
位移动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fromXDelta="100"
android:fromYDelta="100"
android:toXDelta="200"
android:toYDelta="0"
/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fromXDelta="200"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="0"
/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="100"
android:toYDelta="100"
/>
</set>
透明度动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<alpha
android:fromAlpha="0.2"
android:toAlpha="1.0"
android:duration="200"
/>
</set>
然后使用AnimationUtils类
final Animation animation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.animtion);
final Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.animtion1);
final Animation animation2 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.animtion2);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
imageView.startAnimation(animation1);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
animation1.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
imageView.startAnimation(animation2);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
animation2.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation anim) {
imageView.startAnimation(animation);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
imageView.startAnimation(animation);