终于建了一个自己个人小站:https://huangtianyu.gitee.io,以后优先更新小站博客,欢迎进站,O(∩_∩)O~~
在各种应用中,通常会使用到多种动画效果,在Android中常见的动画有四种:Scale,Alpha,Translate,Rotate。Android系统也很人性化的给出了利用xml方式和利用Java代码方式进行设置。以下分别进行介绍,算是给自己做个总结。
首先给出工程的地址:Android各种动画效果Scale,Alpha,Translate,Rotate
1.ScaleAnimation动画
xml方式进行设置:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="2000"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:interpolator="@android:anim/decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="2"
android:repeatMode="reverse"
android:startOffset="100"
android:toXScale="1.2"
android:toYScale="1.2" />
<!--
fromXDelta,fromYDelta 动画起始时X,Y座标
toXDelta, toYDelta 动画结束时X,Y的座标
-->
<!--
interpolator 动画所使用的类型:常见的有加速减速插入器:加速插入器 accelerate_interpolator;速插入器 decelerate_interpolator。
fromXScale,fromYScale, 动画开始前X,Y的缩放,0.0为不显示, 1.0为正常大小
toXScale,toYScale, 动画最终缩放的倍数, 1.0为正常大小,大于1.0放大
pivotX, pivotY 动画起始位置,相对于屏幕的百分比,两个都为50%表示动画从屏幕中间开始
startOffset, 动画多次执行的间隔时间,如果只执行一次,执行前会暂停这段时间,单位毫秒 duration,一次动画效果消耗的时间,单位毫秒,
repeatCount, 动画重复的计数,动画将会执行该值+1次
repeatMode, 动画重复的模式,reverse为反向,当第偶次执行时,动画方向会相反。 restart重新执行,方向不变
-->
</set>
工程中引用:
anim = AnimationUtils.loadAnimation(mContext, R.anim.scale_test);
//设置动画开始,结束时候的监听
anim.setAnimationListener(this);
img.startAnimation(anim);
Java代码方式设置:
anim = new ScaleAnimation(0.0f, 1.2f, 0.0f, 1.2f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
//Animation.ABSOLUTE:绝对坐标
//Animation.RELATIVE_TO_PARENT:相对父控件
//Animation.RELATIVE_TO_SELF:相对自己
anim.setDuration(2000);//所有属性可以通过set函数设置
anim.setAnimationListener(this);//进行下一个动画时候要清除上一个动画
img.startAnimation(anim);
2.AlphaAnimation动画
xml方式:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="2300"
android:repeatCount="2"
android:repeatMode="reverse"
android:interpolator="@android:anim/accelerate_interpolator"
android:startOffset="100"
android:background="@android:color/holo_green_light"
/>
<!--
fromAlpha:开始时透明度
toAlpha: 结束时透明度
duration:动画持续时间
-->
</set>
工程中引用:
anim = AnimationUtils.loadAnimation(mContext, R.anim.alpha_test);
anim.setAnimationListener(this);
img.startAnimation(anim);
Java代码设置:
anim = new AlphaAnimation(1.0f, 0.0f);
anim.setDuration(2000);
img.startAnimation(anim);
3.TranslateAnimation动画
xml方式:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="2000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="690"
android:repeatCount="1"
android:repeatMode="reverse"
android:toYDelta="0" />
<!--
fromXDelta,fromYDelta 动画起始时X,Y的座标
toXDelta,toYDelta 动画结束时X,Y的座标
-->
</set>
在工程中的引用代码:
anim = AnimationUtils.loadAnimation(mContext, R.anim.tranlate_test);
anim.setAnimationListener(this);
img.startAnimation(anim);
Java代码方式
anim = new TranslateAnimation(0.0f, 690f, 0f, 0f);
anim.setAnimationListener(this);
img.startAnimation(anim);
4.
RotateAnimation动画
xml方式:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<rotate
android:duration="5000"
android:fromDegrees="300"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="-360" />
<!--
fromDegrees 动画开始时的角度
toDegrees 动画结束时物件的旋转角度,正代表顺时针
pivotX 属性为动画相对于物件的X坐标的开始位置
pivotY 属性为动画相对于物件的Y坐标的开始位置
-->
</set>
在工程中引用:
anim = AnimationUtils.loadAnimation(mContext, R.anim.rotate_test);
anim.setAnimationListener(this);
img.startAnimation(anim);
Java代码设置:
anim = new RotateAnimation(0.0f, -360f, 540/2f, 540/2f);
anim.setAnimationListener(this);
anim.setDuration(2000);
anim.setRepeatCount(2);
anim.setRepeatMode(RotateAnimation.RESTART);
img.startAnimation(anim);
5.多个动画合并
xml方式:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<rotate
android:duration="5000"
android:fromDegrees="300"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="-360" />
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="2300"
android:repeatCount="2"
android:repeatMode="reverse"
android:interpolator="@android:anim/accelerate_interpolator"
android:startOffset="100"
android:background="@android:color/holo_green_light"
/>
</set>
在工程中的引用:
Animation a1 = AnimationUtils.loadAnimation(mContext, R.anim.alpha_test);
Animation a2 = AnimationUtils.loadAnimation(mContext, R.anim.rotate_test);
AnimationSet set = new AnimationSet(mContext,null);
set.addAnimation(a1);
set.addAnimation(a2);
set.setAnimationListener(this);
img.startAnimation(set);
其中在上面用到的监听是:
new AnimationListener() {
/**
* 动画开始前的回调
*/
@Override
public void onAnimationStart(Animation animation) {
if (flag) {//看当前img是否在动画,是就取消
img.clearAnimation();
flag = false;
}else{
flag = true;
}
Log.d(TAG, "start");
}
/**
* 动画重复时的回调
*/
@Override
public void onAnimationRepeat(Animation animation) {
Log.d(TAG, "repeat");
}
/**
* 动画结束后的回调
*/
@Override
public void onAnimationEnd(Animation animation) {
Log.d(TAG, "end");
flag = false;
}
}
从上面代码可以看出,几个动画效果设置都差不多,会一个其他的都是相通的。
工程的地址:Android各种动画效果Scale,Alpha,Translate,Rotate