【问题标题】:Animation lags on large screen sizes动画在大屏幕尺寸上滞后
【发布时间】:2015-11-06 00:01:18
【问题描述】:

我有一个RelativeLayout,其中存储了五个ImageView,每个都包含一个云图像。所有图像的大小都不同,最小的为 582*182,最大的为 700*400。包含云的区域大小为860*1080。
屏幕顶部是我要加载到服务器上的图像,下面是上面描述的RelativeLayout。当用户落在那个屏幕上时,云开始移动。问题是整个动画严重滞后(我试图在 HTC One 上运行它)。 我的动画描述如下:

    <set xmlns:android="http://schemas.android.com/apk/res/android"
         android:interpolator="@android:anim/linear_interpolator"
         android:fillEnabled="true"
         android:fillAfter="true">
        <translate xmlns:android="http://schemas.android.com/apk/res/android"
                   android:duration="12000"
                   android:toXDelta="-12%p"
                   android:repeatCount="infinite"
                   android:repeatMode="reverse"/>
    </set>

ImageViews 是这样描述的:

<ImageView
    android:id="@+id/clouds_image_view_cloud2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="cloud 2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"/>

我开始如下:

Animation animRToL = AnimationUtils.loadAnimation(mContext, R.anim.right_to_left);
Animation animLToR = AnimationUtils.loadAnimation(mContext, R.anim.left_to_right);

mClouds[0].startAnimation(animRToL);
mClouds[2].startAnimation(animRToL);

mClouds[1].startAnimation(animLToR);
mClouds[3].startAnimation(animLToR);

mClouds[4].startAnimation(animRToL);

我已经通过mClouds[0].isHardwareAccelerated() 检查了硬件加速条件 - 它已启用。我试图通过以RGB_565 格式加载它们来减小位图的大小 - 不走运。对于这种滞后,我能做些什么吗?

P.S. 相同图像的动画在iOS 运行在iPhone 4 上的应用程序版本没有任何滞后。

更新:我注意到如果我将动画云的数量从 5 个减少到 4 个,动画会运行顺利。

【问题讨论】:

  • 您找到解决方案了吗?我遇到过类似的情况。
  • @Jerome 很遗憾没有,我们决定不在 android 上使用这样的动画。

标签: android lag translate-animation


【解决方案1】:

这听起来可能很疯狂,但在类似的情况下救了我:

为不同的视图使用不同的动画对象。

在您的情况下: 您需要三个 animRTL 对象和两个 animLTR Java 对象。所以:animLTR1、animLTR2、animRTL1、animRTL2、animRTL3。请注意,您不必创建比已有的两个更多的 XML 动画文件。

试着告诉我结果。

编辑:

我想到了另外两个值得尝试的解决方案:

在您的清单中,放在应用程序标签下

  android:hardwareAccelerated="true"

  android:largeHeap="true"

【讨论】:

  • 不幸的是它没有帮助。 :)
  • 抱歉,我编辑了我的答案,以防其他人遇到动画问题。
【解决方案2】:

与我类似,我的动画可以在小型设备上流畅运行,但在屏幕尺寸为 2560 x 1600 像素的三星 Galaxy Pro 等大型设备上运行缓慢、生涩。

我尝试了很多解决方案,但几乎放弃了。但是我意识到我仍然可以拖动而不滞后一些比我想用于动画的对象更复杂的对象。显然,某些 Android 动画(至少是翻译动画)并未真正优化。

首先,我尝试使用一些线程/睡眠来模拟拖动行为。我已经使用 Handler / Runnable 编写了代码,结果得到了相当大的改进。

然后在搜索了一段时间后,我发现 ValueAnimator 可以用我的 Handler / Runnable 代码做同样的工作,但方式更好。我的最终代码如下所示(它水平移动一个对象):

    ValueAnimator anim = ValueAnimator.ofFloat(0, amountToMoveX);
    anim.setDuration(300);
    final float startX = mainView.getX();

    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            mainView.setX(startX + (Float) animation.getAnimatedValue());
        }
    });

    anim.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
        }

        @Override
        public void onAnimationCancel(Animator animation) {
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
        }
    });

    anim.start();

【讨论】:

    猜你喜欢
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多