问题描述
所以这里的交易,我已经搜查每一个问题,并链接在线,但没有一个是有帮助的。我在.jpg格式的动画的120帧为我的启动画面。据我所知,JPEG文件转换为位图在内存中,因此这就是为什么我得到一个OutOfMemoryError。我得到动画的最大帧是10。有什么办法一帧做这个框架,或者我应该试试别的。这是我的code:
So here's the deal, I've searched every single question and link online but none are helpful.I have 120 frames of an animation in .jpg format for my splash screen. I understand that jpegs are converted to bitmaps on memory so that's why I get an OutOfMemoryError. The maximum frames I get to animate are 10. Is there any way to do this frame by frame, or should I try something else. Here's my code:
final AnimationDrawable anim = new AnimationDrawable();
anim.setOneShot(true);
for (int i = 1; i <= 120; i++)
{
Drawable logo = getResources().getDrawable(getResources()
.getIdentifier("l"+i, "drawable", getPackageName()));
anim.addFrame(logo, 50);
if (i % 3 == 0)
{
System.gc();
}
}
ImageView myImageView = (ImageView) findViewById(R.id.SplashImageView);
myImageView.setBackgroundDrawable(anim);
myImageView.post(new Runnable()
{
public void run()
{
anim.start();
}
});
我已经放在120 JPEG文件下的绘制文件夹的LpreFIX(如L1,L2等)。我做垃圾回收,每3 JPEG文件,但不会做任何事情。
I've placed the 120 jpegs under the drawable folder with an "l" prefix (eg l1, l2 etc).I do garbage collection every 3 jpegs but that won't do a thing.
推荐答案
您可以尝试做没有 AnimationDrawable
使用 Handler.postDelayed
。事情是这样的:
You can try to do it without AnimationDrawable
using Handler.postDelayed
. Something like this:
final ImageView image = (ImageView) findViewById(R.id.SplashImageView);
final Handler handler = new Handler();
final Runnable animation = new Runnable() {
private static final int MAX = 120;
private static final int DELAY = 50;
private int current = 0;
@Override
public void run() {
final Resources resources = getResources();
final int id = resources.getIdentifier("l" + current, "drawable", getPackageName());
final Drawable drawable = resources.getDrawable(id);
image.setBackgroundDrawable(drawable);
handler.postDelayed(this, DELAY);
current = (current + 1) % MAX;
}
};
handler.post(animation);
这个解决方案需要更少的内存,因为它使只有一个可拉伸的时候。
This solution requires less memory because it keeps only one drawable at the time.
您可以使用取消动画 handler.removeCallbacks(动画);
如果你想要做一个一次性的动画,你可以叫 handler.postDelayed
有条件的:
If you want make a one-shot animation you can call handler.postDelayed
conditionally:
if (current != MAX - 1) {
handler.postDelayed(this, DELAY);
}
这篇关于创建使用在Android帧动画闪屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!