How Can I Make A Surfaceview Larger Than The Screen?
Solution 1:
//Activity classpublicclassCameraActivityextendsActivityimplementsSurfaceListener {
privatestatic final StringTAG = "CameraActivity";
Camera mCamera;
CameraPreview mPreview;
privateFrameLayout mCameraPreview;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_camera);
mCamera = getCameraInstance();
mPreview = newCameraPreview(this, mCamera);
mCameraPreview = (FrameLayout) findViewById(R.id.camera_preview);
mCameraPreview.addView(mPreview);
}
@OverrideprotectedvoidonPause() {
super.onPause();
releaseCamera();
}
privateCameragetCameraInstance() {
Camera camera = null;
try {
camera = Camera.open();
} catch (Exception e) {
}
return camera;
}
privatevoidreleaseCamera() {
if (null != mCamera) {
mCamera.release();
}
mCamera = null;
}
@OverridepublicvoidsurfaceCreated() {
//Change these mate
int width = 1000;
int height = 1000;
// Set parent window paramsgetWindow().setLayout(width, height);
RelativeLayout.LayoutParams params = newRelativeLayout.LayoutParams(
width, height);
mCameraPreview.setLayoutParams(params);
mCameraPreview.requestLayout();
}
}
// Preview classpublicclassCameraPreviewextendsSurfaceViewimplementsSurfaceHolder.Callback {
privatestatic final StringTAG = "CameraPreview";
Context mContext;
Camera mCamera;
SurfaceHolder mHolder;
publicinterfaceSurfaceListener{
publicvoidsurfaceCreated();
}
SurfaceListener listener;
publicCameraPreview(Context context, Camera camera) {
super(context);
mContext = context;
listener = (SurfaceListener)mContext;
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
// Required prior 3.0 HC
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@OverridepublicvoidsurfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
Parameters params = mCamera.getParameters();
//Change parameters here
mCamera.setParameters(params);
mCamera.startPreview();
listener.surfaceCreated();
} catch (Exception e) {
// TODO: handle exception
}
}
publicvoidsurfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
publicvoidsurfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.// Make sure to stop the preview before resizing or reformatting it.Log.i(TAG, "Surface changed called");
if (mHolder.getSurface() == null) {
// preview surface does not existreturn;
}
// stop preview before making changestry {
mCamera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or// reformatting changes here
mCamera.setDisplayOrientation(90);
// start preview with new settingstry {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e) {
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
}
//Layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><FrameLayoutandroid:id="@+id/camera_preview"android:layout_width="300dp"android:layout_height="400dp"android:layout_centerHorizontal="true"android:paddingTop="10dp" ></FrameLayout></RelativeLayout>
Solution 2:
You can't make your surfaceView bigger than the screen. That being said there are ways around it.
I found you can adjust the size of the canvas in the SurfaceView, which will allow zooming.
publicclassDrawingThreadextendsThread {
private MagnificationView mainPanel;
private SurfaceHolder surfaceHolder;
privateboolean run;
publicDrawingThread(SurfaceHolder surface, MagnificationView panel){
surfaceHolder = surface;
mainPanel = panel;
}
public SurfaceHolder getSurfaceHolder(){
return surfaceHolder;
}
publicvoidsetRunning(boolean run){
this.run = run;
}
publicvoidrun(){
Canvas c;
while (run){
c = null;
try {
c = surfaceHolder.lockCanvas(null);
synchronized (surfaceHolder){
mainPanel.OnDraw(c);
}
} finally {
if (c != null){
surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
In the MagnificationView class add a method:
public void OnDraw(Canvas canvas){
if (canvas!=null){
canvas.save();
canvas.scale(scaleX,scaleY);
canvas.restore();
}
}
DrawingThread would be a thread you start in in your Activity. Also in your MagnificationView class override the OnTouchEvent to handle your own pinch-zoom (which will modify scaleX & scaleY.
Hope This solves your issue
Solution 3:
What you can do is to get the window and set its height:
getWindow().setLayout(1000, 1000);
This makes your window larger than the screen making your root view and consequently your surfaceview, probably contained inside a Framelayout larger than screen.
This worked for me let me know.
The above would work no matter what. What you would want to do is listen for onSurfaceCreated
event for your surface view. Then after you have the started the camera view and you are able to calculate size of your widget holding the preview, you would want to change size of the container widget.
The concept is your container widget (probably FrameLayout
) wants to grow larger than screen. The screen itself is restricted by the activity so first set size of your window,
then set size of your framelayout (it would always be shrunk to max size of windows, so set accordingly).
I do all this logic after my onSurfaceCreated
is finished I have started the preview. I listen for this event in my activity by implementing a small interface, as my Camera preview is a separate class.
Working on all API level >= 8
Solution 4:
Here's my TouchSurfaceView
's onMeasure
that performs zoom:
@OverrideprotectedvoidonMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
intwidth= MeasureSpec.getSize(widthMeasureSpec);
intheight= MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension((int) (width * scaleFactor), (int) (height * scaleFactor));
}
This properly zooms in and out depending on scaleFactor.
I haven't tested this with camera, but it works properly with MediaPlayer
(behaving as VideoView
).
Post a Comment for "How Can I Make A Surfaceview Larger Than The Screen?"