事情是这样的,我已经搜索了每一个问题和在线链接,但都没有帮助。 我的启动画面有 120 帧 .jpg 格式的动画。我知道 jpeg 会在内存中转换为位图,所以这就是我得到 OutOfMemoryError 的原因。我制作动画的最大帧数是 10。有什么方法可以逐帧执行此操作,还是我应该尝试其他方法。这是我的代码:
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 图片放在带有“l”前缀的可绘制文件夹下(例如 l1、l2 等)。 我每 3 个 jpeg 就进行一次垃圾收集,但这不会做任何事情。
最佳答案
您可以尝试在没有 AnimationDrawable
的情况下使用 Handler.postDelayed
来实现。像这样:
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);
此解决方案需要较少的内存,因为它一次只保留一个可绘制对象。
您可以使用 handler.removeCallbacks(animation);
取消动画。
如果你想制作一个一次性动画,你可以有条件地调用handler.postDelayed
:
if (current != MAX - 1) {
handler.postDelayed(this, DELAY);
}
关于android - 在 Android 上使用框架创建动画启动画面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14340372/