TextureViewActivity.java revision 302a9df1d50373c82923bb84ff665dfce584fb22
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.Matrix;
21import android.graphics.SurfaceTexture;
22import android.hardware.Camera;
23import android.os.Bundle;
24import android.view.Gravity;
25import android.view.TextureView;
26import android.view.View;
27import android.widget.Button;
28import android.widget.FrameLayout;
29
30import java.io.IOException;
31
32@SuppressWarnings({"UnusedDeclaration"})
33public class TextureViewActivity extends Activity implements TextureView.SurfaceTextureListener {
34    private Camera mCamera;
35    private TextureView mTextureView;
36    private FrameLayout mContent;
37    private Matrix mMatrix = new Matrix();
38
39    @Override
40    protected void onCreate(Bundle savedInstanceState) {
41        super.onCreate(savedInstanceState);
42
43        mContent = new FrameLayout(this);
44
45        mTextureView = new TextureView(this);
46        mTextureView.setSurfaceTextureListener(this);
47
48        Button button = new Button(this);
49        button.setText("Remove/Add");
50        button.setOnClickListener(new View.OnClickListener() {
51            private boolean mAdded = true;
52
53            @Override
54            public void onClick(View v) {
55                if (mAdded) {
56                    mContent.removeView(mTextureView);
57                } else {
58                    mContent.addView(mTextureView);
59                }
60                mAdded = !mAdded;
61            }
62        });
63
64        mContent.addView(mTextureView, new FrameLayout.LayoutParams(
65                FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT,
66                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        Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
77        mTextureView.setLayoutParams(new FrameLayout.LayoutParams(
78                previewSize.width, previewSize.height, Gravity.CENTER));
79
80        try {
81            mCamera.setPreviewTexture(surface);
82        } catch (IOException t) {
83            android.util.Log.e("TextureView", "Cannot set preview texture target!", t);
84        }
85
86        mCamera.startPreview();
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 boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
96        mCamera.stopPreview();
97        mCamera.release();
98        return true;
99    }
100
101    @Override
102    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
103        // Ignored
104    }
105}
106