TextureViewActivity.java revision 4a5a71518a71a44dbccb3af9a08b82056ea748b3
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.app.Activity;
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 AnimatorSet mAnimatorSet;
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                    if (mAnimatorSet != null) mAnimatorSet.cancel();
57                    mContent.removeView(mTextureView);
58                } else {
59                    mContent.addView(mTextureView);
60                }
61                mAdded = !mAdded;
62            }
63        });
64
65        mContent.addView(mTextureView, new FrameLayout.LayoutParams(500, 400, Gravity.CENTER));
66        mContent.addView(button, new FrameLayout.LayoutParams(
67                FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT,
68                Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM));
69        setContentView(mContent);
70    }
71
72    @Override
73    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
74        mCamera = Camera.open();
75
76        try {
77            mCamera.setPreviewTexture(surface);
78        } catch (IOException t) {
79            android.util.Log.e("TextureView", "Cannot set preview texture target!", t);
80        }
81
82        mCamera.startPreview();
83
84        mTextureView.setCameraDistance(5000);
85
86//        ObjectAnimator rotationY = ObjectAnimator.ofFloat(mTextureView, "rotationY", 0.0f, 360.0f);
87//        rotationY.setRepeatMode(ObjectAnimator.REVERSE);
88//        rotationY.setRepeatCount(ObjectAnimator.INFINITE);
89//        rotationY.setDuration(4000);
90
91//        ObjectAnimator alpha = ObjectAnimator.ofFloat(mTextureView, "alpha", 1.0f, 0.0f);
92//        alpha.setRepeatMode(ObjectAnimator.REVERSE);
93//        alpha.setRepeatCount(ObjectAnimator.INFINITE);
94//        alpha.setDuration(4000);
95
96//        mAnimatorSet = new AnimatorSet();
97//        mAnimatorSet.play(alpha).with(rotationY);
98//        mAnimatorSet.start();
99    }
100
101    @Override
102    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
103        // Ignored, the Camera does all the work for us
104    }
105
106    @Override
107    public void onSurfaceTextureDestroyed(SurfaceTexture surface) {
108        mCamera.stopPreview();
109        mCamera.release();
110    }
111
112    @Override
113    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
114        // Ignored
115    }
116}
117