John Reck | 51aa189 | 2019-01-30 15:28:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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.content.Context; |
| 21 | import android.graphics.Canvas; |
| 22 | import android.graphics.RenderNode; |
| 23 | import android.os.Bundle; |
| 24 | import android.widget.LinearLayout; |
| 25 | import android.widget.ProgressBar; |
| 26 | import android.widget.ScrollView; |
| 27 | import android.widget.TextView; |
| 28 | |
| 29 | @SuppressWarnings({"UnusedDeclaration"}) |
| 30 | public class PositionListenerActivity extends Activity { |
| 31 | @Override |
| 32 | protected void onCreate(Bundle savedInstanceState) { |
| 33 | super.onCreate(savedInstanceState); |
| 34 | |
| 35 | final LinearLayout layout = new LinearLayout(this); |
| 36 | layout.setOrientation(LinearLayout.VERTICAL); |
| 37 | |
| 38 | ProgressBar spinner = new ProgressBar(this, null, android.R.attr.progressBarStyleLarge); |
| 39 | layout.addView(spinner); |
| 40 | |
| 41 | ScrollView scrollingThing = new ScrollView(this); |
| 42 | scrollingThing.addView(new MyPositionReporter(this)); |
| 43 | layout.addView(scrollingThing); |
| 44 | |
| 45 | setContentView(layout); |
| 46 | } |
| 47 | |
| 48 | static class MyPositionReporter extends TextView implements RenderNode.PositionUpdateListener { |
| 49 | RenderNode mNode; |
| 50 | int mCurrentCount = 0; |
| 51 | int mTranslateY = 0; |
| 52 | |
| 53 | MyPositionReporter(Context c) { |
| 54 | super(c); |
| 55 | mNode = new RenderNode("positionListener"); |
Adam Powell | 769b863 | 2019-02-04 11:28:22 -0800 | [diff] [blame] | 56 | mNode.addPositionUpdateListener(this); |
John Reck | 51aa189 | 2019-01-30 15:28:44 -0800 | [diff] [blame] | 57 | setTextAlignment(TEXT_ALIGNMENT_VIEW_START); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 62 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 63 | setMeasuredDimension(getMeasuredWidth(), 10000); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 68 | mNode.setLeftTopRightBottom(left, top, right, bottom); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | protected void onDraw(Canvas canvas) { |
| 73 | ScrollView parent = (ScrollView) getParent(); |
| 74 | canvas.translate(0, parent.getScrollY()); |
| 75 | super.onDraw(canvas); |
| 76 | canvas.translate(0, -parent.getScrollY()); |
| 77 | // Inject our listener proxy |
| 78 | canvas.drawRenderNode(mNode); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void positionChanged(long frameNumber, int left, int top, int right, int bottom) { |
| 83 | post(() -> { |
| 84 | mCurrentCount++; |
| 85 | setText(String.format("%d: Position [%d, %d, %d, %d]", mCurrentCount, |
| 86 | left, top, right, bottom)); |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void positionLost(long frameNumber) { |
| 92 | post(() -> { |
| 93 | mCurrentCount++; |
| 94 | setText(mCurrentCount + " No position"); |
| 95 | }); |
| 96 | } |
| 97 | } |
| 98 | } |