Below is the code that I use to take a screenshot using GLSurfaceView. But I do not know why the onDraw () method in the GLSurfaceView.Renderer class is not called.
Please, if someone can look at the code below and indicate what I'm doing wrong.
public class MainActivity extends Activity {
private GLSurfaceView mGLView;
int x,y,w,h;
Display disp;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
x=0;
y=0;
disp = getWindowManager().getDefaultDisplay();
w = disp.getWidth();
h = disp.getHeight();
mGLView = new ClearGLSurfaceView(this);
}
class ClearGLSurfaceView extends GLSurfaceView {
public ClearGLSurfaceView(Context context) {
super(context);
setDebugFlags(DEBUG_CHECK_GL_ERROR | DEBUG_LOG_GL_CALLS);
mRenderer = new ClearRenderer();
setRenderer(mRenderer);
}
ClearRenderer mRenderer;
}
class ClearRenderer implements GLSurfaceView.Renderer {
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}
public void onSurfaceChanged(GL10 gl, int w, int h) {
}
public void onDrawFrame(GL10 gl) {
int b[]=new int[w*(y+h)];
int bt[]=new int[w*h];
IntBuffer ib=IntBuffer.wrap(b);
ib.position(0);
gl.glReadPixels(x, 0, w, y+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)&0x00ff0000;
int pix1=(pix&0xff00ff00) | pr | pb;
bt[(h-k-1)*w+j]=pix1;
}
}
Bitmap bmp = Bitmap.createBitmap(bt, w, h,Bitmap.Config.ARGB_8888);
try
{
File f = new File("/sdcard/testpicture.png");
f.createNewFile();
FileOutputStream fos=new FileOutputStream(f);
bmp.compress(CompressFormat.PNG, 100, fos);
try
{
fos.flush();
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
fos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}
Please help me. I just started learning to work on Android.
source
share