コード例 #1
0
        void UpdateImage()
        {
            if (_disposed || _renderer == null || _element == null)
            {
                return;
            }

            AppCompatButton view = View;

            if (view == null)
            {
                return;
            }

            ImageSource elementImage = _element.ImageSource;

            if (elementImage == null || elementImage.IsEmpty)
            {
                view.SetCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
                return;
            }

            // No text, so no need for relative position; just center the image
            // There's no option for just plain-old centering, so we'll use Top
            // (which handles the horizontal centering) and some tricksy padding (in OnLayout)
            // to handle the vertical centering
            var layout = string.IsNullOrEmpty(_element.Text) ? _imageOnlyLayout : _element.ContentLayout;

            if (_maintainLegacyMeasurements)
            {
                view.CompoundDrawablePadding = (int)layout.Spacing;
            }
            else
            {
                view.CompoundDrawablePadding = (int)Context.ToPixels(layout.Spacing);
            }

            _renderer.ApplyDrawableAsync(Button.ImageSourceProperty, Context, image =>
            {
                switch (layout.Position)
                {
                case Button.ButtonContentLayout.ImagePosition.Top:
                    TextViewCompat.SetCompoundDrawablesRelativeWithIntrinsicBounds(view, null, image, null, null);
                    break;

                case Button.ButtonContentLayout.ImagePosition.Right:
                    TextViewCompat.SetCompoundDrawablesRelativeWithIntrinsicBounds(view, null, null, image, null);
                    break;

                case Button.ButtonContentLayout.ImagePosition.Bottom:
                    TextViewCompat.SetCompoundDrawablesRelativeWithIntrinsicBounds(view, null, null, null, image);
                    break;

                default:
                    // Defaults to image on the left
                    TextViewCompat.SetCompoundDrawablesRelativeWithIntrinsicBounds(view, image, null, null, null);
                    break;
                }

                // Invalidating here causes a crazy amount of increased measure invalidations
                // when I tested with Issue4484 it caused about 800 calls to invalidate measure vs the 8 without this
                // I'm pretty sure it gets into a layout / invalidation loop where these are invalidating mid layout
                //_element?.InvalidateMeasureNonVirtual(InvalidationTrigger.MeasureChanged);
            });
        }
コード例 #2
0
        void UpdateImage()
        {
            if (_disposed || _renderer == null || _element == null)
            {
                return;
            }

            AppCompatButton view = View;

            if (view == null)
            {
                return;
            }

            ImageSource elementImage = _element.ImageSource;

            if (elementImage == null || elementImage.IsEmpty)
            {
                view.SetCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
                return;
            }

            // No text, so no need for relative position; just center the image
            // There's no option for just plain-old centering, so we'll use Top
            // (which handles the horizontal centering) and some tricksy padding (in OnLayout)
            // to handle the vertical centering
            var layout = string.IsNullOrEmpty(_element.Text) ? _imageOnlyLayout : _element.ContentLayout;

            if (_maintainLegacyMeasurements)
            {
                view.CompoundDrawablePadding = (int)layout.Spacing;
            }
            else
            {
                view.CompoundDrawablePadding = (int)Context.ToPixels(layout.Spacing);
            }

            Drawable existingImage = null;
            var      images        = TextViewCompat.GetCompoundDrawablesRelative(view);

            for (int i = 0; i < images.Length; i++)
            {
                if (images[i] != null)
                {
                    existingImage = images[i];
                    break;
                }
            }

            _renderer.ApplyDrawableAsync(Button.ImageSourceProperty, Context, image =>
            {
                if (image == existingImage)
                {
                    return;
                }

                switch (layout.Position)
                {
                case Button.ButtonContentLayout.ImagePosition.Top:
                    TextViewCompat.SetCompoundDrawablesRelativeWithIntrinsicBounds(view, null, image, null, null);
                    break;

                case Button.ButtonContentLayout.ImagePosition.Right:
                    TextViewCompat.SetCompoundDrawablesRelativeWithIntrinsicBounds(view, null, null, image, null);
                    break;

                case Button.ButtonContentLayout.ImagePosition.Bottom:
                    TextViewCompat.SetCompoundDrawablesRelativeWithIntrinsicBounds(view, null, null, null, image);
                    break;

                default:
                    // Defaults to image on the left
                    TextViewCompat.SetCompoundDrawablesRelativeWithIntrinsicBounds(view, image, null, null, null);
                    break;
                }

                if (_hasLayoutOccurred)
                {
                    _element?.InvalidateMeasureNonVirtual(InvalidationTrigger.MeasureChanged);
                }
            });
        }