We provide complete mobile and web apps development solutions

Monday, February 25, 2013

SurfaceView

android.view.SurfaceView

 SurfaceView class provides dedicated drawing surface.Surface is Zordered. It punches a hole in the window to allow the surface to be displayed. SurfaceView takes care of placing the view at correct place.
getHolder() - Access to the surface is provided through SurfaceHolder interface which can be retrieved by calling getHolder().

Implement SurfaceCreated, SurfaceDestroyed , SurfaceChanged to discover when the surface is created, changed, destroyed.


Here is the example for SurfaceView class

public class Surface


public class SurfaceExample extends SurfaceView
{
public SurfaceExample(Context context, AttributeSet attrSet)
{
super(context, attrSet);
}
}

Download Source code 

 

package com.srinivas.hellomr;

import java.io.IOException;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.hardware.Camera;
import android.view.WindowManager;

public class MainActivity extends Activity implements SurfaceHolder.Callback {
    SurfaceHolder mSurfaceHolder;
    SurfaceView mSurfaceView;
    public Camera mCamera;
    boolean mPreviewRunning;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        mSurfaceView = (SurfaceView) findViewById(R.id.surfaceview);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        if (mPreviewRunning) {

            mCamera.stopPreview();
        }
        Camera.Parameters p = mCamera.getParameters();
        mCamera.setParameters(p);
        try {
            mCamera.setPreviewDisplay(arg0);
        } catch (IOException e) {
            e.printStackTrace();
        }
        mCamera.startPreview();
        mPreviewRunning = true;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mCamera = Camera.open();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mCamera.stopPreview();
        mPreviewRunning = false;
        mCamera.release();
    }

    Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
        public void onPictureTaken(byte[] imageData, Camera c) {

        }
    };
}

 

 activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:id="@+id/layout">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <SurfaceView
        android:id="@+id/surfaceview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

 



0 coment�rios:

Post a Comment

Online Training

Your Name :
Your Email: (required)
Your Message: (required)

Powered by Blogger.

Recent Posts

Find Us On Facebook

Popular Posts