我需要以每秒不同的帧数显示图像,最大 FPS 为 30。图像来自 SD 卡,并且都具有相同的尺寸:480 x 640。我创建了 3 个可能的解决方案,但每个都有问题:
以下结果为 30 FPS。
一、不重用位图
- 大量 GC 调用:大约。每秒 30 次垃圾回收
CPU负载:高达92%
private Bitmap bitmap; private void startAnimation1() { TimerTask updateImage = new UpdateImage1(); timer.scheduleAtFixedRate(updateImage, 0, 1000 / FPS); } class UpdateImage1 extends TimerTask { @Override public void run() { try { if (i == IMAGES_NR) { i = 0; } bitmap = BitmapFactory.decodeStream(new FileInputStream(framesFiles[i]), null, null); i++; } catch (FileNotFoundException e) { System.out.println("Exception 1: " + e.getMessage()); } runOnUiThread(new Runnable() { @Override public void run() { imgView.setImageBitmap(bitmap); } }); } }
二、通过 BitmapFactory.Options.inBitmap 重用位图
- GC 调用较低 -每秒 1 或 2 个
- CPU负载:高达84%
运行动画一段时间后,应用程序崩溃:
06-20 15:08:58.158: WARN/System.err(7880): java.lang.ArrayIndexOutOfBoundsException: length=-5131855; regionStart=0; regionLength=1024
06-20 15:08:58.158: WARN/System.err(7880): at java.util.Arrays.checkOffsetAndCount(Arrays.java:1731)
06-20 15:08:58.158: WARN/System.err(7880): at java.io.BufferedInputStream.read(BufferedInputStream.java:273)
06-20 15:08:58.158: WARN/System.err(7880): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
06-20 15:08:58.158: WARN/System.err(7880): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
06-20 15:08:58.158: WARN/System.err(7880): at com.example.SendPreviewOptimization.MyActivity$UpdateImage2.run(MyActivity.java:148)
06-20 15:08:58.158: WARN/System.err(7880): at java.util.Timer$TimerImpl.run(Timer.java:284)
06-20 15:08:58.168: DEBUG/skia(7880): ---- read threw an exception
06-20 15:08:58.168: DEBUG/skia(7880): --- decoder->decode returned false
06-20 15:08:58.168: WARN/System.err(7880): java.lang.IllegalArgumentException: Problem decoding into existing bitmap
06-20 15:08:58.168: WARN/System.err(7880): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:590)
06-20 15:08:58.168: WARN/System.err(7880): at com.example.SendPreviewOptimization.MyActivity$UpdateImage2.run(MyActivity.java:148)
06-20 15:08:58.168: WARN/System.err(7880): at java.util.Timer$TimerImpl.run(Timer.java:284)
06-20 15:08:58.178: ERROR/msm8960.hwcomposer(330): prepareBypass: Unable to setup bypass due to non-pmem memory
06-20 15:08:58.198: ASSERT/libc(7880): Fatal signal 11 (SIGSEGV) at 0xffd1d447 (code=1)
06-20 15:08:58.238: ERROR/msm8960.hwcomposer(330): prepareBypass: Unable to setup bypass due to non-pmem memory
06-20 15:08:58.498: ERROR/MP-Decision(1448): DOWN Ld:25 Ns:1.100000 Ts:190 rq:0.000000 seq:194.000000
06-20 15:08:58.708: INFO/DEBUG(27660): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
private static BitmapFactory.Options bitmapOptions;
private FileInputStream in;
private void startAnimation2() {
bitmapOptions = new BitmapFactory.Options();
// setup bitmap reuse options:
bitmapOptions.inPurgeable = true;
bitmapOptions.inInputShareable = true;
bitmapOptions.inBitmap = reusableBitmap;
bitmapOptions.inMutable = true;
bitmapOptions.inSampleSize = 1;
TimerTask updateImage = new UpdateImage2();
timer.scheduleAtFixedRate(updateImage, 0, 1000 / FPS);
}
class UpdateImage2 extends TimerTask {
@Override
public void run() {
try {
if (i == IMAGES_NR) {
i = 0;
}
//** version 1:
in = new FileInputStream(framesFiles[i]);
//decode into existing bitmap
BitmapFactory.decodeStream(in, null, bitmapOptions);
in.close();
//** version 2:
//BitmapFactory.decodeFile(framesFiles[i].getAbsolutePath(), bitmapOptions);
i++;
} catch (Exception e) {
System.out.println("Exception 2: " + e.getMessage());
}
runOnUiThread(new Runnable() {
@Override
public void run() {
imgView.setImageBitmap(reusableBitmap);
}
});
}
}
三、选项 III:使用缓冲区Bytebuffer
(提高效率的 一件事是使用直接内存。)
这个选项我无法使它工作:(
private ByteBuffer buffer; private byte[] b; private IntBuffer mPixels; private void startAnimation3() { buffer = ByteBuffer.allocate(480 * 640 * 6); b = new byte[480 * 640 * 6]; TimerTask updateImage = new UpdateImage3(); timer.scheduleAtFixedRate(updateImage, 0, 1000 / FPS); } class UpdateImage3 extends TimerTask { public void run() { try { if (i == IMAGES_NR) { i = 0; } FileInputStream frameInputStream = new FileInputStream(framesFiles[i]); frameInputStream.read(b); buffer.wrap(b); buffer.position(0); reusableBitmap.copyPixelsFromBuffer(buffer); frameInputStream.close(); i++; } catch (Exception e) { System.out.println("Exception 3: " + e.getMessage()); } runOnUiThread(new Runnable() { @Override public void run() { imgView.setImageBitmap(reusableBitmap); } }); } } private ByteBuffer copyToBuffer(Bitmap bitmap) { int size = bitmap.getHeight() * bitmap.getRowBytes(); ByteBuffer buffer = ByteBuffer.allocateDirect(size); bitmap.copyPixelsToBuffer(buffer); return buffer; }
在上述每个解决方案中,我在 logcat 中收到了很多
ERROR/msm8960.hwcomposer(330): prepareBypass: Unable to setup bypass due to non-pmem memory
我不知道究竟是什么意思。
我以前没有使用过位图重用,不知道哪个是最好的解决方案。
我已经添加了我在这里创建的项目:https ://www.dropbox.com/sh/3xov369u1bmjpd1/qBQax4t48D以及 2 帧/图像。
对 Neron T 的回答
我试过那个库:
//Option IV:
private AQuery aquery;
private void startAnimation4() {
aquery = new AQuery(this);
aquery.id(R.id.imgView);
TimerTask updateImage = new UpdateImage4();
timer.scheduleAtFixedRate(updateImage, 0, 1000 / FPS);
}
class UpdateImage4 extends TimerTask {
public void run() {
try {
if (i == 29) {
i = 0;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
//load image from file, down sample to target width of 300 pixels
aquery.image(framesFiles[i],300);
}
});
i++;
} catch (Exception e) {
System.out.println("Exception 4: " + e.getMessage());
}
}
}
它没有像我预期的那样工作 - 每张照片之前我都有一个闪烁的效果。我认为首先它会清除图片,然后添加一个新图片:(