Android屏幕截图速度更快?

时间:2014-09-12 15:19:38

标签: android cocos2d-android

我目前正在使用下面的方法创建一个模糊的屏幕截图(它是在另一个线程上找到的解决方案的略微修改版本)并且它可以正常运行,但生成速度相当慢在游戏中期使用非常有用。有谁能建议更好的解决方案?希望这对其他人也有用。

public static  Bitmap blurredScreenshot(){
    CGSize winSize = CCDirector.sharedDirector().displaySize();
    int w = (int) winSize.width;
    int h = (int) winSize.height;
    int b[] = new int[w * h];
    int bt[] = new int[w * h];
    IntBuffer ib = IntBuffer.wrap(b);
    ib.position(0);

    GL10 gl = CCDirector.gl;
    gl.glReadPixels(0, 0, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);

    for (int i = 0, k = 0; i < h; i++, k++) {
        for (int j = 0; j < w; j++) {
            int pix = b[i * w + j];
            int pb = (pix >> 16) & 0xff;
            int pr = (pix << 16) & 0xffff0000;
            int pix1 = (pix & 0xff00ff00) | pr | pb;
            bt[(h - k - 1) * w + j] = pix1;
        }
    }

    Bitmap bitmap = Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888);
    Bitmap scaledSmall = Bitmap.createScaledBitmap(bitmap, (int)w/5, (int)h/5, true);
    Bitmap scaledBlur = Bitmap.createScaledBitmap(scaledSmall, w, h, true);
    return scaledBlur;
}

1 个答案:

答案 0 :(得分:0)

查看Renderscript库:http://developer.android.com/guide/topics/renderscript/compute.html

Bitmap screen = [screenshot bitmap];
RenderScript rs = RenderScript.create(context);
Allocation input = Allocation.createFromBitmap(rs, screen,
    Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
Allocation output = Allocation.createTyped(rs, input.getType());
ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
script.setRadius(25);
script.setInput(input);

script.forEach(output);
output.copyTo(screen);

return screen;

获得更多模糊的技巧是将原始屏幕截图缩小一半。您还可以执行多次传递以增加模糊。在我的应用程序中,我在缩放到一半之后做了两次传递。