TextureViewActivity.java revision ec19b4a764d512091a780fc93ced567dfbf80914
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.test.hwui; 18 19import android.app.Activity; 20import android.graphics.SurfaceTexture; 21import android.hardware.Camera; 22import android.os.Bundle; 23import android.view.Gravity; 24import android.view.TextureView; 25import android.view.View; 26import android.widget.Button; 27import android.widget.FrameLayout; 28 29import java.io.IOException; 30 31@SuppressWarnings({"UnusedDeclaration"}) 32public class TextureViewActivity extends Activity implements TextureView.SurfaceTextureListener { 33 private Camera mCamera; 34 private TextureView mTextureView; 35 private FrameLayout mContent; 36 37 @Override 38 protected void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 41 mContent = new FrameLayout(this); 42 43 mTextureView = new TextureView(this); 44 mTextureView.setSurfaceTextureListener(this); 45 46 Button button = new Button(this); 47 button.setText("Remove/Add"); 48 button.setOnClickListener(new View.OnClickListener() { 49 private boolean mAdded = true; 50 51 @Override 52 public void onClick(View v) { 53 if (mAdded) { 54 mContent.removeView(mTextureView); 55 } else { 56 mContent.addView(mTextureView); 57 } 58 mAdded = !mAdded; 59 } 60 }); 61 62 mContent.addView(mTextureView, new FrameLayout.LayoutParams( 63 FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, 64 Gravity.CENTER)); 65 mContent.addView(button, new FrameLayout.LayoutParams( 66 FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, 67 Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM)); 68 setContentView(mContent); 69 } 70 71 @Override 72 public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { 73 mCamera = Camera.open(); 74 Camera.Size previewSize = mCamera.getParameters().getPreviewSize(); 75 mTextureView.setLayoutParams(new FrameLayout.LayoutParams( 76 previewSize.width, previewSize.height, Gravity.CENTER)); 77 78 try { 79 mCamera.setPreviewTexture(surface); 80 } catch (IOException t) { 81 android.util.Log.e("TextureView", "Cannot set preview texture target!", t); 82 } 83 84 mCamera.startPreview(); 85 86 mTextureView.setCameraDistance(5000); 87 } 88 89 @Override 90 public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { 91 // Ignored, the Camera does all the work for us 92 } 93 94 @Override 95 public void onSurfaceTextureDestroyed(SurfaceTexture surface) { 96 mCamera.stopPreview(); 97 mCamera.release(); 98 } 99 100 @Override 101 public void onSurfaceTextureUpdated(SurfaceTexture surface) { 102 // Ignored 103 } 104} 105