动画在Android Studio中过慢。

huangapple 未分类评论49阅读模式
英文:

Animation is Too Slow in Android Studio

问题

我想在我的Android应用中显示由最多127个png文件组成的动画。然而,我无法让它们以我想要的速度播放。如您下面所见,我创建了一个AnimationDrawable对象,并将png文件作为帧添加到其中。我调用了animation.addFrame(frame, 1),所以总动画播放时间应为127毫秒(127帧 x 1毫秒),但在我的手机上播放所需的时间超过了一秒。从对代码进行性能分析,我发现byte[]导致了大部分的内存使用,我尝试通过进一步调整图像大小来解决这个问题。然而,动画的速度仍远远达不到127毫秒。最佳的行动方向是什么?

void createAnimation(AnimationDrawable animation, int endFrameIdx){
    for (int i = 0; i < endFrameIdx; i++) {
        String name = idArray[i]; // idArray包含png文件的名称
        int id = getResources().getIdentifier(name, "drawable", getPackageName());
        Drawable d = ContextCompat.getDrawable(this, id);
        Drawable compressD = resize(d);
        animation.addFrame(compressD, 1);} // 每帧1毫秒
    }
}

private Drawable resize(Drawable image) {
    Bitmap b = ((BitmapDrawable)image).getBitmap();
    Bitmap bitmapResized = Bitmap.createScaledBitmap(b, 90, 161, true);
    return new BitmapDrawable(getResources(), bitmapResized);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView imageanim = findViewById(R.id.imageanim);
    AnimationDrawable animation = new AnimationDrawable();
    int stopFrameIdx = 270; // 最大添加到动画中的帧数
    createAnimation(animation, stopFrameIdx);
    imageanim.setImageDrawable(animation);
    animation.start();
}

已经进行了性能分析的代码调整png文件大小可以减少`byte[]`所占用的内存但动画仍然播放得很慢
英文:

I would like to display animations consisting of up to 127 png files in my Android app. However, I can't get them to play as fast as I would like. As you can see below I create an AnimationDrawable object and add png files as frames to it. I call animation.addFrame(frame, 1) so the total animation should play for 127 ms (127 frames x 1 ms), but on my phone it takes more than a second to play. From profiling my code, I see byte[] contributes to most memory usage, and I try to solve this by resizing the images further. However, the speed of the animation is still nowhere close to 127 ms. What is the best course of action?

void createAnimation(AnimationDrawable animation, int endFrameIdx){
    for (int i = 0; i &lt; endFrameIdx; i++) {
        String name = idArray[i]; // idArray contains name of png files
        int id = getResources().getIdentifier(name, &quot;drawable&quot;, getPackageName());
        Drawable d = ContextCompat.getDrawable(this, id);
        Drawable compressD = resize(d);
        animation.addFrame(compressD, 1);} // 1 ms per frame
    }
}

private Drawable resize(Drawable image) {
    Bitmap b = ((BitmapDrawable)image).getBitmap();
    Bitmap bitmapResized = Bitmap.createScaledBitmap(b, 90, 161, true);
    return new BitmapDrawable(getResources(), bitmapResized);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView imageanim = findViewById(R.id.imageanim);
    AnimationDrawable animation = new AnimationDrawable();
    int stopFrameIdx = 270; // maximum # of frames to add to animation
    createAnimation(animation, stopFrameIdx);
    imageanim.setImageDrawable(animation);
    animation.start();

Profiled Code. Resizing the png files reduces memory consumed by byte[], but animation still plays slowly.

动画在Android Studio中过慢。

答案1

得分: 0

在尝试优化我的代码并未看到重大改进后,我目前的最佳猜测是动画受手机显示器的刷新率限制(60赫兹)。尽管我对安卓支持以较小时间间隔显示帧的方法感到困惑。

英文:

After trying to optimize my code and failing to see major improvements, my best guess for now is that the animation is limited by the phone display refresh rate (60 hz). Though I'm confused why android supports methods that display frames at smaller time intervals.

huangapple
  • 本文由 发表于 2020年6月29日 11:47:25
  • 转载请务必保留本文链接:https://java.coder-hub.com/62630896.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定