1、FrameAnimation
帧动画:
a、(AnimationDrawable对象)就像GIF图片,通过一系列Drawable依次显示来模拟动画的效果;
b、必须以animation-list为根元素,以item表示要轮换的照片,duration表示各项显示的持续时间(XML文件要放在/res/anim/或者/res/animator目录下)
c、AnimationDrawable对象的方法:
isRunning()返回值为boolean型,动画是否正在运行
start()动画开始播放
stop()动画停止播放
setOneShot(false/true);一组动画是否只播放一次
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/anim1"
android:duration="150"/>
<item
android:drawable="@drawable/anim2"
android:duration="150"/>
<item
android:drawable="@drawable/anim3"
android:duration="150"/>
<item
android:drawable="@drawable/anim4"
android:duration="150"/>
<item
android:drawable="@drawable/anim5"
android:duration="150"/>
<item
android:drawable="@drawable/anim6"
android:duration="150"/>
</animation-list>
//AnimationDrawable对象
private AnimationDrawable anim = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//通过ImageView的getBackground()方法,此方法的返回值为Drawable类型
//因为其ivPic的是AnimationDrawable故可强转,从而获得AnimationDrawable对象
//<--android:background="@drawable/boy_girl"-->
anim = (AnimationDrawable) ivPic.getBackground();
}
/**
* 屏幕的触摸事件(只要触摸屏幕即使不触摸图片也会触发)
* 只要用户在屏幕上有点击或触摸事件的发生就会自动触发此方法
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
//一组动画是否只播放一次
anim.setOneShot(false);
//调用AnimationDrawable对象的start()方法开始动画
anim.start();
return super.onTouchEvent(event);
}
2、TweenAnimation
补间动画:
a、就是一系列View 形状的变换,如大小的缩放,透明度的改变,水平/旋转位置的改变
b、动画的定义既可以用java代码定义也可以用XML定义(建议XML)
用XML资源实现补间动画
用XML定义的动画要放在res/anim文件夹内
优点:代码重用性比较高
alpha.xml
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:fromAlpha="0.5"
//注意:此属性若无set节点,当前节点为最外层节点可以设置
//对比set.xml
//fillbefore属性:动画结束后画面停留在第一帧
//fillafter属性: 动画结束后画面停留在最后一帧
android:fillAfter="false"
android:toAlpha="0"
android:repeatCount="2"
android:repeatMode="restart" >
</alpha>
public void alpha(View v){
Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
ivPic.startAnimation(anim);
}
translate.xml
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
// 指的是开始平移的位置
id:fromXDelta="50"
// 指的是要平移到的位置
android:toXDelta="50"
android:fromYDelta="0"
android:toYDelta="0"
android:repeatCount="2"
android:repeatMode="restart">
</translate>
public void translate(View v){
Animation anim = AnimationUtils.loadAnimation(this, R.anim.translate);
ivPic.startAnimation(anim);
}
set.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
//若存在组合关系,不能在</alpha>,</scale>,</translate>,
//</rotate>中设置,不然没有效果,而必须在</set>节点中设置
android:fillAfter="true" >
<alpha
android:duration="3000"
android:fromAlpha="0.5"
android:repeatCount="2"
android:repeatMode="restart"
android:toAlpha="0" />
<translate
android:duration="3000"
android:fromXDelta="0"
android:fromYDelta="0"
android:repeatCount="2"
android:repeatMode="restart"
android:toXDelta="100"
android:toYDelta="200" >
</translate>
</set>