Пример #1
0
        private int MeasureWidth(int measureSpec)
        {
            int             result;
            MeasureSpecMode specMode = MeasureSpec.GetMode(measureSpec);
            int             specSize = MeasureSpec.GetSize(measureSpec);

            if (specMode == MeasureSpecMode.Exactly)
            {
                // The parent has determined an exact size for the child.
                result = specSize;
            }
            else if (specMode == MeasureSpecMode.AtMost)
            {
                // The child can be as large as it wants up to the specified size.
                result = specSize;
            }
            else
            {
                // The parent has not imposed any constraint on the child.
                result = _canvasSize;
            }

            return(result);
        }
Пример #2
0
        private int MeasureHeight(int measureSpecHeight)
        {
            int             result;
            MeasureSpecMode specMode = MeasureSpec.GetMode(measureSpecHeight);
            int             specSize = MeasureSpec.GetSize(measureSpecHeight);

            if (specMode == MeasureSpecMode.Exactly)
            {
                // We were told how big to be
                result = specSize;
            }
            else if (specMode == MeasureSpecMode.AtMost)
            {
                // The child can be as large as it wants up to the specified size.
                result = specSize;
            }
            else
            {
                // Measure the text (beware: ascent is a negative number)
                result = _canvasSize;
            }

            return(result + 2);
        }
Пример #3
0
        /**
         * Determines the width of this view
         * @param measureSpec A measureSpec packed into an int
         * @return The width of the view, honoring constraints from measureSpec
         */
        private int measureWidth(int measureSpec)
        {
            int             result   = 0;
            MeasureSpecMode specMode = MeasureSpec.GetMode(measureSpec);
            int             specSize = MeasureSpec.GetSize(measureSpec);

            if (specMode == MeasureSpecMode.Exactly)
            {
                // We were told how big to be
                result = specSize;
            }
            else
            {
                // Measure the text

                result = IMAGE_WIDTH + MAX_TEXT_WIDTH + TOAST_HEIGHT;
                if (specMode == MeasureSpecMode.AtMost)
                {
                    // Respect AT_MOST value if that was what is called for by measureSpec
                    result = Math.Min(result, specSize);
                }
            }
            return(result);
        }
        private int FindSize(MeasureSpecMode modeSize, int controlMaxSize, int contentSize)
        {
            int realControlLength;

            switch (modeSize)
            {
            case MeasureSpecMode.Unspecified:
                realControlLength = contentSize;
                break;

            case MeasureSpecMode.AtMost:
                realControlLength = Math.Min(contentSize, controlMaxSize);
                break;

            case MeasureSpecMode.Exactly:
                realControlLength = controlMaxSize;
                break;

            default:
                realControlLength = contentSize;
                break;
            }
            return(realControlLength);
        }
Пример #5
0
        protected override (float measuredWidth, float measuredHeight) OnMesure(float widthSpec, float heightSpec, MeasureSpecMode mode)
        {
            for (int i = 0; i < Children.Count; i++)
            {
                var child = Children[i];

                child.Mesure(widthSpec, heightSpec, MeasureSpecMode.AtMost);

                if (child.HorizontalAligment == HorizontalAligment.Stretch)
                {
                    child.Mesure(widthSpec, child.GetValue <float>(nameof(child.CalculatedHeight), ReactiveObject.LAYOUT_VALUE), MeasureSpecMode.Exactly);
                }
                else if (child.Width != (int)WidthOptions.Auto)
                {
                    child.Mesure(child.Width, child.GetValue <float>(nameof(child.CalculatedHeight), ReactiveObject.LAYOUT_VALUE), MeasureSpecMode.Exactly);
                }

                if (child.VerticalAligment == VerticalAligment.Stretch)
                {
                    child.Mesure(child.GetValue <float>(nameof(child.CalculatedWidth), ReactiveObject.LAYOUT_VALUE), heightSpec, MeasureSpecMode.Exactly);
                }
                else if (child.Height != (int)HeightOptions.Auto)
                {
                    child.Mesure(child.GetValue <float>(nameof(child.CalculatedWidth), ReactiveObject.LAYOUT_VALUE), child.Height, MeasureSpecMode.Exactly);
                }
            }
            return(base.OnMesure(widthSpec, heightSpec, MeasureSpecMode.Exactly));//(widthSpec, heightSpec);
        }
Пример #6
0
 int MakeMeasureSpec(MeasureSpecMode mode, int size)
 {
     return(size + (int)mode);
 }
Пример #7
0
        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            int width  = 0;
            int height = 0;

            MeasureSpecMode widthMode  = MeasureSpec.GetMode(widthMeasureSpec);
            MeasureSpecMode heightMode = MeasureSpec.GetMode(heightMeasureSpec);

            int widthAllowed  = MeasureSpec.GetSize(widthMeasureSpec);
            int heightAllowed = MeasureSpec.GetSize(heightMeasureSpec);


            //Log.d("color-picker-view", "widthMode: " + modeToString(widthMode) + " heightMode: " + modeToString(heightMode) + " widthAllowed: " + widthAllowed + " heightAllowed: " + heightAllowed);

            if (widthMode == MeasureSpecMode.Exactly || heightMode == MeasureSpecMode.Exactly)
            {
                //A exact value has been set in either direction, we need to stay within this size.

                if (widthMode == MeasureSpecMode.Exactly && heightMode != MeasureSpecMode.Exactly)
                {
                    //The with has been specified exactly, we need to adopt the height to fit.
                    int h = (int)(widthAllowed - _panelSpacing - _huePanelWidth);

                    if (_mShowAlphaPanel)
                    {
                        h += (int)(_panelSpacing + _alphaPanelHeight);
                    }

                    height = h > heightAllowed ? heightAllowed : h;

                    width = widthAllowed;
                }
                else if (heightMode == MeasureSpecMode.Exactly && widthMode != MeasureSpecMode.Exactly)
                {
                    //The height has been specified exactly, we need to stay within this height and adopt the width.

                    int w = (int)(heightAllowed + _panelSpacing + _huePanelWidth);

                    if (_mShowAlphaPanel)
                    {
                        w -= (int)(_panelSpacing - _alphaPanelHeight);
                    }

                    width = w > widthAllowed ? widthAllowed : w;

                    height = heightAllowed;
                }
                else
                {
                    //If we get here the dev has set the width and height to exact sizes. For example match_parent or 300dp.
                    //This will mean that the sat/val panel will not be square but it doesn't matter. It will work anyway.
                    //In all other senarios our goal is to make that panel square.

                    //We set the sizes to exactly what we were told.
                    width  = widthAllowed;
                    height = heightAllowed;
                }
            }
            else
            {
                //If no exact size has been set we try to make our view as big as possible
                //within the allowed space.

                //Calculate the needed with to layout the view based on the allowed height.
                int widthNeeded = (int)(heightAllowed + _panelSpacing + _huePanelWidth);
                //Calculate the needed height to layout the view based on the allowed width.
                int heightNeeded = (int)(widthAllowed - _panelSpacing - _huePanelWidth);

                if (_mShowAlphaPanel)
                {
                    widthNeeded  -= (int)(_panelSpacing + _alphaPanelHeight);
                    heightNeeded += (int)(_panelSpacing + _alphaPanelHeight);
                }


                if (widthNeeded <= widthAllowed)
                {
                    width  = widthNeeded;
                    height = heightAllowed;
                }
                else if (heightNeeded <= heightAllowed)
                {
                    height = heightNeeded;
                    width  = widthAllowed;
                }
            }

            //Log.d("mColorPicker", " Size: " + Width + "x" + Height);

            SetMeasuredDimension(width, height);
        }
        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            base.OnMeasure(widthMeasureSpec, heightMeasureSpec);

            int sizeWidth  = MeasureSpec.GetSize(widthMeasureSpec) - PaddingLeft - PaddingRight;
            int sizeHeight = MeasureSpec.GetSize(heightMeasureSpec);

            MeasureSpecMode modeWidth  = MeasureSpec.GetMode(widthMeasureSpec);
            MeasureSpecMode modeHeight = MeasureSpec.GetMode(heightMeasureSpec);

            int width  = 0;
            int height = PaddingTop + PaddingBottom;

            int lineWidth  = 0;
            int lineHeight = 0;

            int childCount = ChildCount;

            for (int i = 0; i < childCount; i++)
            {
                View child     = GetChildAt(i);
                bool lastChild = i == childCount - 1;

                if (child.Visibility == ViewStates.Gone)
                {
                    if (lastChild)
                    {
                        width   = Math.Max(width, lineWidth);
                        height += lineHeight;
                    }

                    continue;
                }

                MeasureChildWithMargins(child, widthMeasureSpec, lineWidth, heightMeasureSpec, height);

                CustomLayoutParams lp = (CustomLayoutParams)child.LayoutParameters;

                MeasureSpecMode childWidthMode = MeasureSpecMode.AtMost;
                int             childWidthSize = sizeWidth;

                MeasureSpecMode childHeightMode = MeasureSpecMode.AtMost;
                int             childHeightSize = sizeHeight;

                if (lp.Width == LayoutParams.MatchParent)
                {
                    childWidthMode  = MeasureSpecMode.Exactly;
                    childWidthSize -= lp.LeftMargin + lp.RightMargin;
                }
                else if (lp.Width >= 0)
                {
                    childWidthMode = MeasureSpecMode.Exactly;
                    childWidthSize = lp.Width;
                }

                if (lp.Height >= 0)
                {
                    childHeightMode = MeasureSpecMode.Exactly;
                    childHeightSize = lp.Height;
                }
                else if (modeHeight == MeasureSpecMode.Unspecified)
                {
                    childHeightMode = MeasureSpecMode.Unspecified;
                    childHeightSize = 0;
                }

                child.Measure(MeasureSpec.MakeMeasureSpec(childWidthSize, childWidthMode), MeasureSpec.MakeMeasureSpec(childHeightSize, childHeightMode));

                int childWidth = child.MeasuredWidth + lp.LeftMargin + lp.RightMargin;

                if (lineWidth + childWidth > sizeWidth)
                {
                    width     = Math.Max(width, lineWidth);
                    lineWidth = childWidth;

                    height    += lineHeight;
                    lineHeight = child.MeasuredHeight + lp.TopMargin + lp.BottomMargin;
                }
                else
                {
                    lineWidth += childWidth;
                    lineHeight = Math.Max(lineHeight, child.MeasuredHeight + lp.TopMargin + lp.BottomMargin);
                }

                if (lastChild)
                {
                    width   = Math.Max(width, lineWidth);
                    height += lineHeight;
                }
            }

            width += PaddingLeft + PaddingRight;

            SetMeasuredDimension((modeWidth == MeasureSpecMode.Exactly) ? sizeWidth : width, (modeHeight == MeasureSpecMode.Exactly) ? sizeHeight : height);
        }
Пример #9
0
        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            int finalWidth  = 0;
            int finalHeight = 0;

            MeasureSpecMode widthMode  = MeasureSpec.GetMode(widthMeasureSpec);
            MeasureSpecMode heightMode = MeasureSpec.GetMode(heightMeasureSpec);

            int widthAllowed  = (int)MeasureSpec.GetSize(widthMeasureSpec);
            int heightAllowed = (int)MeasureSpec.GetSize(heightMeasureSpec);


            //Log.d("color-picker-view", "widthMode: " + modeToString(widthMode) + " heightMode: " + modeToString(heightMode) + " widthAllowed: " + widthAllowed + " heightAllowed: " + heightAllowed);

            if (widthMode == MeasureSpecMode.Exactly || heightMode == MeasureSpecMode.Exactly)
            {
                //A exact value has been set in either direction, we need to stay within this size.

                if (widthMode == MeasureSpecMode.Exactly && heightMode != MeasureSpecMode.Exactly)
                {
                    //The with has been specified exactly, we need to adopt the height to fit.
                    int h = (int)(widthAllowed - PANEL_SPACING - HUE_PANEL_WIDTH);

                    if (mShowAlphaPanel)
                    {
                        h += (int)(PANEL_SPACING + ALPHA_PANEL_HEIGHT);
                    }

                    if (h > heightAllowed)
                    {
                        //We can't fit the view in this container, set the size to whatever was allowed.
                        finalHeight = heightAllowed;
                    }
                    else
                    {
                        finalHeight = h;
                    }

                    finalWidth = widthAllowed;
                }
                else if (heightMode == MeasureSpecMode.Exactly && widthMode != MeasureSpecMode.Exactly)
                {
                    //The height has been specified exactly, we need to stay within this height and adopt the width.

                    int w = (int)(heightAllowed + PANEL_SPACING + HUE_PANEL_WIDTH);

                    if (mShowAlphaPanel)
                    {
                        w -= (int)(PANEL_SPACING - ALPHA_PANEL_HEIGHT);
                    }

                    if (w > widthAllowed)
                    {
                        //we can't fit within this container, set the size to whatever was allowed.
                        finalWidth = widthAllowed;
                    }
                    else
                    {
                        finalWidth = w;
                    }

                    finalHeight = heightAllowed;
                }
                else
                {
                    //If we get here the dev has set the width and height to exact sizes. For example match_parent or 300dp.
                    //This will mean that the sat/val panel will not be square but it doesn't matter. It will work anyway.
                    //In all other senarios our goal is to make that panel square.

                    //We set the sizes to exactly what we were told.
                    finalWidth  = widthAllowed;
                    finalHeight = heightAllowed;
                }
            }
            else
            {
                //If no exact size has been set we try to make our view as big as possible
                //within the allowed space.

                //Calculate the needed with to layout the view based on the allowed height.
                int widthNeeded = (int)(heightAllowed + PANEL_SPACING + HUE_PANEL_WIDTH);
                //Calculate the needed height to layout the view based on the allowed width.
                int heightNeeded = (int)(widthAllowed - PANEL_SPACING - HUE_PANEL_WIDTH);

                if (mShowAlphaPanel)
                {
                    widthNeeded  -= (int)(PANEL_SPACING + ALPHA_PANEL_HEIGHT);
                    heightNeeded += (int)(PANEL_SPACING + ALPHA_PANEL_HEIGHT);
                }


                if (widthNeeded <= widthAllowed)
                {
                    finalWidth  = widthNeeded;
                    finalHeight = heightAllowed;
                }
                else if (heightNeeded <= heightAllowed)
                {
                    finalHeight = heightNeeded;
                    finalWidth  = widthAllowed;
                }
            }

            //Log.d("mColorPicker", "Final Size: " + finalWidth + "x" + finalHeight);

            SetMeasuredDimension(finalWidth, finalHeight);
        }
Пример #10
0
 public static int MakeMeasureSpec(int size, MeasureSpecMode mode)
 {
     return((size & ~MODE_MASK) | ((int)mode & MODE_MASK));
 }
 public static int MakeMeasureSpec(int size, MeasureSpecMode mode) {
     return size + (int)mode;
 }
Пример #12
0
        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            //Log.i("@@@@", "onMeasure(" + MeasureSpec.toString(widthMeasureSpec) + ", "
            //        + MeasureSpec.toString(heightMeasureSpec) + ")");

            int width  = GetDefaultSize(mVideoWidth, widthMeasureSpec);
            int height = GetDefaultSize(mVideoHeight, heightMeasureSpec);

            if (mVideoWidth > 0 && mVideoHeight > 0)
            {
                MeasureSpecMode widthSpecMode  = MeasureSpec.GetMode(widthMeasureSpec);
                int             widthSpecSize  = MeasureSpec.GetSize(widthMeasureSpec);
                MeasureSpecMode heightSpecMode = MeasureSpec.GetMode(heightMeasureSpec);
                int             heightSpecSize = MeasureSpec.GetSize(heightMeasureSpec);

                if (widthSpecMode == MeasureSpecMode.Exactly && heightSpecMode == MeasureSpecMode.Exactly)
                {
                    // the size is fixed
                    width  = widthSpecSize;
                    height = heightSpecSize;

                    // for compatibility, we adjust size based on aspect ratio
                    if (mVideoWidth * height < width * mVideoHeight)
                    {
                        //Log.i("@@@", "image too wide, correcting");
                        width = height * mVideoWidth / mVideoHeight;
                    }
                    else if (mVideoWidth * height > width * mVideoHeight)
                    {
                        //Log.i("@@@", "image too tall, correcting");
                        height = width * mVideoHeight / mVideoWidth;
                    }
                }
                else if (widthSpecMode == MeasureSpecMode.Exactly)
                {
                    // only the width is fixed, adjust the height to match aspect ratio if possible
                    width  = widthSpecSize;
                    height = width * mVideoHeight / mVideoWidth;
                    if (heightSpecMode == MeasureSpecMode.AtMost && height > heightSpecSize)
                    {
                        // couldn't match aspect ratio within the constraints
                        height = heightSpecSize;
                    }
                }
                else if (heightSpecMode == MeasureSpecMode.Exactly)
                {
                    // only the height is fixed, adjust the width to match aspect ratio if possible
                    height = heightSpecSize;
                    width  = height * mVideoWidth / mVideoHeight;
                    if (widthSpecMode == MeasureSpecMode.AtMost && width > widthSpecSize)
                    {
                        // couldn't match aspect ratio within the constraints
                        width = widthSpecSize;
                    }
                }
                else
                {
                    // neither the width nor the height are fixed, try to use actual video size
                    width  = mVideoWidth;
                    height = mVideoHeight;
                    if (heightSpecMode == MeasureSpecMode.AtMost && height > heightSpecSize)
                    {
                        // too tall, decrease both width and height
                        height = heightSpecSize;
                        width  = height * mVideoWidth / mVideoHeight;
                    }
                    if (widthSpecMode == MeasureSpecMode.AtMost && width > widthSpecSize)
                    {
                        // too wide, decrease both width and height
                        width  = widthSpecSize;
                        height = width * mVideoHeight / mVideoWidth;
                    }
                }
            }
            else
            {
                // no size yet, just adopt the given spec sizes
            }
            SetMeasuredDimension(width, height);
        }
        private int ChooseHeight(MeasureSpecMode mode, int size)
        {
            if (mode == MeasureSpecMode.AtMost || mode == MeasureSpecMode.Exactly)
            {
                return size;
            }

            return GetPrefferedHeight();
        }
Пример #14
0
        private void MeasureFirstStageViews(int width, MeasureSpecMode widthMode, int height, MeasureSpecMode heightMode)
        {
            // measure all children that we can measure at this moment (p-a, p-p, a-a, p-unspec, a-unspec)
            foreach (var viewInfo in _viewsMeasureInfos.Values)
            {
                var child = viewInfo.View;

                bool canMeasureHeight = heightMode == MeasureSpecMode.Unspecified ||
                                        viewInfo.IsInAutoRow ||
                                        viewInfo.IsOnlyInPixelRows;

                bool canMeasureWidth = widthMode == MeasureSpecMode.Unspecified ||
                                       viewInfo.IsInAutoColumn ||
                                       viewInfo.IsOnlyInPixelColumns;

                if (!canMeasureHeight || !canMeasureWidth)
                {
                    continue;
                }

                var childWidthMeasureSpec = MeasureSpec.MakeMeasureSpec(width,
                                                                        widthMode == MeasureSpecMode.Unspecified ? widthMode : MeasureSpecMode.AtMost);
                var childHeightMeasureSpec = MeasureSpec.MakeMeasureSpec(height,
                                                                         heightMode == MeasureSpecMode.Unspecified ? heightMode : MeasureSpecMode.AtMost);

                child.Measure(childWidthMeasureSpec, childHeightMeasureSpec);
                viewInfo.IsMeasured = true;

                var dynamicWidth  = Math.Max(0, child.MeasuredWidth - viewInfo.StaticWidth);
                var dynamicHeight = Math.Max(0, child.MeasuredHeight - viewInfo.StaticHeight);
                if (viewInfo.IsInAutoRow)
                {
                    if (!(viewInfo.FirstAutoRow.IsMeasured && viewInfo.FirstAutoRow.Length > dynamicHeight))
                    {
                        viewInfo.FirstAutoRow.SetLength(dynamicHeight);
                    }
                }
                else if (viewInfo.IsInStarRow && heightMode == MeasureSpecMode.Unspecified)
                {
                    foreach (var rowInfo in viewInfo.AssociatedRows)
                    {
                        if (rowInfo.Definition.Length.IsStar)
                        {
                            var dynamicStarHeight = dynamicHeight * (rowInfo.Definition.Length.Value / viewInfo.HeightStarSum);
                            if (!(rowInfo.IsMeasured && rowInfo.Length > dynamicStarHeight))
                            {
                                rowInfo.SetLength(dynamicStarHeight);
                            }
                        }
                    }
                }
                if (viewInfo.IsInAutoColumn)
                {
                    if (!(viewInfo.FirstAutoColumn.IsMeasured && viewInfo.FirstAutoColumn.Length > dynamicWidth))
                    {
                        viewInfo.FirstAutoColumn.SetLength(dynamicWidth);
                    }
                }
                else if (viewInfo.IsInStarColumn && heightMode == MeasureSpecMode.Unspecified)
                {
                    foreach (var columnInfo in viewInfo.AssociatedColumns)
                    {
                        if (columnInfo.Definition.Length.IsStar)
                        {
                            var dynamicStarWidth = dynamicWidth * (columnInfo.Definition.Length.Value / viewInfo.WidthStarSum);
                            if (!(columnInfo.IsMeasured && columnInfo.Length > dynamicStarWidth))
                            {
                                columnInfo.SetLength(dynamicStarWidth);
                            }
                        }
                    }
                }
            }
        }
        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            int sizeWidth  = MeasureSpec.GetSize(widthMeasureSpec) - this.PaddingRight - this.PaddingLeft;
            int sizeHeight = MeasureSpec.GetSize(heightMeasureSpec) - this.PaddingTop - this.PaddingBottom;

            MeasureSpecMode modeWidth  = MeasureSpec.GetMode(widthMeasureSpec);
            MeasureSpecMode modeHeight = MeasureSpec.GetMode(heightMeasureSpec);

            int controlMaxLength    = this.config.Orientation == HORIZONTAL ? sizeWidth : sizeHeight;
            int controlMaxThickness = this.config.Orientation == HORIZONTAL ? sizeHeight : sizeWidth;

            MeasureSpecMode modeLength    = this.config.Orientation == HORIZONTAL ? modeWidth : modeHeight;
            MeasureSpecMode modeThickness = this.config.Orientation == HORIZONTAL ? modeHeight : modeWidth;

            lines.Clear();
            LineDefinition currentLine = new LineDefinition(controlMaxLength, config);

            lines.Add(currentLine);

            int count = this.ChildCount;

            for (int i = 0; i < count; i++)
            {
                View child = this.GetChildAt(i);
                if (child.Visibility == ViewStates.Gone)
                {
                    continue;
                }

                CustomLayoutParams lp = (CustomLayoutParams)child.LayoutParameters;

                child.Measure(GetChildMeasureSpec(widthMeasureSpec, this.PaddingLeft + this.PaddingRight, lp.Width), GetChildMeasureSpec(heightMeasureSpec, this.PaddingTop + this.PaddingBottom, lp.Height));

                lp.ClearCalculatedFields(this.config.Orientation);
                if (this.config.Orientation == FlowLayout.HORIZONTAL)
                {
                    lp.Length    = child.MeasuredWidth;
                    lp.Thickness = child.MeasuredHeight;
                }
                else
                {
                    lp.Length    = child.MeasuredHeight;
                    lp.Thickness = child.MeasuredWidth;
                }

                bool newLine = lp.newLine || (modeLength != MeasureSpecMode.Unspecified && !currentLine.CanFit(child));
                if (newLine)
                {
                    currentLine = new LineDefinition(controlMaxLength, config);
                    if (this.config.Orientation == VERTICAL && this.config.Direction == LAYOUT_DIRECTION_RTL)
                    {
                        lines.Insert(0, currentLine);
                    }
                    else
                    {
                        lines.Add(currentLine);
                    }
                }

                if (this.config.Orientation == HORIZONTAL && this.config.Direction == LAYOUT_DIRECTION_RTL)
                {
                    currentLine.AddView(0, child);
                }
                else
                {
                    currentLine.AddView(child);
                }
            }

            this.CalculateLinesAndChildPosition(lines);

            int contentLength = 0;

            foreach (LineDefinition l in lines)
            {
                contentLength = Math.Max(contentLength, l.LineLength);
            }
            int contentThickness = currentLine.LineStartThickness + currentLine.LineThickness;

            int realControlLength    = this.FindSize(modeLength, controlMaxLength, contentLength);
            int realControlThickness = this.FindSize(modeHeight, controlMaxThickness, contentThickness);

            this.ApplyGravityToLines(lines, realControlLength, realControlThickness);

            foreach (LineDefinition line in lines)
            {
                this.ApplyGravityToLine(line);
                this.ApplyPositionsToViews(line);
            }

            /* need to take padding into account */
            int totalControlWidth  = this.PaddingLeft + this.PaddingRight;
            int totalControlHeight = this.PaddingBottom + this.PaddingTop;

            if (this.config.Orientation == HORIZONTAL)
            {
                totalControlWidth  += contentLength;
                totalControlHeight += contentThickness;
            }
            else
            {
                totalControlWidth  += contentThickness;
                totalControlHeight += contentLength;
            }
            this.SetMeasuredDimension(ResolveSize(totalControlWidth, widthMeasureSpec), ResolveSize(totalControlHeight, heightMeasureSpec));
        }
Пример #16
0
        private void MeasureFirstStageViews(int width, MeasureSpecMode widthMode, int height, MeasureSpecMode heightMode)
        {
            // measure all children that we can measure at this moment (p-a, p-p, a-a, p-unspec, a-unspec)
            foreach(var viewInfo in _viewsMeasureInfos.Values)
            {
                var child = viewInfo.View;

                bool canMeasureHeight = heightMode == MeasureSpecMode.Unspecified
                    || viewInfo.IsInAutoRow
                    || viewInfo.IsOnlyInPixelRows;

                bool canMeasureWidth = widthMode == MeasureSpecMode.Unspecified
                    || viewInfo.IsInAutoColumn
                    || viewInfo.IsOnlyInPixelColumns;

                if(!canMeasureHeight || !canMeasureWidth)
                {
                    continue;
                }

                var childWidthMeasureSpec = MeasureSpec.MakeMeasureSpec(width,
                                                                        widthMode == MeasureSpecMode.Unspecified ? widthMode : MeasureSpecMode.AtMost);
                var childHeightMeasureSpec = MeasureSpec.MakeMeasureSpec(height,
                                                                         heightMode == MeasureSpecMode.Unspecified ? heightMode : MeasureSpecMode.AtMost);

                child.Measure(childWidthMeasureSpec, childHeightMeasureSpec);
                viewInfo.IsMeasured = true;

                var dynamicWidth = Math.Max(0, child.MeasuredWidth - viewInfo.StaticWidth);
                var dynamicHeight = Math.Max(0, child.MeasuredHeight - viewInfo.StaticHeight);
                if(viewInfo.IsInAutoRow)
                {
                    if(!(viewInfo.FirstAutoRow.IsMeasured && viewInfo.FirstAutoRow.Length > dynamicHeight))
                    {
                        viewInfo.FirstAutoRow.SetLength(dynamicHeight);
                    }
                }
                else if(viewInfo.IsInStarRow && heightMode == MeasureSpecMode.Unspecified)
                {
                    foreach(var rowInfo in viewInfo.AssociatedRows)
                    {
                        if(rowInfo.Definition.Length.IsStar)
                        {
                            var dynamicStarHeight = dynamicHeight * (rowInfo.Definition.Length.Value / viewInfo.HeightStarSum);
                            if(!(rowInfo.IsMeasured && rowInfo.Length > dynamicStarHeight))
                            {
                                rowInfo.SetLength(dynamicStarHeight);
                            }
                        }
                    }
                }
                if(viewInfo.IsInAutoColumn)
                {
                    if(!(viewInfo.FirstAutoColumn.IsMeasured && viewInfo.FirstAutoColumn.Length > dynamicWidth))
                    {
                        viewInfo.FirstAutoColumn.SetLength(dynamicWidth);
                    }
                }
                else if(viewInfo.IsInStarColumn && heightMode == MeasureSpecMode.Unspecified)
                {
                    foreach(var columnInfo in viewInfo.AssociatedColumns)
                    {
                        if(columnInfo.Definition.Length.IsStar)
                        {
                            var dynamicStarWidth = dynamicWidth * (columnInfo.Definition.Length.Value / viewInfo.WidthStarSum);
                            if(!(columnInfo.IsMeasured && columnInfo.Length > dynamicStarWidth))
                            {
                                columnInfo.SetLength(dynamicStarWidth);
                            }
                        }
                    }
                }
            }
        }
Пример #17
0
 /// <summary>
 /// Based on internal class Xamarin.Forms.Platform.Android.MeasureSpecFactory
 /// </summary>
 /// <param name="size">The size.</param>
 /// <param name="mode">The mode.</param>
 /// <returns></returns>
 private static int MakeMeasureSpec(int size, MeasureSpecMode mode)
 {
     return(( int )(size + mode));
 }
Пример #18
0
 public static int MakeMeasureSpec(int size, MeasureSpecMode mode) => size + (int)mode;
Пример #19
0
        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            MeasureSpecMode widthMode  = MeasureSpec.GetMode(widthMeasureSpec);
            MeasureSpecMode heightMode = MeasureSpec.GetMode(heightMeasureSpec);
            int             widthSize  = MeasureSpec.GetSize(widthMeasureSpec);
            int             heightSize = MeasureSpec.GetSize(heightMeasureSpec);

            MeasureChildren(widthMeasureSpec, heightMeasureSpec);

            int width  = 0;
            int height = 0;

            int row          = 0; // The row counter.
            int rowWidth     = 0; // Calc the current row width.
            int rowMaxHeight = 0; // Calc the max tag height, in current row.

            int count = ChildCount;

            for (int i = 0; i < count; i++)
            {
                View child       = GetChildAt(i);
                int  childWidth  = child.MeasuredWidth;
                int  childHeight = child.MeasuredHeight;

                if (child.Visibility != ViewStates.Gone)
                {
                    rowWidth += childWidth;
                    if (rowWidth > widthSize)
                    {                               // Next line.
                        rowWidth     = childWidth;  // The next row width.
                        height      += rowMaxHeight + verticalSpacing;
                        rowMaxHeight = childHeight; // The next row max height.
                        row++;
                    }
                    else
                    { // This line.
                        rowMaxHeight = System.Math.Max(rowMaxHeight, childHeight);
                    }
                    rowWidth += horizontalSpacing;
                }
            }
            // Account for the last row height.
            height += rowMaxHeight;

            // Account for the padding too.
            height += PaddingTop + PaddingBottom;

            // If the tags grouped in one row, set the width to wrap the tags.
            if (row == 0)
            {
                width  = rowWidth;
                width += PaddingLeft + PaddingRight;
            }
            else
            {// If the tags grouped exceed one line, set the width to match the parent.
                width = widthSize;
            }

            SetMeasuredDimension(widthMode == MeasureSpecMode.Exactly ? widthSize : width,
                                 heightMode == MeasureSpecMode.Exactly ? heightSize : height);
        }
Пример #20
0
        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            CoreUtility.ExecuteMethod("OnMeasure", delegate()
            {
                LayoutParams parameters = LayoutParameters;

                MeasureSpecMode widthMode  = MeasureSpec.GetMode(widthMeasureSpec);
                MeasureSpecMode heightMode = MeasureSpec.GetMode(heightMeasureSpec);

                int desiredWidth  = 0;
                int desiredHeight = 0;

                // first find the largest child
                for (int i = 0; i < ChildCount; i++)
                {
                    View child = GetChildAt(i);

                    if (i == 0)
                    {
                        LayoutParams childParams = child.LayoutParameters;
                        childParams.Height       = LayoutParams.MatchParent;
                        child.LayoutParameters   = childParams;
                    }
                    MeasureChild(child, widthMeasureSpec, heightMeasureSpec);

                    if (i != 0)
                    {
                        desiredWidth  = Math.Max(child.MeasuredWidth, desiredWidth);
                        desiredHeight = Math.Max(child.MeasuredHeight, desiredHeight);
                    }
                }
                // create new measure spec using the primary child
                widthMeasureSpec  = MeasureSpec.MakeMeasureSpec(desiredWidth, widthMode);
                heightMeasureSpec = MeasureSpec.MakeMeasureSpec(desiredHeight, heightMode);

                int measuredWidth  = MeasureSpec.GetSize(widthMeasureSpec);
                int measuredHeight = MeasureSpec.GetSize(heightMeasureSpec);

                for (int i = 0; i < ChildCount; i++)
                {
                    View child = GetChildAt(i);
                    LayoutParams childParams = child.LayoutParameters;

                    if (childParams != null)
                    {
                        if (childParams.Height == LayoutParams.MatchParent)
                        {
                            child.SetMinimumHeight(measuredHeight);
                        }

                        if (childParams.Width == LayoutParams.MatchParent)
                        {
                            child.SetMinimumWidth(measuredWidth);
                        }
                    }

                    MeasureChild(child, widthMeasureSpec, heightMeasureSpec);
                    desiredWidth  = Math.Max(child.MeasuredWidth, desiredWidth);
                    desiredHeight = Math.Max(child.MeasuredHeight, desiredHeight);
                }

                // taking accounts of padding
                desiredWidth  += PaddingLeft + PaddingRight;
                desiredHeight += PaddingTop + PaddingBottom;

                // adjust desired width
                if (widthMode == MeasureSpecMode.Exactly)
                {
                    desiredWidth = measuredWidth;
                }
                else
                {
                    if (parameters.Width == LayoutParams.MatchParent)
                    {
                        desiredWidth = measuredWidth;
                    }

                    if (widthMode == MeasureSpecMode.AtMost)
                    {
                        desiredWidth = (desiredWidth > measuredWidth) ? measuredWidth : desiredWidth;
                    }
                }

                // adjust desired height
                if (heightMode == MeasureSpecMode.Exactly)
                {
                    desiredHeight = measuredHeight;
                }
                else
                {
                    if (parameters.Height == LayoutParams.MatchParent)
                    {
                        desiredHeight = measuredHeight;
                    }

                    if (heightMode == MeasureSpecMode.AtMost)
                    {
                        desiredHeight = (desiredHeight > measuredHeight) ? measuredHeight : desiredHeight;
                    }
                }

                SetMeasuredDimension(desiredWidth, desiredHeight);
            });
        }
 internal static int MakeMeasureSpec(int width, MeasureSpecMode exactly)
 {
     return((int)(width + exactly));
 }
Пример #22
0
        private String modeToString(MeasureSpecMode mode)
        {
            switch (mode)
            {
                case MeasureSpecMode.AtMost:
                    return "AT MOST";
                case MeasureSpecMode.Exactly:
                    return "EXACTLY";
                case MeasureSpecMode.Unspecified:
                    return "UNSPECIFIED";
            }

            return "ERROR";
        }
Пример #23
0
        protected override (float measuredWidth, float measuredHeight) OnMesure(float widthSpec, float heightSpec, MeasureSpecMode mode)
        {
            if (Children.Count > 0)
            {
                OnLayout();
                var child = Children[0];
                child.Mesure(widthSpec, heightSpec, mode);
                OnLayout();
                return(child.CalculatedWidth, child.CalculatedHeight);
            }

            return(base.OnMesure(widthSpec, heightSpec, mode));
        }
 /// <summary>
 /// Based on internal class Xamarin.Forms.Platform.Android.MeasureSpecFactory
 /// </summary>
 /// <param name="size">The size.</param>
 /// <param name="mode">The mode.</param>
 /// <returns></returns>
 private static int MakeMeasureSpec ( int size , MeasureSpecMode mode ) {
     return ( int ) ( size + mode );
 }
Пример #25
0
        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            if (!hasDrawable)
            {
                base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
                return;
            }

            Drawable d         = Drawable;
            int      drawableW = getDrawableWidth(d);
            int      drawableH = getDrawableHeight(d);

            int pWidth  = MeasureSpec.GetSize(widthMeasureSpec);
            int pHeight = MeasureSpec.GetSize(heightMeasureSpec);

            MeasureSpecMode widthMode  = MeasureSpec.GetMode(widthMeasureSpec);
            MeasureSpecMode heightMode = MeasureSpec.GetMode(heightMeasureSpec);

            int width  = 0;
            int height = 0;

            ViewGroup.LayoutParams p = LayoutParameters;

            if (p == null)
            {
                p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
            }

            if (p.Width == ViewGroup.LayoutParams.MatchParent)
            {
                if (widthMode == MeasureSpecMode.Unspecified)
                {
                    width = drawableW;
                }
                else
                {
                    width = pWidth;
                }
            }
            else
            {
                if (widthMode == MeasureSpecMode.Exactly)
                {
                    width = pWidth;
                }
                else if (widthMode == MeasureSpecMode.AtMost)
                {
                    width = drawableW > pWidth ? pWidth : drawableW;
                }
                else
                {
                    width = drawableW;
                }
            }

            if (p.Height == ViewGroup.LayoutParams.MatchParent)
            {
                if (heightMode == MeasureSpecMode.Unspecified)
                {
                    height = drawableH;
                }
                else
                {
                    height = pHeight;
                }
            }
            else
            {
                if (heightMode == MeasureSpecMode.Exactly)
                {
                    height = pHeight;
                }
                else if (heightMode == MeasureSpecMode.AtMost)
                {
                    height = drawableH > pHeight ? pHeight : drawableH;
                }
                else
                {
                    height = drawableH;
                }
            }

            if (mAdjustViewBounds && (float)drawableW / drawableH != (float)width / height)
            {
                float hScale = (float)height / drawableH;
                float wScale = (float)width / drawableW;

                float scale = hScale < wScale ? hScale : wScale;
                width  = p.Width == ViewGroup.LayoutParams.MatchParent ? width : (int)(drawableW * scale);
                height = p.Height == ViewGroup.LayoutParams.MatchParent ? height : (int)(drawableH * scale);
            }

            SetMeasuredDimension(width, height);
        }
Пример #26
0
 public static int MakeMeasureSpec(int size, MeasureSpecMode mode)
 {
     return(size + (int)mode);
 }
        protected override void OnLayout(bool changed, int l, int t, int r, int b)
        {
            mLines.Clear();
            mLineHeights.Clear();
            mLineMargins.Clear();

            int width  = Width;
            int height = Height;

            int linesSum = PaddingTop;

            int          lineWidth  = 0;
            int          lineHeight = 0;
            IList <View> lineViews  = new List <View>();

            float horizontalGravityFactor;

            switch ((mGravity & GravityFlags.HorizontalGravityMask))
            {
            case GravityFlags.Left:
            default:
                horizontalGravityFactor = 0;
                break;

            case GravityFlags.CenterHorizontal:
                horizontalGravityFactor = .5f;
                break;

            case GravityFlags.Right:
                horizontalGravityFactor = 1;
                break;
            }

            for (int i = 0; i < ChildCount; i++)
            {
                View child = GetChildAt(i);

                if (child.Visibility == ViewStates.Gone)
                {
                    continue;
                }

                CustomLayoutParams lp = (CustomLayoutParams)child.LayoutParameters;

                int childWidth  = child.MeasuredWidth + lp.LeftMargin + lp.RightMargin;
                int childHeight = child.MeasuredHeight + lp.BottomMargin + lp.TopMargin;

                if (lineWidth + childWidth > width)
                {
                    mLineHeights.Add(lineHeight);
                    mLines.Add(lineViews);
                    mLineMargins.Add((int)((width - lineWidth) * horizontalGravityFactor) + PaddingLeft);

                    linesSum += lineHeight;

                    lineHeight = 0;
                    lineWidth  = 0;
                    lineViews  = new List <View>();
                }

                lineWidth += childWidth;
                lineHeight = Math.Max(lineHeight, childHeight);
                lineViews.Add(child);
            }

            mLineHeights.Add(lineHeight);
            mLines.Add(lineViews);
            mLineMargins.Add((int)((width - lineWidth) * horizontalGravityFactor) + PaddingLeft);

            linesSum += lineHeight;

            int verticalGravityMargin = 0;

            switch ((mGravity & GravityFlags.VerticalGravityMask))
            {
            case GravityFlags.Top:
            default:
                break;

            case GravityFlags.CenterVertical:
                verticalGravityMargin = (height - linesSum) / 2;
                break;

            case GravityFlags.Bottom:
                verticalGravityMargin = height - linesSum;
                break;
            }

            int numLines = mLines.Count;

            int left;
            int top = PaddingTop;

            for (int i = 0; i < numLines; i++)
            {
                lineHeight = mLineHeights[i];
                lineViews  = mLines[i];
                left       = mLineMargins[i];

                int children = lineViews.Count;

                for (int j = 0; j < children; j++)
                {
                    View child = lineViews[j];

                    if (child.Visibility == ViewStates.Gone)
                    {
                        continue;
                    }

                    CustomLayoutParams lp = (CustomLayoutParams)child.LayoutParameters;

                    // if height is match_parent we need to remeasure child to line height
                    if (lp.Height == LayoutParams.MatchParent)
                    {
                        MeasureSpecMode childWidthMode = MeasureSpecMode.AtMost;
                        int             childWidthSize = lineWidth;

                        if (lp.Width == LayoutParams.MatchParent)
                        {
                            childWidthMode = MeasureSpecMode.Exactly;
                        }
                        else if (lp.Width >= 0)
                        {
                            childWidthMode = MeasureSpecMode.Exactly;
                            childWidthSize = lp.Width;
                        }

                        child.Measure(MeasureSpec.MakeMeasureSpec(childWidthSize, childWidthMode), MeasureSpec.MakeMeasureSpec(lineHeight - lp.TopMargin - lp.BottomMargin, MeasureSpecMode.Exactly));
                    }

                    int childWidth  = child.MeasuredWidth;
                    int childHeight = child.MeasuredHeight;

                    int gravityMargin = 0;

                    if (Android.Views.Gravity.IsVertical(lp.gravity))
                    {
                        switch (lp.gravity)
                        {
                        case GravityFlags.Top:
                        default:
                            break;

                        case GravityFlags.CenterVertical:
                        case GravityFlags.Center:
                            gravityMargin = (lineHeight - childHeight - lp.TopMargin - lp.BottomMargin) / 2;
                            break;

                        case GravityFlags.Bottom:
                            gravityMargin = lineHeight - childHeight - lp.TopMargin - lp.BottomMargin;
                            break;
                        }
                    }

                    child.Layout(left + lp.LeftMargin, top + lp.TopMargin + gravityMargin + verticalGravityMargin, left + childWidth + lp.LeftMargin, top + childHeight + lp.TopMargin + gravityMargin + verticalGravityMargin);

                    left += childWidth + lp.LeftMargin + lp.RightMargin;
                }

                top += lineHeight;
            }
        }