視頻截圖或遊戲界面截圖 要求獲取位圖數據 ,然後本地保存圖片。
方式1:測試通過
int screenshotSize = this.getWidth() * this.getHeight();
ByteBuffer bb = ByteBuffer.allocateDirect(screenshotSize * 4);
bb.order(ByteOrder.nativeOrder());
gl.glReadPixels(0, 0, this.getWidth(), this.getHeight(), GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb);
int pixelsBuffer[] = new int[screenshotSize];
bb.asIntBuffer().get(pixelsBuffer);
bb = null;
Bitmap bitmap = Bitmap.createBitmap(this.getWidth(), this.getHeight(), Bitmap.Config.RGB_565);
bitmap.setPixels(pixelsBuffer, screenshotSize-this.getWidth(), -this.getWidth(), 0, 0, this.getWidth(), this.getHeight());
pixelsBuffer = null;
short sBuffer[] = new short[screenshotSize];
ShortBuffer sb = ShortBuffer.wrap(sBuffer);
bitmap.copyPixelsToBuffer(sb);
//Making created bitmap (from OpenGL points) compatible with Android bitmap
for (int i = 0; i < screenshotSize; ++i) {
short v = sBuffer[i];
sBuffer[i] = (short) (((v&0x1f) << 11) | (v&0x7e0) | ((v&0xf800) >> 11));
}
sb.rewind();
bitmap.copyPixelsFromBuffer(sb);
screenshotCallback.onScreenshot(bitmap);
public abstract class AsynScreenshotCallback {
/**
*
* 異步截圖回調
* @param bitmap 截圖的位圖對象
* @throws
*/
public abstract void onScreenshot(Bitmap bitmap);
}
方法2 :http://www.eoeandroid.com/thread-56476-1-1.html
方法3:http://www.oschina.net/code/snippet_729469_19233
後面兩種沒有測試,先收藏起來。