CanvasTextureViewActivity.java revision 3c2c9e4e0dde0f3a9c27ccff5313cfbeb4504f8e
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.Canvas;
21import android.graphics.Paint;
22import android.graphics.PorterDuff;
23import android.graphics.SurfaceTexture;
24import android.os.Bundle;
25import android.view.Gravity;
26import android.view.TextureView;
27import android.widget.FrameLayout;
28
29@SuppressWarnings({"UnusedDeclaration"})
30public class CanvasTextureViewActivity extends Activity
31        implements TextureView.SurfaceTextureListener {
32    private TextureView mTextureView;
33    private CanvasTextureViewActivity.RenderingThread mThread;
34
35    @Override
36    protected void onCreate(Bundle savedInstanceState) {
37        super.onCreate(savedInstanceState);
38
39        FrameLayout content = new FrameLayout(this);
40
41        mTextureView = new TextureView(this);
42        mTextureView.setSurfaceTextureListener(this);
43        mTextureView.setOpaque(false);
44
45        content.addView(mTextureView, new FrameLayout.LayoutParams(500, 500, Gravity.CENTER));
46        setContentView(content);
47    }
48
49    @Override
50    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
51        mThread = new RenderingThread(mTextureView);
52        mThread.start();
53    }
54
55    @Override
56    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
57        // Ignored
58    }
59
60    @Override
61    public void onSurfaceTextureDestroyed(SurfaceTexture surface) {
62        if (mThread != null) mThread.stopRendering();
63    }
64
65    @Override
66    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
67        // Ignored
68    }
69
70    private static class RenderingThread extends Thread {
71        private final TextureView mSurface;
72        private volatile boolean mRunning = true;
73
74        public RenderingThread(TextureView surface) {
75            mSurface = surface;
76        }
77
78        @Override
79        public void run() {
80            float x = 0.0f;
81            float y = 0.0f;
82            float speedX = 5.0f;
83            float speedY = 3.0f;
84
85            Paint paint = new Paint();
86            paint.setColor(0xff00ff00);
87
88            while (mRunning && !Thread.interrupted()) {
89                final Canvas canvas = mSurface.lockCanvas(null);
90                try {
91                    canvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
92                    canvas.drawRect(x, y, x + 20.0f, y + 20.0f, paint);
93                } finally {
94                    mSurface.unlockCanvasAndPost(canvas);
95                }
96
97                if (x + 20.0f + speedX >= mSurface.getWidth() || x + speedX <= 0.0f) {
98                    speedX = -speedX;
99                }
100                if (y + 20.0f + speedY >= mSurface.getHeight() || y + speedY <= 0.0f) {
101                    speedY = -speedY;
102                }
103
104                x += speedX;
105                y += speedY;
106
107                try {
108                    Thread.sleep(15);
109                } catch (InterruptedException e) {
110                    // Interrupted
111                }
112            }
113        }
114
115        void stopRendering() {
116            interrupt();
117            mRunning = false;
118        }
119    }
120}
121