2011-01-08 23 views
3

以下是我用來使用GLSurfaceView拍攝屏幕截圖的代碼。但我不知道爲什麼GLSurfaceView.Renderer類中的onDraw()方法沒有被調用。screenshot in android

請如果有人可以看看下面的代碼,並指出我做錯了什麼。

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(); 
       } 

    } 
} 

} 

請別人幫我一把。 我剛開始學習在android上工作。

+1

OpenGL ES 1.0,1.1或2.0? – Kos 2011-01-08 19:56:56

回答

1

雖然創建surfaceview,你不將其設置爲當前內容查看

setContentView(mGLView); 

此外,要創建截圖的每一個幀,這是非常低效和可能不是什麼,你會想要..

+0

謝謝你會kru。但是我前幾天想到了。還有一件事我想分享一下,我想拍攝整個屏幕。此方法將僅拍攝視圖的屏幕截圖。所以這種方法是沒有幫助的。此外,我瀏覽了幾天的網絡,並得出結論,爲了拍攝整個屏幕的截圖,我們將不可避免地需要根植手機。如果你可以建議一些能夠完成任務而不需要根植電話的東西,那麼對你來說它會很好,謝謝。 – ujjawal 2011-01-21 05:29:37