TextureViewActivity.java revision 8f0095cd33558e9cc8a440047908e53b68906f5f
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.ObjectAnimator;
20import android.animation.ValueAnimator;
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.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
37    @Override
38    protected void onCreate(Bundle savedInstanceState) {
39        super.onCreate(savedInstanceState);
40
41        mTextureView = new TextureView(this);
42        mTextureView.setSurfaceTextureListener(this);
43
44        setContentView(mTextureView, new FrameLayout.LayoutParams(500, 400, Gravity.CENTER));
45    }
46
47    @Override
48    protected void onDestroy() {
49        super.onDestroy();
50
51        mCamera.stopPreview();
52        mCamera.release();
53    }
54
55    @Override
56    public void onSurfaceTextureAvailable(SurfaceTexture surface) {
57        mCamera = Camera.open();
58
59        try {
60            mCamera.setPreviewTexture(surface);
61        } catch (IOException t) {
62            android.util.Log.e("TextureView", "Cannot set preview texture target!", t);
63        }
64
65        mCamera.startPreview();
66
67        mTextureView.setCameraDistance(5000);
68
69        ObjectAnimator animator = ObjectAnimator.ofFloat(mTextureView, "rotationY", 0.0f, 360.0f);
70        animator.setRepeatMode(ObjectAnimator.REVERSE);
71        animator.setRepeatCount(ObjectAnimator.INFINITE);
72        animator.setDuration(4000);
73        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
74            @Override
75            public void onAnimationUpdate(ValueAnimator animation) {
76                ((View) mTextureView.getParent()).invalidate();
77            }
78        });
79        animator.start();
80
81        animator = ObjectAnimator.ofFloat(mTextureView, "alpha", 1.0f, 0.0f);
82        animator.setRepeatMode(ObjectAnimator.REVERSE);
83        animator.setRepeatCount(ObjectAnimator.INFINITE);
84        animator.setDuration(4000);
85        animator.start();
86    }
87
88    @Override
89    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
90        // Ignored, the Camera does all the work for us
91    }
92}
93