Android 实现代码:
public static void animAlphaScaleShowView(final View view, int time_ms) {
if (animFlag) return;
AnimationSet set = new AnimationSet(true);
AlphaAnimation alphaAnim = new AlphaAnimation(0.2f, 1.0f);
set.addAnimation(alphaAnim);
ScaleAnimation scaleAnim = new ScaleAnimation(1.0f, 1.5f, 1.0f, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
set.addAnimation(scaleAnim);
set.setFillAfter(true);
set.setDuration(time_ms);
set.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
animFlag = true;
}
@Override
public void onAnimationEnd(Animation animation) {
animFlag = false;
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
view.startAnimation(set);
}
鸿蒙实现代码:
public static void animAlphaScaleShowView(final Component view, int time_ms) {
AnimatorProperty animator = view.createAnimatorProperty();
//
view.setAlpha(0.2f);
animator.alpha(1.0f);
//
animator.setCurveType(Animator.CurveType.ANTICIPATE_OVERSHOOT);
animator.scaleXFrom(1.0f);
animator.scaleX(1.5f);
animator.scaleYFrom(1.0f);
animator.scaleY(1.5f);
//
animator.setDuration(time_ms);
//
animator.setLoopedCount(0);
animator.start();
}
对比两者有什么区别,都是透明度+缩放的变化 我感觉Android提供构造函数稍微好一点。不过4.0都不用java了 也无所谓了。
但是其中 setLoopedCount() 让我确实没摸着头脑,看见这个参数是不是首先认为 要么是不限循环、要么是播放几次,为什么播放是次数是从0开始 。实际上传0 会播放一次、1播放2次,这个API 这个参数一点都不友好。
看官方的解释:
setLoopedCount
public AnimatorProperty setLoopedCount(int loopedCount)
Sets the repetition times of an animator.
Parameters:
Parameter Name | Parameter Description |
---|---|
loopedCount | Specifies the repetition times of an animator. The value must be greater than or equal to 0, or INFINITE. If the value is 0, the animator will be played only once. |
Returns:
Returns the current property animator for continuous property settings.
确实count是从0开始的,所以还是在开发中要注意一下。
动画学习参考2:文档中心