在 Canvas 中绘制位图的帧速率更高(第 2 部分)?

发布于 2024-11-10 21:09:19 字数 1015 浏览 4 评论 0原文

这就是我想要做的:我正在尝试加载一系列位图来显示动画。这意味着我需要有下降 fps(24 fps)。我需要加载不超过 10 秒的动画或大约 300 个位图。由于它是动态壁纸,我只能手动绘制每一帧。

这是我到目前为止所尝试的:

  1. 明显的策略:每帧加载每个位图并绘制它。这很慢。
  2. 使用缓存。我将位图加载放在单独的线程中。大约 20 个位图的缓存在后台持续加载。然而,我的性能仍然很差(大约 10 fps)。罪魁祸首是位图加载缓慢。
  3. 我本来打算尝试使用 OpenGL,但后来我意识到即使使用 OpenGL,位图加载缓慢仍然是一个问题。正确的? (或者我错了?)

那么我还可以使用哪些其他策略?

这就是我的想法:如果我使用 openGL,我可以使用较小的位图(因为它可以提供更好的缩放效果)。也许那时我可以拥有更大的缓存 - 比如说也许 3 秒。有了更大的缓存,位图加载慢就不会成为问题了,对吗?

还有其他策略吗?


哦,这是我当前的位图加载函数:

void loadNthBitmap(int i, int n) {
    try {
    buf = new 
        BufferedInputStream(assets.
                open(folder+"/"
                     +imageList[n])
                );
    tmpBitmap = BitmapFactory.
        decodeStream(buf);
    rbitmap[i] = Bitmap.createBitmap
        (tmpBitmap,
         0,0,imageWidth,imageHeight,
         transMatrix,false);
    }
    catch(IOException e) {}
}

其中 imageList 是预定义的资源列表,transMatrix 是旋转和缩放矩阵。

Here's what I'm trying to do: I'm trying to load a sequence of bitmaps to display an animation. That means that I need to have a descent fps (24 fps). I need to load no more than 10 seconds of animations or about 300 bitmaps. And since it's a live wallpaper I'm limited to manually drawing each frame.

Here's what I tried so far:

  1. The obvious strategy: load each bitmap each frame and draw it. This is slow.
  2. Use caching. I put the bitmap loading in a seperate thread. A cache of about 20 bitmaps continously load in the background. However, I still get poor performance (about 10 fps). The culprit is slow bitmap loading.
  3. I was going to try using OpenGL but then I realized that even with OpenGL, the slow bitmap loading will still be a problem. Right? (Or am I wrong?)

So what other strategies can I use?

Here's what I had in mind: if I use openGL, I can use smaller bitmaps (because it gives better scaling). Perhaps then I can have a bigger cache - say perhaps 3 seconds. With a larger cache, the slow bitmap loading will not be a problem, right?

Any other strategies?


Oh and this is my current bitmap loading function:

void loadNthBitmap(int i, int n) {
    try {
    buf = new 
        BufferedInputStream(assets.
                open(folder+"/"
                     +imageList[n])
                );
    tmpBitmap = BitmapFactory.
        decodeStream(buf);
    rbitmap[i] = Bitmap.createBitmap
        (tmpBitmap,
         0,0,imageWidth,imageHeight,
         transMatrix,false);
    }
    catch(IOException e) {}
}

where imageList is pre-defined list of assets and transMatrix is a rotation and scaling matrix.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半﹌身腐败 2024-11-17 21:09:19

您想要做的事情(即全帧动画)并不容易。几乎所有 Android 动画(从 SDK 中的月球着陆器示例到《愤怒的小鸟》)都是由在相对静态的背景上移动的小精灵组成。

解决您的问题的“明显”解决方案是将您的动画转换为 mpeg,然后将其解码为视频动态壁纸,复制此人在此采用的方法: http://forum.xda-developers.com/showthread.php?t=804720(替代链接 http://android.ccpcreations.com/vlw/)

What you're trying to do (i.e. full-frame animation) is not easy. Almost all Android animation (from the Lunar Lander example in the SDK to Angry Birds) consists of moving small sprites on a relatively static background.

The "obvious" solution to your problem would be to turn your animation into an mpeg and then decode it as a video live wallpaper, replicating the approach that this guy took here: http://forum.xda-developers.com/showthread.php?t=804720 (alternative link http://android.ccpcreations.com/vlw/)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文