我正在开发一个Android应用程序,其中我正在使用带有以下代码的gif图像
private static byte[] streamToBytes(InputStream is) {
ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
int len;
try {
while ((len = is.read(buffer)) >= 0) {
os.write(buffer, 0, len);
}
} catch (java.io.IOException e) {
}
return os.toByteArray();
}
//在类的构造函数中
is = this.context.getResources().openRawResource(R.drawable.fondo_juego_nexus);
if (DECODE_STREAM) {
System.out.println("in if DECODE_STREAM");
mMovie = Movie.decodeStream(is);
}
else {
byte[] array = streamToBytes(is);
System.out.println("in else DECODE_STREAM");
mMovie = Movie.decodeByteArray(array, 0, array.length);
}
// In On Draw
long now = android.os.SystemClock.uptimeMillis();
if (mMovieStart == 0) { // first time
mMovieStart = now;
}
if (mMovie != null) {
System.out.println("in if (mMovie != null) " + mMovie.duration());
int dur = mMovie.duration();
if (dur == 0)
{
dur = 1000;
System.out.println("in if movie if");
}
System.out.println("duration is "+ dur);
int relTime = (int)((now - mMovieStart) % dur);
mMovie.setTime(relTime);
System.out.println("in if displaying syd");
mMovie.draw(canvas,120,100);
}
并通过ontouch i退出活动,如
else if(_y<=60 && _x<=60)
{
sp.play(mySound1, volume, volume, 0, 0, 1);
sp.release();
playr.stop();
tme.cancel();
act.finish();
}
当我用上述方法退出活动并回到之前的活动时, 并再来到我正在使用gif图像的活动,它没有出现,在设备galaxy s2,2.3.3但在相同大小的2.2的模拟器上它很好
这种方法有什么问题,或者我可以用什么方式来显示gif图像
我该怎么做才能删除此错误
答案 0 :(得分:1)
我找到了解决问题的方法,我不知道它有多好, 但它之前我在构造函数
中使用此代码 if (DECODE_STREAM) {
mMovie = Movie.decodeStream(is);
} else {
byte[] array = streamToBytes(is);
mMovie = Movie.decodeByteArray(array, 0, array.length);
}
}
第一次工作正常,但我认为第二次因为内存耗尽 太多的垃圾收集器是隐式运行的 我的图像初始化也一次又一次地运行,这是原因 游戏表现不佳,而且 我做的是我刚刚补充的 手动使用
进行垃圾收集器System.gc();
并在此之后初始化所有图像 放置以下代码
if (DECODE_STREAM) {
mMovie = Movie.decodeStream(is);
} else {
byte[] array = streamToBytes(is);
mMovie = Movie.decodeByteArray(array, 0, array.length);
}
}
之后 这避免了垃圾收集器的隐式运行
现在gif图像工作正常