【问题标题】:Android Animation High CPU UsageAndroid 动画 CPU 使用率高
【发布时间】:2018-09-25 19:02:40
【问题描述】:

我正在尝试创建一个简单的 android 动画。 一个弹跳的谷歌地图标记。 我已经实现了一个简单的代码来做到这一点,但是当我在 API 28 上运行它时几乎没问题。当我在每个其他 API 版本、多个模拟器甚至我的手机上运行代码时,CPU 使用率看起来非常高,在所有运行时间内从 35% 到 80-90%。我所有的主要 Activity 看起来都比较慢。

我正在使用约束布局,我不知道这是否有帮助

这是我的bounce.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/bounce_interpolator">
    <translate
        android:duration="800"
        android:toYDelta="-50"
        android:repeatCount="infinite"
        android:repeatMode= "reverse"
     />
</set>

这是我简单的主要活动:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.AnimationUtils;

public class Home extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        findViewById(R.id.img_pointer1_id).startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.bounce));
    }
}

关于如何解决这个问题/任何替代解决方案的任何建议?

【问题讨论】:

标签: java android android-layout android-studio cpu-usage


【解决方案1】:

这是您的动画的 ObjectAnimator 版本。你能试试这个吗?希望对您有所帮助。

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //findViewById(R.id.img_pointer1_id).startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.bounce));

    ObjectAnimator animator = ObjectAnimator.ofFloat(findViewById(R.id.img_pointer1_id), "translationY", 0, -50, 0);
    animator.setInterpolator(new BounceInterpolator());
    animator.setDuration(800);
    animator.setRepeatCount(ValueAnimator.INFINITE);
    animator.setRepeatMode(ValueAnimator.REVERSE);
    animator.start();
}

【讨论】:

  • 您尝试更改图像吗?也许图像大小或格式会导致问题。我尝试了代码 24x24px png 图像,并且 cpu 使用率在不同的 api 版本上为 %20-25。
  • 当我使用硬件加速器和动画缓存之类的技巧单独运行 imageView 时,cpu 使用率几乎为 5%,每次动画再次开始时循环峰值达到 13% 最大值。但是当我在屏幕上添加更多静态图像和我的约束布局的背景时,问题又出现了。我使用的所有图像都通过压缩器,以获得更低的质量和更好的性能。没有图像高于 166KB。如果有帮助,我可以分享我所有的图像和代码。
  • 这是您的代码在 API21 设备(不是模拟器)上运行时 Profiler 的屏幕截图。弹跳图片 113kb png 400x400 像素。也许它有帮助。 imgur.com/a/5whrjL2
  • @TizianoSilvi 有什么进展吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-23
  • 1970-01-01
  • 1970-01-01
  • 2021-12-09
  • 2014-07-16
相关资源
最近更新 更多