Android Surface - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android Surface class.

This code example shows how to use the following methods:lockCanvas, unlockCanvasAndPost, hide, show
        final int dh = mLastDH;
 
        Rect dirty = new Rect(0, 0, dw, dh);
        Canvas c = null;
        try { 
            c = mSurface.lockCanvas(dirty);
        } catch (IllegalArgumentException e) {
        } catch (Surface.OutOfResourcesException e) {
        } 
        if (c == null) {
            return; 
        } 
 
        // Top 
        c.clipRect(new Rect(0, 0, dw, mThickness), Region.Op.REPLACE);
        c.drawColor(Color.RED);
        // Left 
        c.clipRect(new Rect(0, 0, mThickness, dh), Region.Op.REPLACE);
        c.drawColor(Color.RED);
        // Right 

8
CodeRank
This code example shows how to use the following methods:lockCanvas, unlockCanvasAndPost
        final int dh = mLastDH;
 
        Rect dirty = new Rect(0, 0, dw, dh);
        Canvas c = null;
        try { 
            c = mSurface.lockCanvas(dirty);
        } catch (IllegalArgumentException e) {
        } catch (Surface.OutOfResourcesException e) {
        } 
        if (c == null) {
            return; 
        } 
 
        // Top 
        c.clipRect(new Rect(0, 0, dw, mThickness), Region.Op.REPLACE);
        c.drawColor(Color.RED);
        // Left 
        c.clipRect(new Rect(0, 0, mThickness, dh), Region.Op.REPLACE);
        c.drawColor(Color.RED);
        // Right 

7
CodeRank
This code example shows how to use the following methods:lockCanvas, sleep
                mTmpDirty.set(mSurfaceFrame);
                dirty = mTmpDirty;
            } 
 
            try { 
                c = mSurface.lockCanvas(dirty);
            } catch (Exception e) {
                Log.e(TAG, "Exception locking surface", e);
            } 
        } 
 
        if (DEBUG) Log.i(TAG, "Returned canvas: " + c);
        if (c != null) {
            mLastLockTime = SystemClock.uptimeMillis();
            return c;
        } 
         
        // If the Surface is not ready to be drawn, then return null, 
        // but throttle calls to this function so it isn't called more 
        // than every 100ms. 

8
CodeRank
This code example shows how to use the following methods:lockCanvas, sleep
                mTmpDirty.set(mSurfaceFrame);
                dirty = mTmpDirty;
            } 
 
            try { 
                c = mSurface.lockCanvas(dirty);
            } catch (Exception e) {
                Log.e(TAG, "Exception locking surface", e);
            } 
        } 
 
        if (DEBUG) Log.i(TAG, "Returned canvas: " + c);
        if (c != null) {
            mLastLockTime = SystemClock.uptimeMillis();
            return c;
        } 
         
        // If the Surface is not ready to be drawn, then return null, 
        // but throttle calls to this function so it isn't called more 
        // than every 100ms. 

6
CodeRank
This code example shows how to use the following methods:setMatrix, setPosition
 
        void setMatrix(Matrix matrix) {
            mTmpMatrix.setTranslate(left, top);
            mTmpMatrix.postConcat(matrix);
            mTmpMatrix.getValues(mTmpFloats);
            surface.setPosition(mTmpFloats[Matrix.MTRANS_X],
                    mTmpFloats[Matrix.MTRANS_Y]);
            surface.setMatrix(
                    mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
                    mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
            if (false) { 
                Slog.i(WindowManagerService.TAG, "Black Surface @ (" + left + "," + top + "): ("
                        + mTmpFloats[Matrix.MTRANS_X] + ","
                        + mTmpFloats[Matrix.MTRANS_Y] + ") matrix=["
                        + mTmpFloats[Matrix.MSCALE_X] + ","
                        + mTmpFloats[Matrix.MSCALE_Y] + "]["
                        + mTmpFloats[Matrix.MSKEW_X] + ","
                        + mTmpFloats[Matrix.MSKEW_Y] + "]");
            } 
        } 
6
CodeRank
This code example shows how to use the following methods:setSize
 
    void positionSurface(int dw, int dh) {
        if (mLastDW != dw || mLastDH != dh) {
            mLastDW = dw;
            mLastDH = dh;
            mSurface.setSize(dw, dh);
            mDrawNeeded = true;
        } 
    } 
 
    void drawIfNeeded() { 
        if (mDrawNeeded) {
            final int dw = mLastDW;
            final int dh = mLastDH;
 
            mDrawNeeded = false;
            Rect dirty = new Rect(0, 0, dw, dh);
            Canvas c = null;
            try { 
                c = mSurface.lockCanvas(dirty);
6
CodeRank
This code example shows how to use the following methods:release
     */ 
    @SuppressWarnings("unused") 
    @CalledByNative 
    static void establishSurfaceTexturePeer(int pid, int type, SurfaceTexture st, int primaryID,
            int secondaryID) {
        Surface surface = new Surface(st);
        SandboxedProcessLauncher.establishSurfacePeer(pid, type, surface, primaryID, secondaryID);
 
        // We need to explicitly release the native resource of our newly created surface 
        // or the Surface class will print a warning message to the log in its finalizer. 
        // This should be ok to do since our caller is responsible for retaining a 
        // reference to the SurfaceTexture that is being sent across processes and the 
        // receiving end should have retained a reference before the binder call finished. 
        surface.release();
    } 
 
    /** 
     * @param context The application {@link Context}. 
     * @return Whether or not the device should be given a tablet UI. 
     */ 
5
CodeRank
This code example shows how to use the following methods:setAlpha, setMatrix, setPosition
        return delta;
    } 
 
    void setSnapshotTransform(Matrix matrix, float alpha) {
        matrix.getValues(mTmpFloats);
        mSurface.setPosition((int)mTmpFloats[Matrix.MTRANS_X],
                (int)mTmpFloats[Matrix.MTRANS_Y]);
        mSurface.setMatrix(
                mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
                mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
        mSurface.setAlpha(alpha);
        if (DEBUG) {
            float[] srcPnts = new float[] { 0, 0, mWidth, mHeight };
            float[] dstPnts = new float[4];
            matrix.mapPoints(dstPnts, srcPnts);
            Slog.i(TAG, "Original  : (" + srcPnts[0] + "," + srcPnts[1]
                    + ")-(" + srcPnts[2] + "," + srcPnts[3] + ")");
            Slog.i(TAG, "Transformed: (" + dstPnts[0] + "," + dstPnts[1]
                    + ")-(" + dstPnts[2] + "," + dstPnts[3] + ")");
        } 
5
CodeRank
This code example shows how to use the following methods:release
                GLES20.GL_LINEAR);
 
        _mySurfaceTexture = new SurfaceTexture(_myTextureId[0]);
        _mySurfaceTexture.setOnFrameAvailableListener(this);
 
        Surface mySurface = new Surface(_mySurfaceTexture);
        _myMediaPlayer = new MediaPlayer();
        _myMediaPlayer.setSurface(mySurface);
        mySurface.release();
        // TODO: switch to async 
        //_myMediaPlayer.setOnPreparedListener(this); 
    } 
 
    // Called when MediaPlayer is ready 
    public void onPrepared(MediaPlayer player) {
        //player.start(); 
 
    } 
 
    synchronized public void onFrameAvailable(SurfaceTexture surface) {
5
CodeRank
This code example shows how to use the following methods:setRenderer
 
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);
 
        mGLSurfaceView = new GLSurfaceView(this);
 
        // Set an OpenGL ES 1.x-compatible renderer. 
        mGLSurfaceView.setRenderer(new GLBenchRenderer(this));
        setContentView(mGLSurfaceView);
 
    } 
 
    private GLSurfaceView mGLSurfaceView;
} 
7
CodeRank

Related Android Surface Questions & Answers:

See Code Examples for Android Surface Methods: