1 /*
2  * Copyright (C) 2021 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.graphics.ColorFilter;
23 import android.graphics.Paint;
24 import android.graphics.drawable.Drawable;
25 import android.os.Bundle;
26 import android.view.SurfaceHolder;
27 import android.view.SurfaceHolder.Callback;
28 import android.view.SurfaceView;
29 import android.widget.FrameLayout;
30 import android.widget.ImageView;
31 
32 public class ScrollingStretchSurfaceViewActivity extends Activity implements Callback {
33 
34     SurfaceView mVerticalSurfaceView;
35     SurfaceView mHorizontalSurfaceView;
36 
37     @Override
onCreate(Bundle savedInstanceState)38     protected void onCreate(Bundle savedInstanceState) {
39         super.onCreate(savedInstanceState);
40         setContentView(R.layout.scrolling_stretch_surfaceview);
41 
42         mVerticalSurfaceView = new SurfaceView(this);
43         mVerticalSurfaceView.getHolder().addCallback(this);
44 
45         mHorizontalSurfaceView = new SurfaceView(this);
46         mHorizontalSurfaceView.getHolder().addCallback(this);
47 
48         FrameLayout verticalContainer = findViewById(R.id.vertical_surfaceview_container);
49         verticalContainer.addView(mVerticalSurfaceView,
50             new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
51                 FrameLayout.LayoutParams.MATCH_PARENT));
52 
53         FrameLayout horizontalContainer = findViewById(R.id.horizontal_surfaceview_container);
54         horizontalContainer.addView(mHorizontalSurfaceView,
55             new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
56                 FrameLayout.LayoutParams.MATCH_PARENT));
57 
58         ImageView verticalImageView = findViewById(R.id.vertical_imageview);
59         verticalImageView.setImageDrawable(new LineDrawable());
60 
61         ImageView horizontalImageView = findViewById(R.id.horizontal_imageview);
62         horizontalImageView.setImageDrawable(new LineDrawable());
63     }
64 
65     @Override
surfaceCreated(SurfaceHolder holder)66     public void surfaceCreated(SurfaceHolder holder) {
67     }
68 
69     @Override
surfaceChanged(SurfaceHolder holder, int format, int width, int height)70     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
71         Canvas canvas = holder.lockCanvas();
72 
73         drawLine(canvas, width, height);
74         holder.unlockCanvasAndPost(canvas);
75     }
76 
drawLine(Canvas canvas, int width, int height)77     private static void drawLine(Canvas canvas, int width, int height) {
78         canvas.drawColor(Color.GRAY);
79 
80         Paint paint = new Paint();
81         paint.setAntiAlias(true);
82         paint.setColor(Color.GREEN);
83         paint.setStyle(Paint.Style.STROKE);
84         paint.setStrokeWidth(10f);
85         canvas.drawLine(0, 0, width, height, paint);
86     }
87 
88     private static class LineDrawable extends Drawable {
89         @Override
draw(Canvas canvas)90         public void draw(Canvas canvas) {
91             drawLine(canvas, getBounds().width(), getBounds().height());
92         }
93 
94         @Override
setAlpha(int alpha)95         public void setAlpha(int alpha) {
96             // NO-OP
97         }
98 
99         @Override
setColorFilter(ColorFilter colorFilter)100         public void setColorFilter(ColorFilter colorFilter) {
101             // NO-OP
102         }
103 
104         @Override
getOpacity()105         public int getOpacity() {
106             return 0;
107         }
108     }
109 
110     @Override
surfaceDestroyed(SurfaceHolder holder)111     public void surfaceDestroyed(SurfaceHolder holder) {
112     }
113 }
114