CanvasTextureViewActivity.java revision 402f05530352f34d5320c2d23be43c274d97c4e2
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 boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
62        if (mThread != null) mThread.stopRendering();
63        return true;
64    }
65
66    @Override
67    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
68        // Ignored
69    }
70
71    private static class RenderingThread extends Thread {
72        private final TextureView mSurface;
73        private volatile boolean mRunning = true;
74
75        public RenderingThread(TextureView surface) {
76            mSurface = surface;
77        }
78
79        @Override
80        public void run() {
81            float x = 0.0f;
82            float y = 0.0f;
83            float speedX = 5.0f;
84            float speedY = 3.0f;
85
86            Paint paint = new Paint();
87            paint.setColor(0xff00ff00);
88
89            while (mRunning && !Thread.interrupted()) {
90                final Canvas canvas = mSurface.lockCanvas(null);
91                try {
92                    canvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
93                    canvas.drawRect(x, y, x + 20.0f, y + 20.0f, paint);
94                } finally {
95                    mSurface.unlockCanvasAndPost(canvas);
96                }
97
98                if (x + 20.0f + speedX >= mSurface.getWidth() || x + speedX <= 0.0f) {
99                    speedX = -speedX;
100                }
101                if (y + 20.0f + speedY >= mSurface.getHeight() || y + speedY <= 0.0f) {
102                    speedY = -speedY;
103                }
104
105                x += speedX;
106                y += speedY;
107
108                try {
109                    Thread.sleep(15);
110                } catch (InterruptedException e) {
111                    // Interrupted
112                }
113            }
114        }
115
116        void stopRendering() {
117            interrupt();
118            mRunning = false;
119        }
120    }
121}
122