HarmonyOS 动画中 AnimatorProperty setLoopedCount​(int loopedCount)

文章比较了Android和鸿蒙OS中的animAlphaScaleShowView方法,强调了Android的构造函数易用性以及鸿蒙的AnimatorPropertyAPI。特别提到了setLoopedCount参数的不直观性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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开始的,所以还是在开发中要注意一下。

https://developer.harmonyos.com/cn/docs/documentation/doc-references/animatorproperty-0000001054439987#ZH-CN_TOPIC_0000001054439987__setLoopedCount-int-

动画学习参考1:Samples: We provide a series of app samples to help you quickly get familiar with the APIs and app development process of the HarmonyOS SDKs. | 为帮助开发者快速熟悉HarmonyOS SDK所提供的API和应用开发流程,我们提供了一系列的应用示例 - Gitee.com

 动画学习参考2:文档中心

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值