TextureViewActivity.java revision 1ec3a58bcdd0d5fa82cf878d974d062811933ae2
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.animation.AnimatorSet; 20import android.animation.ObjectAnimator; 21import android.app.Activity; 22import android.graphics.SurfaceTexture; 23import android.hardware.Camera; 24import android.os.Bundle; 25import android.view.Gravity; 26import android.view.TextureView; 27import android.view.View; 28import android.widget.Button; 29import android.widget.FrameLayout; 30 31import java.io.IOException; 32 33@SuppressWarnings({"UnusedDeclaration"}) 34public class TextureViewActivity extends Activity implements TextureView.SurfaceTextureListener { 35 private Camera mCamera; 36 private TextureView mTextureView; 37 private FrameLayout mContent; 38 private AnimatorSet mAnimatorSet; 39 40 @Override 41 protected void onCreate(Bundle savedInstanceState) { 42 super.onCreate(savedInstanceState); 43 44 mContent = new FrameLayout(this); 45 46 mTextureView = new TextureView(this); 47 mTextureView.setSurfaceTextureListener(this); 48 49 Button button = new Button(this); 50 button.setText("Remove/Add"); 51 button.setOnClickListener(new View.OnClickListener() { 52 private boolean mAdded = true; 53 54 @Override 55 public void onClick(View v) { 56 if (mAdded) { 57 if (mAnimatorSet != null) mAnimatorSet.cancel(); 58 mContent.removeView(mTextureView); 59 } else { 60 mContent.addView(mTextureView); 61 } 62 mAdded = !mAdded; 63 } 64 }); 65 66 mContent.addView(mTextureView, new FrameLayout.LayoutParams(500, 400, Gravity.CENTER)); 67 mContent.addView(button, new FrameLayout.LayoutParams( 68 FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, 69 Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM)); 70 setContentView(mContent); 71 } 72 73 @Override 74 public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { 75 mCamera = Camera.open(); 76 77 try { 78 mCamera.setPreviewTexture(surface); 79 } catch (IOException t) { 80 android.util.Log.e("TextureView", "Cannot set preview texture target!", t); 81 } 82 83 mCamera.startPreview(); 84 85 mTextureView.setCameraDistance(5000); 86 87// ObjectAnimator rotationY = ObjectAnimator.ofFloat(mTextureView, "rotationY", 0.0f, 360.0f); 88// rotationY.setRepeatMode(ObjectAnimator.REVERSE); 89// rotationY.setRepeatCount(ObjectAnimator.INFINITE); 90// rotationY.setDuration(4000); 91 92// ObjectAnimator alpha = ObjectAnimator.ofFloat(mTextureView, "alpha", 1.0f, 0.0f); 93// alpha.setRepeatMode(ObjectAnimator.REVERSE); 94// alpha.setRepeatCount(ObjectAnimator.INFINITE); 95// alpha.setDuration(4000); 96 97// mAnimatorSet = new AnimatorSet(); 98// mAnimatorSet.play(alpha).with(rotationY); 99// mAnimatorSet.start(); 100 } 101 102 @Override 103 public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { 104 // Ignored, the Camera does all the work for us 105 } 106 107 @Override 108 public void onSurfaceTextureDestroyed(SurfaceTexture surface) { 109 mCamera.stopPreview(); 110 mCamera.release(); 111 } 112} 113