TextureViewActivity.java revision 80429c458506485904715180d10584092a5cd082
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.view.ViewGroup;
29import android.widget.Button;
30import android.widget.FrameLayout;
31
32import java.io.IOException;
33
34@SuppressWarnings({"UnusedDeclaration"})
35public class TextureViewActivity extends Activity implements TextureView.SurfaceTextureListener {
36    private Camera mCamera;
37    private TextureView mTextureView;
38    private FrameLayout mContent;
39    private AnimatorSet mAnimatorSet;
40
41    @Override
42    protected void onCreate(Bundle savedInstanceState) {
43        super.onCreate(savedInstanceState);
44
45        mContent = new FrameLayout(this);
46
47        mTextureView = new TextureView(this);
48        mTextureView.setSurfaceTextureListener(this);
49
50        Button button = new Button(this);
51        button.setText("Remove/Add");
52        button.setOnClickListener(new View.OnClickListener() {
53            private boolean mAdded = true;
54
55            @Override
56            public void onClick(View v) {
57                if (mAdded) {
58                    if (mAnimatorSet != null) mAnimatorSet.cancel();
59                    mContent.removeView(mTextureView);
60                } else {
61                    mContent.addView(mTextureView);
62                }
63                mAdded = !mAdded;
64            }
65        });
66
67        mContent.addView(mTextureView, new FrameLayout.LayoutParams(500, 400, Gravity.CENTER));
68        mContent.addView(button, new FrameLayout.LayoutParams(
69                FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT,
70                Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM));
71        setContentView(mContent);
72    }
73
74    @Override
75    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
76        mCamera = Camera.open();
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//        ObjectAnimator rotationY = ObjectAnimator.ofFloat(mTextureView, "rotationY", 0.0f, 360.0f);
89//        rotationY.setRepeatMode(ObjectAnimator.REVERSE);
90//        rotationY.setRepeatCount(ObjectAnimator.INFINITE);
91//        rotationY.setDuration(4000);
92
93//        ObjectAnimator alpha = ObjectAnimator.ofFloat(mTextureView, "alpha", 1.0f, 0.0f);
94//        alpha.setRepeatMode(ObjectAnimator.REVERSE);
95//        alpha.setRepeatCount(ObjectAnimator.INFINITE);
96//        alpha.setDuration(4000);
97
98//        mAnimatorSet = new AnimatorSet();
99//        mAnimatorSet.play(alpha).with(rotationY);
100//        mAnimatorSet.start();
101    }
102
103    @Override
104    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
105        // Ignored, the Camera does all the work for us
106    }
107
108    @Override
109    public void onSurfaceTextureDestroyed(SurfaceTexture surface) {
110        mCamera.stopPreview();
111        mCamera.release();
112    }
113
114    @Override
115    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
116        // Ignored
117    }
118}
119