【问题标题】:Animating custom view(leaf node in ViewHierarchy ) using ObjectAnimator is not smooth in android使用 ObjectAnimator 动画自定义视图(ViewHierarchy 中的叶节点)在 android 中不流畅
【发布时间】:2016-09-22 18:00:46
【问题描述】:

我在一个基于幻灯片的应用程序中工作,并使用自定义的 RelativeLayout 创建了一个形状,这是我的应用程序中非常深的节点(在 viewHierarchy 中的位置大约是第 90 级)。形状是幻灯片的内容。 在尝试使用 ObjectAnimator 或 ViewPropertyAnimator 对形状的 translate 、 rotate 或 scale 属性进行动画处理时。动画不流畅。它在闪烁。

这里是示例代码片段

PropertyValuesHolder pvhTX,pvhTY; PropertyValuesHolder.ofFloat(View.TRANSLATION_X,translateXFrom,translateXTo);

pvhTY = PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, translateYFrom, translateYTo);

anim1 = ObjectAnimator.ofPropertyValuesHolder(shape, pvhTX, pvhTY);

anim1.setDuration((long) (animData.getDetail().getDuration() * 1000)); // 根据我的要求添加监听器覆盖 onAnimationStart,onAnimationEnd。 anim1.addListener(new AnimationListener(shape, animData, slideEventHandler));

anim1.start();

在上面的代码中 形状是一个customeView(custome relativlayout),它可以是任何几何形状,如线圆立方体三角形等。

请帮帮我!!!

【问题讨论】:

    标签: android animation


    【解决方案1】:

    可能您在动画视图中使用了一些图像,或者它移动了一些动画图像。如果您为它们使用了 drawable 文件夹,请尝试将它们移动到文件夹 drawable-nodpi。因为当你使用drawable dir时,它每次都会进行dpi转换,所以在动画时会导致性能下降。

    这里是示例:

    public static void applyAnimation(Context context, View view, int animationResource, int animationOffsetMilisec){
            Animation anim = android.view.animation.AnimationUtils.loadAnimation(context, animationResource);
            anim.setStartOffset(animationOffsetMilisec);
            anim.setFillAfter(true);
            if(view != null) {
                view.setAnimation(anim);
                view.startAnimation(anim);
            }
        }
    

    然后将 sample_animation.xml 添加到 res.anim。

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/linear_interpolator">
        <rotate
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:duration="4000"
            android:fromDegrees="0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:repeatCount="infinite"
            android:startOffset="0"
            android:toDegrees="360"
            android:interpolator="@android:anim/linear_interpolator"/>
        <scale
            android:duration="100"
            android:fromXScale="0.0"
            android:toXScale="1.1"
            android:toYScale="1.1"
            android:fromYScale="0.0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:interpolator="@android:anim/accelerate_interpolator"/>
        <!--<alpha-->
        <!--android:fromAlpha="0.0"-->
        <!--android:toAlpha="1.0"-->
        <!--android:duration="1000" />-->
        <scale
            android:duration="100"
            android:fromXScale="1.1"
            android:toXScale="0.91"
            android:toYScale="0.91"
            android:fromYScale="1.1"
            android:pivotX="50%"
            android:pivotY="50%"
            android:startOffset="100"
            android:interpolator="@android:anim/accelerate_interpolator"/>
    
    
    </set>
    

    用法:

    applyAnimation(context, yourview, R.anim.sample_animation, 0);
    

    【讨论】:

    • @stephan 仅供参考,问题在于各种形状的填充。然而,形状可以用图像、渐变或纯色填充。但在所有情况下都会发生闪烁。我正在尝试使用实心填充。这是自定义视图。那么绘图中是否需要任何优化?我检查了我的自定义形状的 onDraw 方法,它被调用了很多次。请帮我。谢谢
    • @Stephan 感谢您的回复 :-) 但它不符合我的要求。我正在根据我对动画的要求使用关键帧、PropertiesValueHolder 和 ObjectAnimator 制作动画所以我在开始之前创建了所有这些对象动画 请概述一下我的代码 sn-p。我正在将动画应用于我绘制的形状,它是customeView(relativelayout 的子级),这个视图位于最深的级别(从根视图开始的第 90 级)。我应用的动画正在正确发生,但有一些闪烁(不流畅)请在这方面帮助我谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多