Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | package com.android.transitiontests; |
| 17 | |
| 18 | import android.app.Activity; |
| 19 | import android.content.Context; |
| 20 | import android.graphics.Canvas; |
| 21 | import android.graphics.Color; |
| 22 | import android.graphics.SurfaceTexture; |
| 23 | import android.os.Bundle; |
| 24 | import android.util.AttributeSet; |
| 25 | import android.view.SurfaceHolder; |
| 26 | import android.view.SurfaceView; |
| 27 | import android.view.TextureView; |
| 28 | import android.view.View; |
| 29 | import android.view.ViewGroup; |
| 30 | import android.view.transition.Crossfade; |
| 31 | import android.view.transition.Move; |
| 32 | import android.view.transition.Scene; |
| 33 | import android.view.transition.TransitionGroup; |
| 34 | import android.view.transition.TransitionManager; |
| 35 | import android.widget.Button; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 36 | |
| 37 | import static android.widget.LinearLayout.LayoutParams; |
| 38 | |
| 39 | public class SurfaceAndTextureViews extends Activity { |
| 40 | |
| 41 | SimpleView mView; |
| 42 | SimpleSurfaceView mSurfaceView; |
| 43 | SimpleTextureView mTextureView; |
| 44 | private static final int SMALL_SIZE = 200; |
| 45 | |
| 46 | @Override |
| 47 | public void onCreate(Bundle savedInstanceState) { |
| 48 | super.onCreate(savedInstanceState); |
| 49 | setContentView(R.layout.surface_texture_views); |
| 50 | |
| 51 | final ViewGroup container = (ViewGroup) findViewById(R.id.container); |
| 52 | Button toggleButton = (Button) findViewById(R.id.toggleButton); |
| 53 | |
| 54 | mView = new SimpleView(this); |
| 55 | mView.setId(0); |
| 56 | mView.setLayoutParams(new LayoutParams(SMALL_SIZE, SMALL_SIZE)); |
| 57 | container.addView(mView); |
| 58 | |
| 59 | mSurfaceView = new SimpleSurfaceView(this); |
| 60 | mSurfaceView.setId(1); |
| 61 | mSurfaceView.setLayoutParams(new LayoutParams(SMALL_SIZE, SMALL_SIZE)); |
| 62 | container.addView(mSurfaceView); |
| 63 | |
| 64 | mTextureView = new SimpleTextureView(this); |
| 65 | mTextureView.setId(2); |
| 66 | mTextureView.setLayoutParams(new LayoutParams(SMALL_SIZE, SMALL_SIZE)); |
| 67 | container.addView(mTextureView); |
| 68 | |
| 69 | final TransitionGroup transition = new TransitionGroup(); |
| 70 | transition.addTransitions(new Move(), new Crossfade().setTargetIds(0, 1, 2)); |
| 71 | |
| 72 | toggleButton.setOnClickListener(new View.OnClickListener() { |
| 73 | @Override |
| 74 | public void onClick(View v) { |
| 75 | Scene newScene = new Scene(container); |
| 76 | newScene.setEnterAction(new Runnable() { |
| 77 | @Override |
| 78 | public void run() { |
| 79 | if (mView.getWidth() <= SMALL_SIZE) { |
| 80 | mView.setLayoutParams(new LayoutParams(SMALL_SIZE * 2, SMALL_SIZE)); |
| 81 | mSurfaceView.setLayoutParams(new LayoutParams(SMALL_SIZE * 2, SMALL_SIZE)); |
| 82 | mTextureView.setLayoutParams(new LayoutParams(SMALL_SIZE * 2, SMALL_SIZE)); |
| 83 | mView.mColor = SimpleView.LARGE_COLOR; |
| 84 | mSurfaceView.mColor = SimpleSurfaceView.LARGE_COLOR; |
| 85 | mTextureView.mColor = SimpleTextureView.LARGE_COLOR; |
| 86 | } else { |
| 87 | mView.setLayoutParams(new LayoutParams(SMALL_SIZE, SMALL_SIZE)); |
| 88 | mSurfaceView.setLayoutParams(new LayoutParams(SMALL_SIZE, SMALL_SIZE)); |
| 89 | mTextureView.setLayoutParams(new LayoutParams(SMALL_SIZE, SMALL_SIZE)); |
| 90 | mView.mColor = SimpleView.SMALL_COLOR; |
| 91 | mSurfaceView.mColor = SimpleSurfaceView.SMALL_COLOR; |
| 92 | mTextureView.mColor = SimpleTextureView.SMALL_COLOR; |
| 93 | } |
| 94 | } |
| 95 | }); |
| 96 | TransitionManager.go(newScene, transition); |
| 97 | } |
| 98 | }); |
| 99 | |
| 100 | } |
| 101 | |
| 102 | static private class SimpleView extends View { |
| 103 | static final int SMALL_COLOR = Color.BLUE; |
| 104 | static final int LARGE_COLOR = Color.YELLOW; |
| 105 | int mColor = SMALL_COLOR; |
| 106 | |
| 107 | private SimpleView(Context context) { |
| 108 | super(context); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | protected void onDraw(Canvas canvas) { |
| 113 | canvas.drawColor(mColor); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | static private class SimpleSurfaceView extends SurfaceView implements SurfaceHolder.Callback { |
| 118 | |
| 119 | static final int SMALL_COLOR = Color.GREEN; |
| 120 | static final int LARGE_COLOR = Color.GRAY; |
| 121 | int mColor = SMALL_COLOR; |
| 122 | SurfaceHolder mHolder = null; |
| 123 | |
| 124 | private SimpleSurfaceView(Context context) { |
| 125 | super(context); |
| 126 | SurfaceHolder holder = getHolder(); |
| 127 | holder.addCallback(this); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | @Override |
| 132 | public void surfaceCreated(SurfaceHolder holder) { |
| 133 | System.out.println("surfaceCreated"); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { |
| 138 | System.out.println("surfaceChanged: w h = " + width + ", " + height); |
| 139 | Canvas canvas = holder.lockCanvas(); |
| 140 | canvas.drawColor(mColor); |
| 141 | holder.unlockCanvasAndPost(canvas); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void surfaceDestroyed(SurfaceHolder holder) { |
| 146 | System.out.println("surfaceDestroyed"); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | static private class SimpleTextureView extends TextureView implements TextureView.SurfaceTextureListener { |
| 151 | |
| 152 | static final int SMALL_COLOR = Color.RED; |
| 153 | static final int LARGE_COLOR = Color.CYAN; |
| 154 | int mColor = SMALL_COLOR; |
| 155 | |
| 156 | private SimpleTextureView(Context context) { |
| 157 | super(context); |
| 158 | setSurfaceTextureListener(this); |
| 159 | } |
| 160 | |
| 161 | private SimpleTextureView(Context context, AttributeSet attrs) { |
| 162 | super(context, attrs); |
| 163 | setSurfaceTextureListener(this); |
| 164 | } |
| 165 | |
| 166 | private SimpleTextureView(Context context, AttributeSet attrs, int defStyle) { |
| 167 | super(context, attrs, defStyle); |
| 168 | setSurfaceTextureListener(this); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { |
| 173 | System.out.println("SurfaceTexture available"); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { |
| 178 | System.out.println("SurfaceTexture size changed to " + width + ", " + height); |
| 179 | Canvas canvas = lockCanvas(); |
| 180 | canvas.drawColor(mColor); |
| 181 | unlockCanvasAndPost(canvas); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public void onSurfaceTextureUpdated(SurfaceTexture surface) { |
| 191 | System.out.println("SurfaceTexture updated"); |
| 192 | } |
| 193 | } |
| 194 | } |