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 androidx.constraintlayout.core.widgets; 18 19 import static androidx.constraintlayout.core.widgets.analyzer.BasicMeasure.AT_MOST; 20 import static androidx.constraintlayout.core.widgets.analyzer.BasicMeasure.EXACTLY; 21 import static androidx.constraintlayout.core.widgets.analyzer.BasicMeasure.UNSPECIFIED; 22 23 import androidx.constraintlayout.core.LinearSystem; 24 25 /** 26 * Simple VirtualLayout that center the first referenced widget onto itself. 27 */ 28 public class Placeholder extends VirtualLayout { 29 30 // @TODO: add description 31 @Override measure(int widthMode, int widthSize, int heightMode, int heightSize)32 public void measure(int widthMode, int widthSize, int heightMode, int heightSize) { 33 int width = 0; 34 int height = 0; 35 int paddingLeft = getPaddingLeft(); 36 int paddingRight = getPaddingRight(); 37 int paddingTop = getPaddingTop(); 38 int paddingBottom = getPaddingBottom(); 39 40 width += paddingLeft + paddingRight; 41 height += paddingTop + paddingBottom; 42 43 if (mWidgetsCount > 0) { 44 // grab the first referenced widget size in case we are ourselves in wrap_content 45 width += mWidgets[0].getWidth(); 46 height += mWidgets[0].getHeight(); 47 } 48 width = Math.max(getMinWidth(), width); 49 height = Math.max(getMinHeight(), height); 50 51 int measuredWidth = 0; 52 int measuredHeight = 0; 53 54 if (widthMode == EXACTLY) { 55 measuredWidth = widthSize; 56 } else if (widthMode == AT_MOST) { 57 measuredWidth = Math.min(width, widthSize); 58 } else if (widthMode == UNSPECIFIED) { 59 measuredWidth = width; 60 } 61 62 if (heightMode == EXACTLY) { 63 measuredHeight = heightSize; 64 } else if (heightMode == AT_MOST) { 65 measuredHeight = Math.min(height, heightSize); 66 } else if (heightMode == UNSPECIFIED) { 67 measuredHeight = height; 68 } 69 70 setMeasure(measuredWidth, measuredHeight); 71 setWidth(measuredWidth); 72 setHeight(measuredHeight); 73 needsCallbackFromSolver(mWidgetsCount > 0); 74 } 75 76 @Override addToSolver(LinearSystem system, boolean optimize)77 public void addToSolver(LinearSystem system, boolean optimize) { 78 super.addToSolver(system, optimize); 79 80 if (mWidgetsCount > 0) { 81 ConstraintWidget widget = mWidgets[0]; 82 widget.resetAllConstraints(); 83 widget.connect(ConstraintAnchor.Type.LEFT, this, ConstraintAnchor.Type.LEFT); 84 widget.connect(ConstraintAnchor.Type.RIGHT, this, ConstraintAnchor.Type.RIGHT); 85 widget.connect(ConstraintAnchor.Type.TOP, this, ConstraintAnchor.Type.TOP); 86 widget.connect(ConstraintAnchor.Type.BOTTOM, this, ConstraintAnchor.Type.BOTTOM); 87 } 88 } 89 } 90