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);
            });
        }
Example #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;
                }
            }

            if (_renderer is IVisualElementRenderer visualElementRenderer)
            {
                visualElementRenderer.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);
                    }
                });
            }
        }
Example #3
0
        void UpdateImage()
        {
            if (_disposed || _renderer == null || _element == null)
            {
                return;
            }

            AppCompatButton view = View;

            if (view == null)
            {
                return;
            }

            FileImageSource elementImage = _element.Image;
            string          imageFile    = elementImage?.File;

            if (elementImage == null || string.IsNullOrEmpty(imageFile))
            {
                view.SetCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
                return;
            }

            using (var image = Context.GetDrawable(imageFile))
            {
                // 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);
                }

                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;
                }
            }
        }