一、常用动画类
暂分为两类:
1、Animation
- TranslateAnimation 位移动画
//fromXDelta,fromYDelta起始坐标
//toXDelta,toYDelta 移动坐标
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta);
- AlphaAnimation 透明动画
//fromAlpha 起始透明度,toAlpha 最终透明度
AlphaAnimation(float fromAlpha, float toAlpha)
- ScaleAnimation 缩放动画
ScaleAnimation(float fromX, float toX, float fromY, float toY)
- AnimationSet
2、Animator
- ViewAnimator
ofInt(Object target, String propertyName, int... values)
ofFloat(Object target, String propertyName, float... values)
ofObject(Object target, String propertyName,
TypeEvaluator evaluator, Object... values)
ofPropertyValuesHolder(Object target,
PropertyValuesHolder... values)
ObjectAnimator
AnimatorSet
二、 Animator使用注意
- 一个ObjectAnimator多个动画复合
通过PropertyValuesHolder构建ObjectAnimator,如:
float tranX = getResources().getDimension(R.dimen._20dp);
float tranY = getResources().getDimension(R.dimen._10dp);
PropertyValuesHolder valuesHolderX = PropertyValuesHolder.ofFloat("translationX", 0.0f, -tranX);
PropertyValuesHolder valuesHolderY = PropertyValuesHolder.ofFloat("translationY", 0.0f, -tranY);
final ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(mTarget, valuesHolderX, valuesHolderY);
- Animator如何恢复默认值
通过Traget的setXXX方法设置相关默认值,如:
mTarget.setAlpha(1.0f);
mTarget.setTranslationX(0);
mTarget.setTranslationY(0);
- Animator监听器
Animator的监听器常用的有2种:
1、Animator.AnimatorListener 动画关键性的动作回调
2、AnimatorUpdateListener 动画更新的回调
public void addUpdateListener(AnimatorUpdateListener listener){}
public static interface AnimatorUpdateListener {
/**
* <p>Notifies the occurrence of another frame of the animation.</p>
*
* @param animation The animation which was repeated.
*/
void onAnimationUpdate(ValueAnimator animation);
}
public void addListener(AnimatorListener listener) {}
public static interface AnimatorListener {
/**
* <p>Notifies the start of the animation.</p>
*
* @param animation The started animation.
*/
void onAnimationStart(Animator animation);
/**
* `注意`如果动画有设置Repeat,则Repeat结束后最后才回调onAnimationEnd
* <p>Notifies the end of the animation. This callback is not invoked
* for animations with repeat count set to INFINITE.</p>
*
*
* @param animation The animation which reached its end.
*/
void onAnimationEnd(Animator animation);
/**
* <p>Notifies the cancellation of the animation. This callback is not invoked
* for animations with repeat count set to INFINITE.</p>
*
* @param animation The animation which was canceled.
*/
void onAnimationCancel(Animator animation);
/**
*
* <p>Notifies the repetition of the animation.</p>
*
* @param animation The animation which was repeated.
*/
void onAnimationRepeat(Animator animation);
}
三、其他
AnimationSet和AnimatorSet的区别:
AnimationSet 多个Animation动画同时进行,动画播放顺序不可控制。
AnimatorSet 多个Animator动画可以选择顺序也可同时进行,播放顺序不可控制。