Android中的截屏

3
以下是我使用GLSurfaceView进行屏幕截图的代码。但我不知道为什么GLSurfaceView.Renderer类中的onDraw()方法没有被调用。
如果有人能查看下面的代码并指出我做错了什么,那就太好了。
public class MainActivity extends Activity {

    private GLSurfaceView mGLView;
    int x,y,w,h;
    Display disp;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // ToDo add your GUI initialization code here

        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) {
        // Do nothing special.
    }

    public void onSurfaceChanged(GL10 gl, int w, int h) {
        //gl.glViewport(0, 0, w, h);
    }

    public void onDrawFrame(GL10 gl) {
        //gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        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++)

         {//remember, that OpenGL bitmap is incompatible with Android bitmap

          //and so, some correction need.

              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)

                        {

                                // TODO Auto-generated catch block

                                e.printStackTrace();

                        }

                        try

                        {

                                fos.close();

                        }

                        catch (IOException e)

                        {

                                // TODO Auto-generated catch block

                                e.printStackTrace();

                        }



                }

                catch (FileNotFoundException e)

                {

                        // TODO Auto-generated catch block

                        e.printStackTrace();

                }
                catch(IOException e)
                {
                    e.printStackTrace();
                }

    }
}

}

请有人帮助我。 我刚刚开始学习如何在安卓上工作。

1个回答

1
尽管您创建了SurfaceView,但您没有将其设置为当前的ContentView。
setContentView(mGLView);

此外,您正在每一帧创建屏幕截图,这非常低效,可能不是您想要的。

谢谢,kru。但我几天前就已经解决了这个问题。还有一件事我想分享,我想要截取整个屏幕的截图。这种方法只能截取视图的截图,所以这种方法没有帮助。此外,我已经浏览了几天的网络,得出结论,要想截取整个屏幕的截图,我们不可避免地需要root手机。如果您有什么建议,可以在无需root手机的情况下完成任务,那就太好了,谢谢。 - ujjawal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接