コード例 #1
0
        protected override void OnMeasure(
            int widthMeasureSpec,
            int heightMeasureSpec)
        {
            Size availableSize = new Size(
                MeasureSpec.GetSize(widthMeasureSpec) - this.PaddingLeft - this.PaddingRight,
                MeasureSpec.GetSize(heightMeasureSpec) - this.PaddingTop - this.PaddingBottom);

#if DEBUG
            var widthMode  = MeasureSpec.GetMode(widthMeasureSpec);
            var heightMode = MeasureSpec.GetMode(heightMeasureSpec);
#endif

            // Keep track of the space used by children
            double usedLeft   = 0;
            double usedTop    = 0;
            double usedRight  = 0;
            double usedBottom = 0;

            // Keep track of the starting point for measuring each dimension
            // in support of overlay regions
            double startLeft   = 0;
            double startTop    = 0;
            double startRight  = 0;
            double startBottom = 0;

            double minWidth  = 0;
            double minHeight = 0;

            var remainingSize = availableSize;

            int childState = 0;
            int childCount = this.ChildCount;

            for (int i = 0; i < childCount; i += 1)
            {
                AndroidView child = this.GetChildAt(i);

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

                var layoutParams = (LayoutParams)child.LayoutParameters;

                var dockRegion = layoutParams.DockRegion;

                // Arrange all but the center elements
                if (dockRegion != DockRegion.CenterOverlay)
                {
                    LayoutAxis axis = GetLayoutAxis(dockRegion);

                    // Save the child width and height
                    int childWidth  = layoutParams.Width;
                    int childHeight = layoutParams.Height;

                    // Overwrite child MatchParent size on the stacking axis
                    if ((axis == LayoutAxis.Horizontal) && (childWidth == LayoutParams.MatchParent))
                    {
                        childWidth = LayoutParams.WrapContent;
                    }
                    else if ((axis == LayoutAxis.Vertical) && (childHeight == LayoutParams.MatchParent))
                    {
                        childHeight = LayoutParams.WrapContent;
                    }

                    // Measure the child
                    this.MeasureChildWithMarginsOverride(
                        child,
                        widthMeasureSpec,
                        (int)(usedLeft + usedRight),
                        childWidth,
                        heightMeasureSpec,
                        (int)(usedTop + usedBottom),
                        childHeight);

                    Size childUsedSize = GetChildUsedSizeWithMargins(child, layoutParams);

                    if (dockRegion == DockRegion.Left)
                    {
                        usedLeft   = Math.Max(usedLeft, startLeft + childUsedSize.Width);
                        startLeft += childUsedSize.Width;
                        minHeight  = Math.Max(minHeight, usedTop + usedBottom + childUsedSize.Height);
                    }
                    else if (dockRegion == DockRegion.LeftOverlay)
                    {
                        usedLeft  = Math.Max(usedLeft, startLeft + childUsedSize.Width);
                        minHeight = Math.Max(minHeight, usedTop + usedBottom + childUsedSize.Height);
                    }
                    else if (dockRegion == DockRegion.Top)
                    {
                        usedTop   = Math.Max(usedTop, startTop + childUsedSize.Height);
                        startTop += childUsedSize.Height;
                        minWidth  = Math.Max(minWidth, usedLeft + usedRight + childUsedSize.Width);
                    }
                    else if (dockRegion == DockRegion.TopOverlay)
                    {
                        usedTop  = Math.Max(usedTop, startTop + childUsedSize.Height);
                        minWidth = Math.Max(minWidth, usedLeft + usedRight + childUsedSize.Width);
                    }
                    else if (dockRegion == DockRegion.Right)
                    {
                        usedRight   = Math.Max(usedRight, startRight + childUsedSize.Width);
                        startRight += childUsedSize.Width;
                        minHeight   = Math.Max(minHeight, usedTop + usedBottom + childUsedSize.Height);
                    }
                    else if (dockRegion == DockRegion.RightOverlay)
                    {
                        usedRight = Math.Max(usedRight, startRight + childUsedSize.Width);
                        minHeight = Math.Max(minHeight, usedTop + usedBottom + childUsedSize.Height);
                    }
                    else if (dockRegion == DockRegion.Bottom)
                    {
                        usedBottom   = Math.Max(usedBottom, startBottom + childUsedSize.Height);
                        startBottom += childUsedSize.Height;
                        minWidth     = Math.Max(minWidth, usedLeft + usedRight + childUsedSize.Width);
                    }
                    else if (dockRegion == DockRegion.BottomOverlay)
                    {
                        usedBottom = Math.Max(usedBottom, startBottom + childUsedSize.Height);
                        minWidth   = Math.Max(minWidth, usedLeft + usedRight + childUsedSize.Width);
                    }
                    else
                    {
                        throw new NotSupportedException("Unsupported dock region.");
                    }

                    // Update the remaining size
                    remainingSize = new Size(
                        (float)Math.Max(0, availableSize.Width - startLeft - startRight),
                        (float)Math.Max(0, availableSize.Height - startTop - startBottom));

                    // Update the child state
                    childState = CombineMeasuredStates(childState, child.MeasuredState);
                }
            }

            // Measure the center elements now that the center size has been completely calculated
            Size usedCenterSize = new Size(0, 0);
            for (int i = 0; i < childCount; i += 1)
            {
                AndroidView child = this.GetChildAt(i);

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

                var layoutParams = (LayoutParams)child.LayoutParameters;

                var dockRegion = layoutParams.DockRegion;

                // Arrange only the center elements
                if (dockRegion != DockRegion.CenterOverlay)
                {
                    continue;
                }

                this.MeasureChildWithMarginsOverride(
                    child,
                    widthMeasureSpec,
                    (int)(usedLeft + usedRight),
                    layoutParams.Width,
                    heightMeasureSpec,
                    (int)(usedTop + usedBottom),
                    layoutParams.Height);

                Size childUsedSize = GetChildUsedSizeWithMargins(child, layoutParams);

                usedCenterSize = new Size(
                    Math.Max(usedCenterSize.Width, childUsedSize.Width),
                    Math.Max(usedCenterSize.Height, childUsedSize.Height));

                childState = CombineMeasuredStates(childState, child.MeasuredState);
            }

            // Calculate the total size used by children
            Size usedSize = new Size(
                (float)Math.Max(minWidth, usedLeft + usedRight + usedCenterSize.Width),
                (float)Math.Max(minHeight, usedTop + usedBottom + usedCenterSize.Height));

            // Default the final size to the size used by children
            var finalSize = new Size(
                usedSize.Width + this.PaddingLeft + this.PaddingRight,
                usedSize.Height + this.PaddingTop + this.PaddingBottom);

            int dimensionX = ResolveSizeAndState((int)finalSize.Width, widthMeasureSpec, childState);
            int dimensionY = ResolveSizeAndState((int)finalSize.Height, heightMeasureSpec, childState);

            this.SetMeasuredDimension(dimensionX, dimensionY);
        }
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            int resWidth  = 0;
            int resHeight = 0;

            int width     = MeasureSpec.GetSize(widthMeasureSpec);
            int widthMode = (int)MeasureSpec.GetMode(widthMeasureSpec);

            int height     = MeasureSpec.GetSize(heightMeasureSpec);
            int heightMode = (int)MeasureSpec.GetMode(heightMeasureSpec);

            if (widthMode != (int)MeasureSpecMode.Exactly ||
                heightMode != (int)MeasureSpecMode.Exactly)
            {
                resWidth = getSuggestedMinimumWidth();
                resWidth = resWidth == 0 ? getDefaultWidth() : resWidth;

                resHeight = getSuggestedMinimumHeight();
                resHeight = resHeight == 0 ? getDefaultWidth() : resHeight;
            }
            else
            {
                resWidth = resHeight = Math.Min(width, height);
            }

            SetMeasuredDimension(resWidth, resHeight);

            mRadius = Math.Max(GetMeasuredWidth(), GetMeasuredHeight());

            int count = getChildCount();
            // menu ite
            int childSize = (int)(mRadius * RADIO_DEFAULT_CHILD_DIMENSION);
            // menu item
            int childMode = (int)MeasureSpecMode.Exactly;

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

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

                // menu item
                int makeMeasureSpec = -1;

                if (child.Id == Resource.Id.circle_menu_item_center)
                {
                    makeMeasureSpec = MeasureSpec.MakeMeasureSpec(
                        (int)(mRadius * RADIO_DEFAULT_CENTERITEM_DIMENSION),
                        MeasureSpec.GetMode(childMode));
                }
                else
                {
                    makeMeasureSpec = (int)MeasureSpec.MakeMeasureSpec(childSize,
                                                                       MeasureSpec.GetMode(childMode));
                }
                child.Measure(makeMeasureSpec, makeMeasureSpec);
            }

            mPadding = RADIO_PADDING_LAYOUT * mRadius;
        }
コード例 #3
0
        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            base.OnMeasure(widthMeasureSpec, heightMeasureSpec);

            int widthSize  = MeasureSpec.GetSize(widthMeasureSpec);
            int widthMode  = (int)MeasureSpec.GetMode(widthMeasureSpec);
            int heightSize = MeasureSpec.GetSize(heightMeasureSpec);
            int heightMode = (int)MeasureSpec.GetMode(heightMeasureSpec);

            mHorizontalSpacingForRow.Clear();
            mChildNumForRow.Clear();
            mHeightForRow.Clear();

            int  measuredHeight = 0, measuredWidth = 0, childCount = ChildCount;
            int  rowWidth = 0, maxChildHeightInRow = 0, childNumInRow = 0;
            int  rowSize      = widthSize - PaddingLeft - PaddingRight;
            bool allowFlow    = widthMode != 0 && mFlow;
            int  childSpacing = mChildSpacing == SPACING_AUTO && widthMode == (int)MeasureSpecMode.Unspecified
                    ? 0 : mChildSpacing;
            float tmpSpacing = childSpacing == SPACING_AUTO ? (int)MeasureSpecMode.Unspecified : childSpacing;

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

                LayoutParams childParams      = child.LayoutParameters;
                int          horizontalMargin = 0;
                int          verticalMargin   = 0;
                if (childParams is MarginLayoutParams)
                {
                    MeasureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, measuredHeight);
                    MarginLayoutParams marginParams = (MarginLayoutParams)childParams;
                    horizontalMargin = marginParams.LeftMargin + marginParams.RightMargin;
                    verticalMargin   = marginParams.TopMargin + marginParams.BottomMargin;
                }
                else
                {
                    MeasureChild(child, widthMeasureSpec, heightMeasureSpec);
                }

                int childWidth  = child.MeasuredWidth + horizontalMargin;
                int childHeight = child.MeasuredHeight + verticalMargin;
                if (allowFlow && rowWidth + childWidth > rowSize)
                { // Need flow to next row
                  // Save parameters for current row
                    mHorizontalSpacingForRow.Add(
                        GetSpacingForRow(childSpacing, rowSize, rowWidth, childNumInRow));
                    mChildNumForRow.Add(childNumInRow);
                    mHeightForRow.Add(maxChildHeightInRow);
                    if (mHorizontalSpacingForRow.Count <= mMaxRows)
                    {
                        measuredHeight += maxChildHeightInRow;
                    }
                    measuredWidth = Math.Max(measuredWidth, rowWidth);

                    // Place the child view to next row
                    childNumInRow       = 1;
                    rowWidth            = childWidth + (int)tmpSpacing;
                    maxChildHeightInRow = childHeight;
                }
                else
                {
                    childNumInRow++;
                    rowWidth           += (int)(childWidth + tmpSpacing);
                    maxChildHeightInRow = Math.Max(maxChildHeightInRow, childHeight);
                }
            }

            // Measure remaining child views in the last row
            if (mChildSpacingForLastRow == SPACING_ALIGN)
            {
                // For SPACING_ALIGN, use the same spacing from the row above if there is more than one
                // row.
                if (mHorizontalSpacingForRow.Count >= 1)
                {
                    mHorizontalSpacingForRow.Add(
                        mHorizontalSpacingForRow[mHorizontalSpacingForRow.Count - 1]);
                }
                else
                {
                    mHorizontalSpacingForRow.Add(
                        GetSpacingForRow(childSpacing, rowSize, rowWidth, childNumInRow));
                }
            }
            else if (mChildSpacingForLastRow != SPACING_UNDEFINED)
            {
                // For SPACING_AUTO and specific DP values, apply them to the spacing strategy.
                mHorizontalSpacingForRow.Add(
                    GetSpacingForRow(mChildSpacingForLastRow, rowSize, rowWidth, childNumInRow));
            }
            else
            {
                // For SPACING_UNDEFINED, apply childSpacing to the spacing strategy for the last row.
                mHorizontalSpacingForRow.Add(
                    GetSpacingForRow(childSpacing, rowSize, rowWidth, childNumInRow));
            }

            mChildNumForRow.Add(childNumInRow);
            mHeightForRow.Add(maxChildHeightInRow);
            if (mHorizontalSpacingForRow.Count <= mMaxRows)
            {
                measuredHeight += maxChildHeightInRow;
            }
            measuredWidth = Math.Max(measuredWidth, rowWidth);

            if (childSpacing == SPACING_AUTO)
            {
                measuredWidth = widthSize;
            }
            else if (widthMode == 0)
            {
                measuredWidth = measuredWidth + PaddingLeft + PaddingRight;
            }
            else
            {
                measuredWidth = Math.Min(measuredWidth + PaddingLeft + PaddingRight, widthSize);
            }

            measuredHeight += PaddingTop + PaddingBottom;
            int rowNum = Math.Min(mHorizontalSpacingForRow.Count, mMaxRows);

            float rowSpacing = (mRowSpacing == SPACING_AUTO && heightMode == (int)MeasureSpecMode.Unspecified) ? 0 : mRowSpacing;

            if (rowSpacing == SPACING_AUTO)
            {
                if (rowNum > 1)
                {
                    mAdjustedRowSpacing = (heightSize - measuredHeight) / (rowNum - 1);
                }
                else
                {
                    mAdjustedRowSpacing = 0;
                }
                measuredHeight = heightSize;
            }
            else
            {
                mAdjustedRowSpacing = rowSpacing;
                if (rowNum > 1)
                {
                    measuredHeight = heightMode == (int)MeasureSpecMode.Unspecified
                            ? ((int)(measuredHeight + mAdjustedRowSpacing * (rowNum - 1)))
                            : (Math.Min((int)(measuredHeight + mAdjustedRowSpacing * (rowNum - 1)),
                                        heightSize));
                }
            }

            measuredWidth  = widthMode == (int)MeasureSpecMode.Exactly ? widthSize : measuredWidth;
            measuredHeight = heightMode == (int)MeasureSpecMode.Exactly ? heightSize : measuredHeight;
            SetMeasuredDimension(measuredWidth, measuredHeight);
        }
コード例 #4
0
        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            var widthMode  = MeasureSpec.GetMode(widthMeasureSpec);
            var widthSize  = MeasureSpec.GetSize(widthMeasureSpec);
            var heightMode = MeasureSpec.GetMode(heightMeasureSpec);
            var heightSize = MeasureSpec.GetSize(heightMeasureSpec);

            if (widthMode != MeasureSpecMode.Exactly)
            {
                throw new InvalidOperationException("Width must have an exact value or match_parent");
            }
            if (heightMode != MeasureSpecMode.Exactly)
            {
                throw new InvalidOperationException("Height must have an exact value or match_parent");
            }

            var layoutHeight = heightSize - PaddingTop - PaddingBottom;
            var panelHeight  = _panelHeight;

            if (ChildCount > 2)
            {
                Log.Error(Tag, "OnMeasure: More than two child views are not supported.");
            }
            else
            {
                panelHeight = 0;
            }

            _slideableView = null;
            _canSlide      = false;

            for (var i = 0; i < ChildCount; i++)
            {
                var child = GetChildAt(i);
                var lp    = (LayoutParams)child.LayoutParameters;

                var height = layoutHeight;
                if (child.Visibility == ViewStates.Gone)
                {
                    lp.DimWhenOffset = false;
                    continue;
                }

                if (i == 1)
                {
                    lp.Slideable     = true;
                    lp.DimWhenOffset = true;
                    _slideableView   = child;
                    _canSlide        = true;
                }
                else
                {
                    if (!OverlayContent)
                    {
                        height -= panelHeight;
                    }
                }

                int childWidthSpec;
                if (lp.Width == ViewGroup.LayoutParams.WrapContent)
                {
                    childWidthSpec = MeasureSpec.MakeMeasureSpec(widthSize, MeasureSpecMode.AtMost);
                }
                else if (lp.Width == ViewGroup.LayoutParams.MatchParent)
                {
                    childWidthSpec = MeasureSpec.MakeMeasureSpec(widthSize, MeasureSpecMode.Exactly);
                }
                else
                {
                    childWidthSpec = MeasureSpec.MakeMeasureSpec(lp.Width, MeasureSpecMode.Exactly);
                }

                int childHeightSpec;
                if (lp.Height == ViewGroup.LayoutParams.WrapContent)
                {
                    childHeightSpec = MeasureSpec.MakeMeasureSpec(height, MeasureSpecMode.AtMost);
                }
                else if (lp.Height == ViewGroup.LayoutParams.MatchParent)
                {
                    childHeightSpec = MeasureSpec.MakeMeasureSpec(height, MeasureSpecMode.Exactly);
                }
                else
                {
                    childHeightSpec = MeasureSpec.MakeMeasureSpec(lp.Height, MeasureSpecMode.Exactly);
                }

                child.Measure(childWidthSpec, childHeightSpec);
            }
            SetMeasuredDimension(widthSize, heightSize);
        }
コード例 #5
0
 protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
 {
     int targetWidth = outerInstance.MeasuredWidth - outerInstance.PaddingLeft - outerInstance.PaddingRight;
     widthMeasureSpec = MeasureSpec.MakeMeasureSpec(targetWidth, MeasureSpec.GetMode(widthMeasureSpec));
     base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
 }
コード例 #6
0
        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));
        }
コード例 #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);
        }
コード例 #8
0
ファイル: PhotoView.cs プロジェクト: rainmanzj/ZhihuFind
        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);
        }
コード例 #9
0
        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            ///	Assert(MeasureSpec.GetMode(widthMeasureSpec) != Android.Views.MeasureSpecMode.Unspecified);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
            int width  = MeasureSpec.GetSize(widthMeasureSpec) - PaddingLeft - PaddingRight;
            int height = MeasureSpec.GetSize(heightMeasureSpec) - PaddingTop - PaddingBottom;
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final int count = getChildCount();
            int count = ChildCount;
            int xpos  = PaddingLeft;
            int ypos  = PaddingTop;
            int childHeightMeasureSpec;

            if (MeasureSpec.GetMode(heightMeasureSpec) == Android.Views.MeasureSpecMode.AtMost)
            {
                childHeightMeasureSpec = MeasureSpec.MakeMeasureSpec(height, Android.Views.MeasureSpecMode.AtMost);
            }
            else
            {
                childHeightMeasureSpec = MeasureSpec.MakeMeasureSpec(0, Android.Views.MeasureSpecMode.Unspecified);
            }
            MHeight = 0;
            int cols = 0;

            MCols = 0;
            for (int i = 0; i < count; i++)
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final Android.view.View child = getChildAt(i);
                View child = GetChildAt(i);
                if (child.Visibility != Android.Views.ViewStates.Gone)
                {
                    child.Measure(MeasureSpec.MakeMeasureSpec(width, Android.Views.MeasureSpecMode.AtMost), childHeightMeasureSpec);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int childw = child.getMeasuredWidth();
                    int childw = child.MeasuredWidth;
                    MHeight = Math.Max(MHeight, child.MeasuredHeight + PAD_V);
                    if (xpos + childw > width)
                    {
                        xpos  = PaddingLeft;
                        ypos += MHeight;
                        MCols = Math.Max(MCols, cols);
                        cols  = 0;
                    }
                    else
                    {
                        cols++;
                    }
                    xpos += childw + PAD_H;
                }
            }

            MCols = Math.Max(MCols, cols);

            if (MeasureSpec.GetMode(heightMeasureSpec) == Android.Views.MeasureSpecMode.Unspecified)
            {
                height = ypos + MHeight;
            }
            else if (MeasureSpec.GetMode(heightMeasureSpec) == Android.Views.MeasureSpecMode.AtMost)
            {
                if (ypos + MHeight < height)
                {
                    height = ypos + MHeight;
                }
            }
            height += 5;  // Fudge to avoid clipping bottom of last row.
            SetMeasuredDimension(width, height);
        }                 // end onMeasure()
コード例 #10
0
 public static MeasureSpecMode GetMode(this int measureSpec)
 {
     return(MeasureSpec.GetMode(measureSpec));
 }
コード例 #11
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);
        }
コード例 #12
0
        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            var sizeWidth  = MeasureSpec.GetSize(widthMeasureSpec) - PaddingRight - PaddingLeft;
            var sizeHeight = MeasureSpec.GetSize(heightMeasureSpec) - PaddingRight - PaddingLeft;

            var modeWidth  = (int)MeasureSpec.GetMode(widthMeasureSpec);
            var modeHeight = (int)MeasureSpec.GetMode(heightMeasureSpec);

            int size;
            int mode;

            if (Orientation == Horizontal)
            {
                size = sizeWidth;
                mode = modeWidth;
            }
            else
            {
                size = sizeHeight;
                mode = modeHeight;
            }

            var lineThicknessWithSpacing = 0;
            var lineThickness            = 0;
            var lineLengthWithSpacing    = 0;

            var prevLinePosition = 0;

            var controlMaxLength    = 0;
            var controlMaxThickness = 0;

            var count = ChildCount;

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

                child.Measure(
                    MeasureSpec.MakeMeasureSpec(sizeWidth, (MeasureSpecMode)modeWidth == MeasureSpecMode.Exactly ? MeasureSpecMode.AtMost : (MeasureSpecMode)modeWidth),
                    MeasureSpec.MakeMeasureSpec(sizeHeight, (MeasureSpecMode)modeHeight == MeasureSpecMode.Exactly ? MeasureSpecMode.AtMost : (MeasureSpecMode)modeHeight)
                    );

                var lp = (LayoutParams)child.LayoutParameters;

                var hSpacing = GetHorizontalSpacing(lp);
                var vSpacing = GetVerticalSpacing(lp);

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

                int childLength;
                int childThickness;
                int spacingLength;
                int spacingThickness;

                if (Orientation == Horizontal)
                {
                    childLength      = childWidth;
                    childThickness   = childHeight;
                    spacingLength    = hSpacing;
                    spacingThickness = vSpacing;
                }
                else
                {
                    childLength      = childHeight;
                    childThickness   = childWidth;
                    spacingLength    = vSpacing;
                    spacingThickness = hSpacing;
                }

                var lineLength = lineLengthWithSpacing + childLength;
                lineLengthWithSpacing = lineLength + spacingLength;

                var newLine = lp.NewLine || ((MeasureSpecMode)mode != MeasureSpecMode.Unspecified && lineLength > size);
                if (newLine)
                {
                    prevLinePosition = prevLinePosition + lineThicknessWithSpacing;

                    lineThickness            = childThickness;
                    lineLength               = childLength;
                    lineThicknessWithSpacing = childThickness + spacingThickness;
                    lineLengthWithSpacing    = lineLength + spacingLength;
                }

                lineThicknessWithSpacing = Math.Max(lineThicknessWithSpacing, childThickness + spacingThickness);
                lineThickness            = Math.Max(lineThickness, childThickness);

                int posX;
                int posY;
                if (Orientation == Horizontal)
                {
                    posX = PaddingLeft + lineLength - childLength;
                    posY = PaddingTop + prevLinePosition;
                }
                else
                {
                    posX = PaddingLeft + prevLinePosition;
                    posY = PaddingTop + lineLength - childHeight;
                }
                lp.SetPosition(posX, posY);

                controlMaxLength    = Math.Max(controlMaxLength, lineLength);
                controlMaxThickness = prevLinePosition + lineThickness;
            }

            if (Orientation == Horizontal)
            {
                SetMeasuredDimension(ResolveSize(controlMaxLength, widthMeasureSpec), ResolveSize(controlMaxThickness, heightMeasureSpec));
            }
            else
            {
                SetMeasuredDimension(ResolveSize(controlMaxThickness, widthMeasureSpec), ResolveSize(controlMaxLength, heightMeasureSpec));
            }
        }
コード例 #13
0
        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            try
            {
                var widthMode  = (int)MeasureSpec.GetMode(widthMeasureSpec);
                int widthSize  = MeasureSpec.GetSize(widthMeasureSpec);
                var heightMode = (int)MeasureSpec.GetMode(heightMeasureSpec);
                int heightSize = MeasureSpec.GetSize(heightMeasureSpec);

                if (mBitmap != null)
                {
                    base.OnMeasure(widthMeasureSpec, heightMeasureSpec);

                    // Bypasses a baffling bug when used within a ScrollView, where
                    // heightSize is set to 0.
                    if (heightSize == 0)
                    {
                        heightSize = mBitmap.Height;
                    }

                    int desiredWidth;
                    int desiredHeight;

                    double viewToBitmapWidthRatio  = Double.PositiveInfinity;
                    double viewToBitmapHeightRatio = Double.PositiveInfinity;

                    // Checks if either width or height needs to be fixed
                    if (widthSize < mBitmap.Width)
                    {
                        viewToBitmapWidthRatio = widthSize / (double)mBitmap.Width;
                    }
                    if (heightSize < mBitmap.Height)
                    {
                        viewToBitmapHeightRatio = heightSize / (double)mBitmap.Height;
                    }

                    // If either needs to be fixed, choose smallest ratio and calculate
                    // from there
                    if (viewToBitmapWidthRatio != Double.PositiveInfinity ||
                        viewToBitmapHeightRatio != Double.PositiveInfinity)
                    {
                        if (viewToBitmapWidthRatio <= viewToBitmapHeightRatio)
                        {
                            desiredWidth  = widthSize;
                            desiredHeight = (int)(mBitmap.Height * viewToBitmapWidthRatio);
                        }
                        else
                        {
                            desiredHeight = heightSize;
                            desiredWidth  = (int)(mBitmap.Width * viewToBitmapHeightRatio);
                        }
                    }

                    // Otherwise, the picture is within frame layout bounds. Desired
                    // width is
                    // simply picture size
                    else
                    {
                        desiredWidth  = mBitmap.Width;
                        desiredHeight = mBitmap.Height;
                    }

                    int width  = GetOnMeasureSpec(widthMode, widthSize, desiredWidth);
                    int height = GetOnMeasureSpec(heightMode, heightSize, desiredHeight);

                    mLayoutWidth  = width;
                    mLayoutHeight = height;

                    Rect bitmapRect = ImageViewUtil.getBitmapRectCenterInside(mBitmap.Width,
                                                                              mBitmap.Height,
                                                                              mLayoutWidth,
                                                                              mLayoutHeight);
                    mCropOverlayView.SetBitmapRect(bitmapRect);

                    // MUST CALL THIS
                    SetMeasuredDimension(mLayoutWidth, mLayoutHeight);
                }
                else
                {
                    mCropOverlayView.SetBitmapRect(EMPTY_RECT);
                    SetMeasuredDimension(widthSize, heightSize);
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }
コード例 #14
0
ファイル: GridView.cs プロジェクト: ZuZuK/GridView
        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            PremeasureActions();

            var widthMode  = MeasureSpec.GetMode(widthMeasureSpec);
            var width      = MeasureSpec.GetSize(widthMeasureSpec);
            var heightMode = MeasureSpec.GetMode(heightMeasureSpec);
            var height     = MeasureSpec.GetSize(heightMeasureSpec);

            // sizes without fixed columns
            double availableWidth  = width;
            double availableHeight = height;

            // compute size for auto and star definitions
            foreach (var columnInfo in _columnsInfo.Values)
            {
                columnInfo.Unmeasure();
            }
            foreach (var rowInfo in _rowsInfo.Values)
            {
                rowInfo.Unmeasure();
            }
            // measure all views that we can measure at start
            MeasureFirstStageViews(width, widthMode, height, heightMode);

            if (widthMode == MeasureSpecMode.Unspecified)
            {
                double maxLength     = 0;
                double maxStarLength = 0;
                foreach (var columnInfo in _columnsInfo.Values)
                {
                    if (!columnInfo.Definition.Length.IsStar || !columnInfo.IsMeasured)
                    {
                        continue;
                    }
                    if (maxStarLength == 0 ||
                        (_widthStarSum / columnInfo.Definition.Length.Value) * columnInfo.Length > (_widthStarSum / maxStarLength) * maxLength)
                    {
                        maxStarLength = columnInfo.Definition.Length.Value;
                        maxLength     = columnInfo.Length;
                    }
                }
                double fullWidth = (_widthStarSum / maxStarLength) * maxLength;
                foreach (var columnInfo in _columnsInfo.Values)
                {
                    if (columnInfo.Definition.Length.IsStar)
                    {
                        columnInfo.SetLength(fullWidth * (columnInfo.Definition.Length.Value / _widthStarSum));
                    }
                }
            }
            else
            {
                foreach (var columnInfo in _columnsInfo.Values)
                {
                    if (columnInfo.IsMeasured)
                    {
                        availableWidth -= columnInfo.Length;
                    }
                }
                if (availableWidth > 0)
                {
                    foreach (var columnInfo in _columnsInfo.Values)
                    {
                        if (columnInfo.Definition.Length.IsStar)
                        {
                            columnInfo.SetLength((columnInfo.Definition.Length.Value / _widthStarSum) * availableWidth);
                        }
                    }
                }
            }

            if (heightMode == MeasureSpecMode.Unspecified)
            {
                double maxLength     = 0;
                double maxStarLength = 0;
                foreach (var rowInfo in _rowsInfo.Values)
                {
                    if (!rowInfo.Definition.Length.IsStar || !rowInfo.IsMeasured)
                    {
                        continue;
                    }
                    if (maxStarLength == 0 ||
                        (_widthStarSum / rowInfo.Definition.Length.Value) * rowInfo.Length > (_widthStarSum / maxStarLength) * maxLength)
                    {
                        maxStarLength = rowInfo.Definition.Length.Value;
                        maxLength     = rowInfo.Length;
                    }
                }
                double fullHeight = (_heightStarSum / maxStarLength) * maxLength;
                foreach (var rowInfo in _rowsInfo.Values)
                {
                    if (rowInfo.Definition.Length.IsStar)
                    {
                        rowInfo.SetLength(fullHeight * (rowInfo.Definition.Length.Value / _heightStarSum));
                    }
                }
            }
            else
            {
                foreach (var rowInfo in _rowsInfo.Values)
                {
                    if (rowInfo.IsMeasured)
                    {
                        availableHeight -= rowInfo.Length;
                    }
                }
                if (availableHeight > 0)
                {
                    foreach (var rowInfo in _rowsInfo.Values)
                    {
                        if (rowInfo.Definition.Length.IsStar)
                        {
                            rowInfo.SetLength((rowInfo.Definition.Length.Value / _heightStarSum) * availableHeight);
                        }
                    }
                }
            }

            bool isAvailableSizeChanged = false;

            foreach (var columnInfo in _columnsInfo.Values)
            {
                if (columnInfo.Definition.Length.IsAuto)
                {
                    int maxWidth = 0;
                    foreach (var viewInfo in columnInfo.AttachedViews)
                    {
                        if (!viewInfo.IsMeasured)
                        {
                            double viewMeasureHeight = 0;

                            foreach (var row in viewInfo.AssociatedRows)
                            {
                                if (row.IsMeasured)
                                {
                                    viewMeasureHeight += row.Length;
                                }
                            }
                            viewInfo.View.Measure(MeasureSpec.MakeMeasureSpec((int)width, MeasureSpecMode.AtMost),
                                                  MeasureSpec.MakeMeasureSpec((int)viewMeasureHeight, MeasureSpecMode.AtMost));
                        }
                        maxWidth = Math.Max(maxWidth, viewInfo.View.MeasuredWidth);
                    }
                    columnInfo.SetLength(maxWidth);
                    availableWidth        -= maxWidth;
                    isAvailableSizeChanged = true;
                }
            }

            if (isAvailableSizeChanged && heightMode != MeasureSpecMode.Unspecified)
            {
                foreach (var columnInfo in _columnsInfo.Values)
                {
                    if (columnInfo.Definition.Length.IsStar)
                    {
                        if (availableWidth > 0)
                        {
                            columnInfo.SetLength((columnInfo.Definition.Length.Value / _widthStarSum) * availableWidth);
                        }
                        else
                        {
                            columnInfo.SetLength(0);
                        }
                    }
                }
            }

            isAvailableSizeChanged = false;
            foreach (var rowInfo in _rowsInfo.Values)
            {
                if (rowInfo.Definition.Length.IsAuto)
                {
                    int maxHeight = 0;
                    foreach (var viewInfo in rowInfo.AttachedViews)
                    {
                        if (!viewInfo.IsMeasured)
                        {
                            double measureWidth = 0;

                            foreach (var column in viewInfo.AssociatedColumns)
                            {
                                if (column.IsMeasured)
                                {
                                    measureWidth += column.Length;
                                }
                            }
                            viewInfo.View.Measure(MeasureSpec.MakeMeasureSpec((int)measureWidth, MeasureSpecMode.AtMost),
                                                  MeasureSpec.MakeMeasureSpec(height, MeasureSpecMode.AtMost));
                        }
                        maxHeight = Math.Max(maxHeight, viewInfo.View.MeasuredHeight);
                    }
                    rowInfo.SetLength(maxHeight);
                    availableHeight       -= maxHeight;
                    isAvailableSizeChanged = true;
                }
            }

            if (isAvailableSizeChanged && heightMode != MeasureSpecMode.Unspecified)
            {
                if (availableHeight > 0)
                {
                    foreach (var rowInfo in _rowsInfo.Values)
                    {
                        if (rowInfo.Definition.Length.IsStar)
                        {
                            if (availableHeight > 0)
                            {
                                rowInfo.SetLength((rowInfo.Definition.Length.Value / _heightStarSum) * availableHeight);
                            }
                            else
                            {
                                rowInfo.SetLength(0);
                            }
                        }
                    }
                }
            }

            double changedSize;

            do
            {
                changedSize = 0;
                foreach (var columnInfo in _columnsInfo.Values)
                {
                    if (!columnInfo.Definition.Length.IsAuto)
                    {
                        continue;
                    }
                    double columnShrinkedSize = 0;
                    foreach (var viewInfo in columnInfo.AttachedViews)
                    {
                        double staticSizeForColumn = Math.Max(0, viewInfo.View.MeasuredWidth - viewInfo.StaticWidth);
                        foreach (var viewColumnInfo in viewInfo.AssociatedColumns)
                        {
                            if (staticSizeForColumn == 0)
                            {
                                break;
                            }
                            if (viewColumnInfo.Definition.Length.IsPixel || viewColumnInfo == columnInfo)
                            {
                                continue;
                            }
                            staticSizeForColumn = Math.Max(0, staticSizeForColumn - viewColumnInfo.Length);
                        }
                        columnShrinkedSize = Math.Max(columnShrinkedSize, staticSizeForColumn);
                    }
                    if (columnShrinkedSize < columnInfo.Length)
                    {
                        columnInfo.SetLength(columnShrinkedSize);
                        changedSize += columnInfo.Length - columnShrinkedSize;
                        foreach (var starColumnInfo in _columnsInfo.Values)
                        {
                            if (starColumnInfo.Definition.Length.IsStar)
                            {
                                starColumnInfo.SetLength(starColumnInfo.Length + changedSize * (starColumnInfo.Definition.Length.Value / _widthStarSum));
                            }
                        }
                    }
                }
            } while(changedSize != 0);

            do
            {
                changedSize = 0;
                foreach (var rowInfo in _rowsInfo.Values)
                {
                    if (!rowInfo.Definition.Length.IsAuto)
                    {
                        continue;
                    }
                    double rowShrinkedSize = 0;
                    foreach (var viewInfo in rowInfo.AttachedViews)
                    {
                        double staticSizeForRow = Math.Max(0, viewInfo.View.MeasuredWidth - viewInfo.StaticHeight);
                        foreach (var viewRowInfo in viewInfo.AssociatedRows)
                        {
                            if (staticSizeForRow == 0)
                            {
                                break;
                            }
                            if (viewRowInfo.Definition.Length.IsPixel || viewRowInfo == rowInfo)
                            {
                                continue;
                            }
                            staticSizeForRow = Math.Max(0, staticSizeForRow - viewRowInfo.Length);
                        }
                        rowShrinkedSize = Math.Max(rowShrinkedSize, staticSizeForRow);
                    }
                    if (rowShrinkedSize < rowInfo.Length)
                    {
                        rowInfo.SetLength(rowShrinkedSize);
                        changedSize += rowInfo.Length - rowShrinkedSize;
                        foreach (var starRowInfo in _rowsInfo.Values)
                        {
                            if (starRowInfo.Definition.Length.IsStar)
                            {
                                starRowInfo.SetLength(starRowInfo.Length + changedSize * (starRowInfo.Definition.Length.Value / _heightStarSum));
                            }
                        }
                    }
                }
            } while(changedSize != 0);

            double mWidth  = 0;
            double mHeight = 0;

            foreach (var columnInfo in _columnsInfo.Values)
            {
                mWidth += columnInfo.Length;
            }
            foreach (var rowInfo in _rowsInfo.Values)
            {
                mHeight += rowInfo.Length;
            }
            SetMeasuredDimension((int)mWidth, (int)mHeight);
        }
コード例 #15
0
        /// <summary>
        /// Gets the size of the desired.
        /// </summary>
        /// <returns>The desired size.</returns>
        /// <param name="widthConstraint">Width constraint.</param>
        /// <param name="heightConstraint">Height constraint.</param>
        public override SizeRequest GetDesiredSize(int widthConstraint, int heightConstraint)
        {
            if (_currentControlState.IsNullOrEmpty || Control == null)
            {
                return(new SizeRequest(Xamarin.Forms.Size.Zero));
            }

            _currentControlState.AvailWidth = MeasureSpec.GetSize(widthConstraint);
            if (MeasureSpec.GetMode(widthConstraint) == Android.Views.MeasureSpecMode.Unspecified)
            {
                _currentControlState.AvailWidth = int.MaxValue / 2;
            }
            _currentControlState.AvailHeight = MeasureSpec.GetSize(heightConstraint);
            if (MeasureSpec.GetMode(heightConstraint) == Android.Views.MeasureSpecMode.Unspecified)
            {
                _currentControlState.AvailHeight = int.MaxValue / 2;
            }

            if (_currentControlState.AvailWidth <= 0 || _currentControlState.AvailHeight <= 0)
            {
                return(new SizeRequest(Xamarin.Forms.Size.Zero));
            }

            if (_currentControlState == _lastControlState && _lastSizeRequest.HasValue)
            {
                return(_lastSizeRequest.Value);
            }



            ICharSequence text        = _currentControlState.JavaText;
            var           tmpFontSize = BoundTextSize(Element.FontSize);

            Control.TextSize = tmpFontSize;
            Control.SetSingleLine(false);
            Control.SetMaxLines(int.MaxValue / 2);
            Control.SetIncludeFontPadding(false);
            Control.Ellipsize = null;

            int tmpHt = -1;
            int tmpWd = -1;

            var fontMetrics    = Control.Paint.GetFontMetrics();
            var fontLineHeight = fontMetrics.Descent - fontMetrics.Ascent;
            var fontLeading    = System.Math.Abs(fontMetrics.Bottom - fontMetrics.Descent);

            if (DebugCondition)
            {
                System.Diagnostics.Debug.WriteLine("");
            }

            if (_currentControlState.Lines == 0)
            {
                if (_currentControlState.AvailHeight < int.MaxValue / 3)
                {
                    tmpFontSize = F9PTextView.ZeroLinesFit(_currentControlState.JavaText, new TextPaint(Control.Paint), ModelMinFontSize, tmpFontSize, _currentControlState.AvailWidth, _currentControlState.AvailHeight);
                }
            }
            else
            {
                if (_currentControlState.AutoFit == AutoFit.Lines)
                {
                    if (_currentControlState.AvailHeight > int.MaxValue / 3)
                    {
                        tmpHt = (int)System.Math.Round(_currentControlState.Lines * fontLineHeight + (_currentControlState.Lines - 1) * fontLeading);
                    }
                    else
                    {
                        var fontPointSize   = tmpFontSize;
                        var lineHeightRatio = fontLineHeight / fontPointSize;
                        var leadingRatio    = fontLeading / fontPointSize;
                        tmpFontSize = ((_currentControlState.AvailHeight / (_currentControlState.Lines + leadingRatio * (_currentControlState.Lines - 1))) / lineHeightRatio - 0.1f);
                    }
                }
                else if (_currentControlState.AutoFit == AutoFit.Width)
                {
                    tmpFontSize = F9PTextView.WidthFit(_currentControlState.JavaText, new TextPaint(Control.Paint), _currentControlState.Lines, ModelMinFontSize, tmpFontSize, _currentControlState.AvailWidth, _currentControlState.AvailHeight);
                }
            }

            /*
             * if (_currentControlState.Lines == 0) // && _currentControlState.AutoFit != AutoFit.None)
             *  tmpFontSize = F9PTextView.ZeroLinesFit(_currentControlState.JavaText, new TextPaint(Control.Paint), ModelMinFontSize, tmpFontSize, _currentControlState.AvailWidth, _currentControlState.AvailHeight);
             * else if (_currentControlState.AutoFit == AutoFit.Lines)
             * {
             *
             *  if (_currentControlState.AvailHeight > int.MaxValue / 3)
             *      tmpHt = (int)System.Math.Round(_currentControlState.Lines * fontLineHeight + (_currentControlState.Lines - 1) * fontLeading);
             *  else
             *  {
             *      var fontPointSize = tmpFontSize;
             *      var lineHeightRatio = fontLineHeight / fontPointSize;
             *      var leadingRatio = fontLeading / fontPointSize;
             *      tmpFontSize = ((_currentControlState.AvailHeight / (_currentControlState.Lines + leadingRatio * (_currentControlState.Lines - 1))) / lineHeightRatio - 0.1f);
             *  }
             * }
             * else if (_currentControlState.AutoFit == AutoFit.Width)
             *  tmpFontSize = F9PTextView.WidthFit(_currentControlState.JavaText, new TextPaint(Control.Paint), _currentControlState.Lines, ModelMinFontSize, tmpFontSize, _currentControlState.AvailWidth, _currentControlState.AvailHeight);
             */

            tmpFontSize = BoundTextSize(tmpFontSize);

            // this is the optimal font size.  Let it be known!
            if (tmpFontSize != Element.FittedFontSize)
            {
                //Device.StartTimer(TimeSpan.FromMilliseconds(50), () =>
                //{
                if (Element != null && Control != null)  // multipicker test was getting here with Element and Control both null
                {
                    if (tmpFontSize == Element.FontSize || (Element.FontSize == -1 && tmpFontSize == F9PTextView.DefaultTextSize))
                    {
                        Element.FittedFontSize = -1;
                    }
                    else
                    {
                        Element.FittedFontSize = tmpFontSize;
                    }
                }
                //    return false;
                //});
            }


            var syncFontSize = (float)((ILabel)Element).SynchronizedFontSize;

            if (syncFontSize >= 0 && tmpFontSize != syncFontSize)
            {
                tmpFontSize = syncFontSize;
            }


            Control.TextSize = tmpFontSize;

            var layout = new StaticLayout(_currentControlState.JavaText, new TextPaint(Control.Paint), _currentControlState.AvailWidth, Android.Text.Layout.Alignment.AlignNormal, 1.0f, 0.0f, true);

            int lines = _currentControlState.Lines;

            if (lines == 0 && _currentControlState.AutoFit == AutoFit.None)
            {
                for (int i = 0; i < layout.LineCount; i++)
                {
                    if (layout.GetLineBottom(i) <= _currentControlState.AvailHeight - layout.TopPadding - layout.BottomPadding)
                    {
                        lines++;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (layout.Height > _currentControlState.AvailHeight || (lines > 0 && layout.LineCount > lines))
            {
                if (_currentControlState.Lines == 1)
                {
                    Control.SetSingleLine(true);
                    Control.SetMaxLines(1);
                    Control.Ellipsize = _currentControlState.LineBreakMode.ToEllipsize();
                }
                else
                {
                    layout = F9PTextView.Truncate(_currentControlState.Text, Element.F9PFormattedString, new TextPaint(Control.Paint), _currentControlState.AvailWidth, _currentControlState.AvailHeight, Element.AutoFit, Element.LineBreakMode, ref lines, ref text);
                }
            }
            lines = lines > 0 ? System.Math.Min(lines, layout.LineCount) : layout.LineCount;
            for (int i = 0; i < lines; i++)
            {
                tmpHt = layout.GetLineBottom(i);
                var width = layout.GetLineWidth(i);
                //System.Diagnostics.Debug.WriteLine("\t\tright=["+right+"]");
                if (width > tmpWd)
                {
                    tmpWd = (int)System.Math.Ceiling(width);
                }
            }
            if (_currentControlState.AutoFit == AutoFit.None && _currentControlState.Lines > 0)
            {
                Control.SetMaxLines(_currentControlState.Lines);
            }

            //System.Diagnostics.Debug.WriteLine("\tLabelRenderer.GetDesiredSize\ttmp.size=[" + tmpWd + ", " + tmpHt + "]");
            if (Element.IsDynamicallySized && _currentControlState.Lines > 0 && _currentControlState.AutoFit == AutoFit.Lines)
            {
                fontMetrics    = Control.Paint.GetFontMetrics();
                fontLineHeight = fontMetrics.Descent - fontMetrics.Ascent;
                fontLeading    = System.Math.Abs(fontMetrics.Bottom - fontMetrics.Descent);
                tmpHt          = (int)(fontLineHeight * _currentControlState.Lines + fontLeading * (_currentControlState.Lines - 1));
            }

            Control.Gravity = Element.HorizontalTextAlignment.ToHorizontalGravityFlags() | Element.VerticalTextAlignment.ToVerticalGravityFlags();

            if (Element.Text != null)
            {
                Control.Text = text.ToString();
            }
            else
            {
                Control.TextFormatted = text;
            }

            _lastSizeRequest = new SizeRequest(new Xamarin.Forms.Size(tmpWd, tmpHt), new Xamarin.Forms.Size(10, tmpHt));
            if (showDebugMsg)
            {
                Control.SetWidth((int)_lastSizeRequest.Value.Request.Width);
                Control.SetHeight((int)_lastSizeRequest.Value.Request.Height);

                System.Diagnostics.Debug.WriteLine("\t[" + elementText + "] LabelRenderer.GetDesiredSize(" + (_currentControlState.AvailWidth > int.MaxValue / 3 ? "infinity" : _currentControlState.AvailWidth.ToString()) + "," + (_currentControlState.AvailHeight > int.MaxValue / 3 ? "infinity" : _currentControlState.AvailHeight.ToString()) + ") exit (" + _lastSizeRequest.Value + ")");
                System.Diagnostics.Debug.WriteLine("\t\tControl.Visibility=[" + Control.Visibility + "]");
                System.Diagnostics.Debug.WriteLine("\t\tControl.TextFormatted=[" + Control.TextFormatted + "]");
                System.Diagnostics.Debug.WriteLine("\t\tControl.TextSize=[" + Control.TextSize + "]");
                //System.Diagnostics.Debug.WriteLine("\t\tControl.ClipBounds=["+Control.ClipBounds.Width()+","+Control.ClipBounds.Height()+"]");
                System.Diagnostics.Debug.WriteLine("\t\tControl.Width[" + Control.Width + "]  .Height=[" + Control.Height + "]");
                System.Diagnostics.Debug.WriteLine("\t\tControl.GetX[" + Control.GetX() + "]  .GetY[" + Control.GetY() + "]");
                System.Diagnostics.Debug.WriteLine("\t\tControl.Alpha[" + Control.Alpha + "]");
                System.Diagnostics.Debug.WriteLine("\t\tControl.Background[" + Control.Background + "]");
                //System.Diagnostics.Debug.WriteLine("\t\tControl.Elevation["+Control.Elevation+"]");
                System.Diagnostics.Debug.WriteLine("\t\tControl.Enabled[" + Control.Enabled + "]");
                System.Diagnostics.Debug.WriteLine("\t\tControl.Error[" + Control.Error + "]");
                System.Diagnostics.Debug.WriteLine("\t\tControl.IsOpaque[" + Control.IsOpaque + "]");
                System.Diagnostics.Debug.WriteLine("\t\tControl.IsShown[" + Control.IsShown + "]");
                //Control.BringToFront();
                System.Diagnostics.Debug.WriteLine("\t\t");
            }

            if (Element.LineBreakMode == LineBreakMode.NoWrap)
            {
                Control.SetSingleLine(true);
            }

            _lastControlState = new ControlState(_currentControlState);
            return(_lastSizeRequest.Value);
        }
コード例 #16
0
ファイル: FlowLayout.cs プロジェクト: yuva2achieve/dot42
        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            // need to call base.OnMeasure(...) otherwise Get some funny behaviour
            base.OnMeasure(widthMeasureSpec, heightMeasureSpec);

            int width  = MeasureSpec.GetSize(widthMeasureSpec);
            int height = MeasureSpec.GetSize(heightMeasureSpec);

            // increment the x position as we progress through a line
            int xpos = GetPaddingLeft();
            // increment the y position as we progress through the lines
            int ypos = GetPaddingTop();
            // the height of the current line
            int line_height = 0;

            // go through children
            // to work out the height required for this view

            // call to measure size of children not needed I think?!
            // Getting child's measured height/width seems to work okay without it
            //measureChildren(widthMeasureSpec, heightMeasureSpec);

            View child;
            MarginLayoutParams childMarginLayoutParams;
            int childWidth, childHeight, childMarginLeft, childMarginRight, childMarginTop, childMarginBottom;

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

                if (child.GetVisibility() != GONE)
                {
                    childWidth  = child.GetMeasuredWidth();
                    childHeight = child.GetMeasuredHeight();

                    if (child.GetLayoutParams() != null &&
                        child.GetLayoutParams() is MarginLayoutParams)
                    {
                        childMarginLayoutParams = (MarginLayoutParams)child.GetLayoutParams();

                        childMarginLeft   = childMarginLayoutParams.LeftMargin;
                        childMarginRight  = childMarginLayoutParams.RightMargin;
                        childMarginTop    = childMarginLayoutParams.TopMargin;
                        childMarginBottom = childMarginLayoutParams.BottomMargin;
                    }
                    else
                    {
                        childMarginLeft   = 0;
                        childMarginRight  = 0;
                        childMarginTop    = 0;
                        childMarginBottom = 0;
                    }

                    if (xpos + childMarginLeft + childWidth + childMarginRight + GetPaddingRight() > width)
                    {
                        // this child will need to go on a new line

                        xpos  = GetPaddingLeft();
                        ypos += line_height;

                        line_height = childMarginTop + childHeight + childMarginBottom;
                    }
                    else
                    {
                        // enough space for this child on the current line
                        line_height = Math.Max(
                            line_height,
                            childMarginTop + childHeight + childMarginBottom);
                    }

                    xpos += childMarginLeft + childWidth + childMarginRight;
                }
            }

            ypos += line_height + GetPaddingBottom();

            if (MeasureSpec.GetMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED)
            {
                // set height as measured since there's no height restrictions
                height = ypos;
            }
            else if (MeasureSpec.GetMode(heightMeasureSpec) == MeasureSpec.AT_MOST &&
                     ypos < height)
            {
                // set height as measured since it's less than the maximum allowed
                height = ypos;
            }

            SetMeasuredDimension(width, height);
        }
コード例 #17
0
        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            int resWidth  = 0;
            int resHeight = 0;

            int width     = MeasureSpec.GetSize(widthMeasureSpec);
            int widthMode = (int)MeasureSpec.GetMode(widthMeasureSpec);

            int height     = MeasureSpec.GetSize(heightMeasureSpec);
            int heightMode = (int)MeasureSpec.GetMode(heightMeasureSpec);

            if (widthMode != (int)MeasureSpecMode.Exactly ||
                heightMode != (int)MeasureSpecMode.Exactly)
            {
                resWidth = this.SuggestedMinimumWidth;
                resWidth = resWidth == 0 ? getDefaultWidth() : resWidth;

                resHeight = this.SuggestedMinimumHeight;
                resHeight = resHeight == 0 ? getDefaultWidth() : resHeight;
            }
            else
            {
                resWidth = resHeight = Math.Min(width, height);
            }

            SetMeasuredDimension(resWidth, resHeight);

            var metrics    = Resources.DisplayMetrics;
            var widthInDp  = ConvertPixelsToDp(metrics.WidthPixels);
            var heightInDp = ConvertPixelsToDp(metrics.HeightPixels);

            mRadius = Math.Max(this.MeasuredHeight, this.MeasuredWidth);//Math.Max(widthInDp, heightInDp);

            int count = ChildCount;
            // menu item
            int childSize = (int)(mRadius * RADIO_DEFAULT_CHILD_DIMENSION);
            // menu item
            int childMode = (int)MeasureSpecMode.Exactly;

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

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

                // menu item
                int makeMeasureSpec = -1;


                if (child.Id == Resource.Id.circle_menu_item_center)
                {
                    makeMeasureSpec = MeasureSpec.MakeMeasureSpec(
                        (int)(mRadius * RADIO_DEFAULT_CENTERITEM_DIMENSION),
                        MeasureSpec.GetMode(childMode));
                }
                else
                {
                    makeMeasureSpec = (int)MeasureSpec.MakeMeasureSpec(childSize,
                                                                       MeasureSpec.GetMode(childMode));
                }

                child.Measure(makeMeasureSpec, makeMeasureSpec);
            }

            mPadding = RADIO_PADDING_LAYOUT * mRadius;

            base.OnMeasure(
                MeasureSpec.MakeMeasureSpec(width, MeasureSpecMode.Exactly),
                MeasureSpec.MakeMeasureSpec(height, MeasureSpecMode.Exactly)
                );
        }
コード例 #18
0
        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);
        }