1 /* 2 * Copyright 2022 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 17 package com.android.test.hwui; 18 19 import android.app.Activity; 20 import android.graphics.Canvas; 21 import android.graphics.Color; 22 import android.os.Bundle; 23 import android.view.SurfaceHolder; 24 import android.view.SurfaceHolder.Callback; 25 import android.view.SurfaceView; 26 import android.view.View; 27 import android.widget.Button; 28 import android.widget.LinearLayout; 29 import android.widget.RelativeLayout; 30 import android.widget.SeekBar; 31 import android.widget.TextView; 32 33 public class SurfaceViewAlphaActivity extends Activity implements Callback { 34 SurfaceView mSurfaceView; 35 36 private enum ZOrder { 37 ABOVE, 38 BELOW 39 } 40 41 private float mAlpha = 127f / 255f; 42 private ZOrder mZOrder = ZOrder.BELOW; 43 44 getAlphaText()45 private String getAlphaText() { 46 return "Alpha: " + mAlpha; 47 } 48 toggleZOrder()49 private void toggleZOrder() { 50 if (ZOrder.ABOVE.equals(mZOrder)) { 51 mZOrder = ZOrder.BELOW; 52 } else { 53 mZOrder = ZOrder.ABOVE; 54 } 55 } 56 57 // Overlaps a blue view on the left, then the SurfaceView in the center, then a blue view on the 58 // right. overlapViews(SurfaceView view, LinearLayout parent)59 private void overlapViews(SurfaceView view, LinearLayout parent) { 60 float density = getResources().getDisplayMetrics().density; 61 int surfaceViewSize = (int) (200 * density); 62 int blueViewSize = (int) (surfaceViewSize * 2 / 3f); 63 int totalSize = (int) (surfaceViewSize * 5 / 3f); 64 65 RelativeLayout overlapLayout = new RelativeLayout(this); 66 67 RelativeLayout.LayoutParams leftViewLayoutParams = new RelativeLayout.LayoutParams( 68 blueViewSize, surfaceViewSize); 69 leftViewLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 70 71 View leftBlueView = new View(this); 72 leftBlueView.setBackgroundColor(Color.BLUE); 73 overlapLayout.addView(leftBlueView, leftViewLayoutParams); 74 75 RelativeLayout.LayoutParams sVLayoutParams = new RelativeLayout.LayoutParams( 76 surfaceViewSize, surfaceViewSize); 77 sVLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT); 78 overlapLayout.addView(view, sVLayoutParams); 79 80 RelativeLayout.LayoutParams rightViewLayoutParams = new RelativeLayout.LayoutParams( 81 blueViewSize, surfaceViewSize); 82 rightViewLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 83 84 View rightBlueView = new View(this); 85 rightBlueView.setBackgroundColor(Color.BLUE); 86 overlapLayout.addView(rightBlueView, rightViewLayoutParams); 87 88 parent.addView(overlapLayout, new LinearLayout.LayoutParams( 89 totalSize, surfaceViewSize)); 90 } 91 92 @Override onCreate(Bundle savedInstanceState)93 protected void onCreate(Bundle savedInstanceState) { 94 super.onCreate(savedInstanceState); 95 96 mSurfaceView = new SurfaceView(this); 97 mSurfaceView.getHolder().addCallback(this); 98 mSurfaceView.setAlpha(mAlpha); 99 100 LinearLayout content = new LinearLayout(this); 101 content.setOrientation(LinearLayout.VERTICAL); 102 103 TextView alphaText = new TextView(this); 104 alphaText.setText(getAlphaText()); 105 106 SeekBar alphaToggle = new SeekBar(this); 107 alphaToggle.setMin(0); 108 alphaToggle.setMax(255); 109 alphaToggle.setProgress(Math.round(mAlpha * 255)); 110 alphaToggle.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 111 @Override 112 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 113 mAlpha = progress / 255f; 114 alphaText.setText(getAlphaText()); 115 mSurfaceView.setAlpha(mAlpha); 116 } 117 118 @Override 119 public void onStartTrackingTouch(SeekBar seekBar) { 120 121 } 122 123 @Override 124 public void onStopTrackingTouch(SeekBar seekBar) { 125 126 } 127 }); 128 129 content.addView(alphaText, new LinearLayout.LayoutParams( 130 LinearLayout.LayoutParams.WRAP_CONTENT, 131 LinearLayout.LayoutParams.WRAP_CONTENT)); 132 133 content.addView(alphaToggle, new LinearLayout.LayoutParams( 134 LinearLayout.LayoutParams.MATCH_PARENT, 135 LinearLayout.LayoutParams.WRAP_CONTENT)); 136 137 Button button = new Button(this); 138 button.setText("Z " + mZOrder.toString()); 139 button.setOnClickListener(v -> { 140 toggleZOrder(); 141 mSurfaceView.setZOrderOnTop(ZOrder.ABOVE.equals(mZOrder)); 142 button.setText("Z " + mZOrder.toString()); 143 }); 144 145 content.addView(button, new LinearLayout.LayoutParams( 146 LinearLayout.LayoutParams.WRAP_CONTENT, 147 LinearLayout.LayoutParams.WRAP_CONTENT)); 148 149 overlapViews(mSurfaceView, content); 150 151 setContentView(content); 152 } 153 154 @Override surfaceCreated(SurfaceHolder holder)155 public void surfaceCreated(SurfaceHolder holder) { 156 } 157 158 @Override surfaceChanged(SurfaceHolder holder, int format, int width, int height)159 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 160 Canvas canvas = holder.lockCanvas(); 161 canvas.drawColor(Color.RED); 162 holder.unlockCanvasAndPost(canvas); 163 } 164 165 @Override surfaceDestroyed(SurfaceHolder holder)166 public void surfaceDestroyed(SurfaceHolder holder) { 167 } 168 } 169