예제 #1
0
        private async Task SetImageSourceAsync(Android.Widget.Button targetButton, ImageButton model)
        {
            if (targetButton == null || targetButton.Handle == IntPtr.Zero || model == null)
            {
                return;
            }

            var source = model.IsEnabled ? model.Source : model.DisabledSource ?? model.Source;

            using (var bitmap = await GetBitmapAsync(source))
            {
                if (bitmap == null)
                {
                    targetButton.SetCompoundDrawables(null, null, null, null);
                }
                else
                {
                    var drawable = new BitmapDrawable(bitmap);
                    using (var scaledDrawable = GetScaleDrawable(drawable, GetWidth(model.ImageWidthRequest), GetHeight(model.ImageHeightRequest)))
                    {
                        Drawable left    = null;
                        Drawable right   = null;
                        Drawable top     = null;
                        Drawable bottom  = null;
                        int      padding = 5;
                        targetButton.CompoundDrawablePadding = RequestToPixels(padding);
                        switch (model.Orientation)
                        {
                        case ImageOrientation.ImageToLeft:
                            targetButton.Gravity = GravityFlags.Left | GravityFlags.CenterVertical;
                            left = scaledDrawable;
                            break;

                        case ImageOrientation.ImageToRight:
                            targetButton.Gravity = GravityFlags.Right | GravityFlags.CenterVertical;
                            right = scaledDrawable;
                            break;

                        case ImageOrientation.ImageOnTop:
                            targetButton.Gravity = GravityFlags.Top | GravityFlags.CenterHorizontal;
                            top = scaledDrawable;
                            break;

                        case ImageOrientation.ImageOnBottom:
                            targetButton.Gravity = GravityFlags.Bottom | GravityFlags.CenterHorizontal;
                            bottom = scaledDrawable;
                            break;

                        case ImageOrientation.ImageCentered:
                            targetButton.Gravity = GravityFlags.Center;
                            top = scaledDrawable;
                            break;
                        }

                        targetButton.SetCompoundDrawables(left, top, right, bottom);
                        targetButton.SetBackgroundColor(Android.Graphics.Color.Transparent);
                    }
                }
            }
        }
        /// <summary>
        /// Sets the image source.
        /// </summary>
        /// <param name="targetButton">The target button.</param>
        /// <param name="model">The model.</param>
        /// <returns>A <see cref="Task"/> for the awaited operation.</returns>
        private async Task SetImageSourceAsync(Android.Widget.Button targetButton, ImageButton model)
        {
            if (targetButton == null || model == null)
            {
                return;
            }

            const int Padding = 10;
            var       source  = model.IsEnabled ? model.Source : model.DisabledSource ?? model.Source;

            using (var bitmap = await this.GetBitmapAsync(source))
            {
                if (bitmap != null)
                {
                    var drawable  = new BitmapDrawable(bitmap);
                    var tintColor = model.IsEnabled ? model.ImageTintColor : model.DisabledImageTintColor;
                    if (tintColor != Xamarin.Forms.Color.Transparent)
                    {
                        drawable.SetTint(tintColor.ToAndroid());
                        drawable.SetTintMode(PorterDuff.Mode.SrcIn);
                    }

                    using (var scaledDrawable = GetScaleDrawable(drawable, GetWidth(model.ImageWidthRequest),
                                                                 GetHeight(model.ImageHeightRequest))) {
                        Drawable left   = null;
                        Drawable right  = null;
                        Drawable top    = null;
                        Drawable bottom = null;
                        targetButton.CompoundDrawablePadding = Padding;
                        switch (model.Orientation)
                        {
                        case ImageOrientation.ImageToLeft:
                            targetButton.Gravity = GravityFlags.Left | GravityFlags.CenterVertical;
                            left = scaledDrawable;
                            break;

                        case ImageOrientation.ImageToRight:
                            targetButton.Gravity = GravityFlags.Right | GravityFlags.CenterVertical;
                            right = scaledDrawable;
                            break;

                        case ImageOrientation.ImageOnTop:
                            top = scaledDrawable;
                            break;

                        case ImageOrientation.ImageOnBottom:
                            bottom = scaledDrawable;
                            break;
                        }

                        targetButton.SetCompoundDrawables(left, top, right, bottom);
                    }
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Sets the image source.
        /// </summary>
        /// <param name="targetButton">The target button.</param>
        /// <param name="model">The model.</param>
        /// <param name="imageSource">Optional source for disabled image.</param>
        /// <returns>A <see cref="Task"/> for the awaited operation.</returns>
        private async Task SetImageSourceAsync(Android.Widget.Button targetButton, ImageButton model)
        {
            const int Padding = 10;
            var       source  = model.IsEnabled ? model.Source : model.DisabledSource ?? model.Source;


            using (var bitmap = await this.GetBitmapAsync(source))
            {
                if (bitmap != null)
                {
                    Drawable drawable       = new BitmapDrawable(bitmap);
                    var      scaledDrawable = GetScaleDrawable(drawable, GetWidth(model.ImageWidthRequest),
                                                               GetHeight(model.ImageHeightRequest));

                    Drawable left   = null;
                    Drawable right  = null;
                    Drawable top    = null;
                    Drawable bottom = null;
                    targetButton.CompoundDrawablePadding = Padding;
                    switch (model.Orientation)
                    {
                    case ImageOrientation.ImageToLeft:
                        targetButton.Gravity = GravityFlags.Left | GravityFlags.CenterVertical;
                        left = scaledDrawable;
                        break;

                    case ImageOrientation.ImageToRight:
                        targetButton.Gravity = GravityFlags.Right | GravityFlags.CenterVertical;
                        right = scaledDrawable;
                        break;

                    case ImageOrientation.ImageOnTop:
                        top = scaledDrawable;
                        break;

                    case ImageOrientation.ImageOnBottom:
                        bottom = scaledDrawable;
                        break;
                    }

                    targetButton.SetCompoundDrawables(left, top, right, bottom);
                }
            }
        }
예제 #4
0
        private void SetImageSource(Button targetButton, Core.ImageButton model)
        {
            var          packageName  = Context.PackageName;
            const int    Padding      = 10;
            const string ResourceType = "drawable";
            var          resId        = int.Parse(Resource.Drawable.Refresh.ToString());

//			var resId = Resources.GetIdentifier (model.Image, ResourceType, packageName);
            if (resId > 0)
            {
                var scaledDrawable = GetScaleDrawableFromResourceId(resId, GetWidth(model.ImageWidthRequest),
                                                                    GetHeight(model.ImageHeightRequest));


                Drawable left   = null;
                Drawable right  = null;
                Drawable top    = null;
                Drawable bottom = null;
//				targetButton.CompoundDrawablePadding = Padding;
                switch (model.Orientation)
                {
                case ImageOrientation.ImageToLeft:
                    targetButton.Gravity = GravityFlags.Left | GravityFlags.CenterVertical;
                    left = scaledDrawable;
                    break;

                case ImageOrientation.ImageToRight:
                    targetButton.Gravity = GravityFlags.Right | GravityFlags.CenterVertical;
                    right = scaledDrawable;
                    break;

                case ImageOrientation.ImageOnTop:
                    top = scaledDrawable;
                    break;

                case ImageOrientation.ImageOnBottom:
                    bottom = scaledDrawable;
                    break;
                }


                targetButton.SetCompoundDrawables(left, top, right, bottom);
            }
        }
        /// <summary>
        /// Sets the image source.
        /// </summary>
        /// <param name="targetButton">The target button.</param>
        /// <param name="model">The model.</param>
        private void SetImageSource(Android.Widget.Button targetButton, Core.Helpers.Controls.ImageButton model)
        {
            var          packageName  = Context.PackageName;
            const int    padding      = 0;
            const string resourceType = "drawable";

            var resId = Resources.GetIdentifier(model.Image, resourceType, packageName);

            if (resId > 0)
            {
                var scaledDrawable = GetScaleDrawableFromResourceId(resId, GetWidth(model.ImageWidthRequest),
                                                                    GetHeight(model.ImageHeightRequest));

                Drawable left   = null;
                Drawable right  = null;
                Drawable top    = null;
                Drawable bottom = null;
                targetButton.CompoundDrawablePadding = padding;
                switch (model.Orientation)
                {
                case (ImageOrientation.ImageToLeft):
                    targetButton.Gravity = GravityFlags.Left | GravityFlags.CenterVertical;
                    left = scaledDrawable;
                    break;

                case (ImageOrientation.ImageToRight):
                    targetButton.Gravity = GravityFlags.Right | GravityFlags.CenterVertical;
                    right = scaledDrawable;
                    break;

                case (ImageOrientation.ImageOnTop):
                    top = scaledDrawable;
                    break;

                case (ImageOrientation.ImageOnBottom):
                    bottom = scaledDrawable;
                    break;
                }

                targetButton.SetCompoundDrawables(left, top, right, bottom);
            }
        }
        /// <summary>
        /// Sets the image source.
        /// </summary>
        /// <param name="targetButton">The target button.</param>
        /// <param name="model">The model.</param>
        private void SetImageSource(Android.Widget.Button targetButton, Toolkit.Controls.ImageButton model)
        {
            var packageName = Context.PackageName;

            var resId = Resources.GetIdentifier(model.Image, "drawable", packageName);

            if (resId > 0)
            {
                var scaledDrawable = GetScaleDrawableFromResourceId(resId, GetWidth(model.ImageWidthRequest),
                                                                    GetHeight(model.ImageHeightRequest));

                Drawable left   = null;
                Drawable right  = null;
                Drawable top    = null;
                Drawable bottom = null;
                switch (model.Orientation)
                {
                case (ImageOrientation.ImageToLeft):
                    left = scaledDrawable;
                    break;

                case (ImageOrientation.ImageToRight):
                    right = scaledDrawable;
                    break;

                case (ImageOrientation.ImageOnTop):
                    top = scaledDrawable;
                    break;

                case (ImageOrientation.ImageOnBottom):
                    bottom = scaledDrawable;
                    break;
                }

                targetButton.SetCompoundDrawables(left, top, right, bottom);
            }
        }
        /// <summary>
        /// Sets the image source.
        /// </summary>
        /// <param name="targetButton">The target button.</param>
        /// <param name="model">The model.</param>
        /// <returns>A <see cref="Task"/> for the awaited operation.</returns>
        private async Task SetImageSourceAsync(Android.Widget.Button targetButton, ImageButton model)
        {
            if (targetButton == null || targetButton.Handle == IntPtr.Zero || model == null)
            {
                return;
            }

            // const int Padding = 10;
            var source = model.IsEnabled ? model.Source : model.DisabledSource ?? model.Source;

            using (var bitmap = await GetBitmapAsync(source))
            {
                if (bitmap == null)
                {
                    targetButton.SetCompoundDrawables(null, null, null, null);
                }
                else
                {
                    var drawable  = new BitmapDrawable(bitmap);
                    var tintColor = model.IsEnabled ? model.ImageTintColor : model.DisabledImageTintColor;
                    if (tintColor != Color.Transparent)
                    {
                        drawable.SetTint(tintColor.ToAndroid());
                        drawable.SetTintMode(PorterDuff.Mode.SrcIn);
                    }

                    using (var scaledDrawable = GetScaleDrawable(drawable, model.ImageWidthRequest, model.ImageHeightRequest))
                    {
                        Drawable left   = null;
                        Drawable right  = null;
                        Drawable top    = null;
                        Drawable bottom = null;
                        //System.Diagnostics.Debug.WriteLine($"SetImageSourceAsync intptr{targetButton.Handle}");
                        int padding = 10; // model.Padding
                        targetButton.CompoundDrawablePadding = RequestToPixels(padding);
                        switch (model.Orientation)
                        {
                        case ImageOrientation.ImageToLeft:
                            targetButton.Gravity = GravityFlags.Left | GravityFlags.CenterVertical;
                            left = scaledDrawable;
                            break;

                        case ImageOrientation.ImageToRight:
                            targetButton.Gravity = GravityFlags.Right | GravityFlags.CenterVertical;
                            right = scaledDrawable;
                            break;

                        case ImageOrientation.ImageOnTop:
                            targetButton.Gravity = GravityFlags.Top | GravityFlags.CenterHorizontal;
                            top = scaledDrawable;
                            break;

                        case ImageOrientation.ImageOnBottom:
                            targetButton.Gravity = GravityFlags.Bottom | GravityFlags.CenterHorizontal;
                            bottom = scaledDrawable;
                            break;
                            //case ImageOrientation.ImageCentered:
                            //    targetButton.Gravity = GravityFlags.Center; // | GravityFlags.Fill;
                            //    top = scaledDrawable;
                            //    break;
                        }

                        targetButton.SetCompoundDrawables(left, top, right, bottom);
                    }
                }
            }
        }
예제 #8
0
        /// <summary>
        /// Sets the image source.
        /// </summary>
        /// <param name="targetButton">The target button.</param>
        /// <param name="model">The model.</param>
        /// <returns>A <see cref="Task"/> for the awaited operation.</returns>
        private void SetImageSource(Android.Widget.Button targetButton, ImageButton model)
        {
            if (targetButton == null || targetButton.Handle == IntPtr.Zero || model == null)
            {
                return;
            }

            var source   = model.IsEnabled ? model.Source : model.DisabledSource ?? model.Source;
            var drawable = Context.GetDrawable((source as FileImageSource).File);//new BitmapDrawable(bitmap);

            if (drawable == null)
            {
                targetButton.SetCompoundDrawables(null, null, null, null);
            }
            else
            {
                var tintColor = model.IsEnabled ? model.ImageTintColor : model.DisabledImageTintColor;
                if (tintColor != Color.Transparent)
                {
                    drawable.SetTint(tintColor.ToAndroid());
                    drawable.SetTintMode(PorterDuff.Mode.SrcIn);
                }

                using (var scaledDrawable = GetScaleDrawable(drawable, model.ImageWidthRequest, model.ImageHeightRequest)) {
                    Drawable left   = null;
                    Drawable right  = null;
                    Drawable top    = null;
                    Drawable bottom = null;

                    switch (model.Orientation)
                    {
                    case ImageOrientation.ImageToLeft:
                        targetButton.Gravity = GravityFlags.Left | GravityFlags.CenterVertical;
                        left = scaledDrawable;
                        break;

                    case ImageOrientation.ImageToRight:
                        targetButton.Gravity = GravityFlags.Right | GravityFlags.CenterVertical;
                        right = scaledDrawable;
                        break;

                    case ImageOrientation.ImageOnTop:
                        targetButton.Gravity = GravityFlags.Top | GravityFlags.CenterHorizontal;
                        top = scaledDrawable;
                        break;

                    case ImageOrientation.ImageOnBottom:
                        targetButton.Gravity = GravityFlags.Bottom | GravityFlags.CenterHorizontal;
                        bottom = scaledDrawable;
                        break;

                    case ImageOrientation.ImageCenterToLeft:
                        targetButton.Gravity = GravityFlags.CenterHorizontal | GravityFlags.CenterVertical;
                        left = scaledDrawable;
                        break;

                    case ImageOrientation.ImageCenterToRight:
                        targetButton.Gravity = GravityFlags.CenterHorizontal | GravityFlags.CenterVertical;
                        right = scaledDrawable;
                        break;
                    }

                    var width  = scaledDrawable.Bounds.Width();
                    var height = scaledDrawable.Bounds.Height();

                    targetButton.CompoundDrawablePadding = (int)Context.ToPixels(10);
                    targetButton.SetMinimumWidth(width + Control.PaddingLeft + Control.PaddingRight);
                    targetButton.SetMinimumHeight(height + Control.PaddingTop + Control.PaddingBottom);
                    targetButton.SetCompoundDrawables(left, top, right, bottom);
                }
            }
        }
예제 #9
0
        private void SetImageSource(Button targetButton, Core.ImageButton model)
        {
            var packageName = Context.PackageName;
            const int Padding = 10;
            const string ResourceType = "drawable";
            var resId = int.Parse (Resource.Drawable.Refresh.ToString ());

            //			var resId = Resources.GetIdentifier (model.Image, ResourceType, packageName);
            if (resId > 0) {
                var scaledDrawable = GetScaleDrawableFromResourceId (resId, GetWidth (model.ImageWidthRequest),
                                         GetHeight (model.ImageHeightRequest));

                Drawable left = null;
                Drawable right = null;
                Drawable top = null;
                Drawable bottom = null;
            //				targetButton.CompoundDrawablePadding = Padding;
                switch (model.Orientation) {
                case ImageOrientation.ImageToLeft:
                    targetButton.Gravity = GravityFlags.Left | GravityFlags.CenterVertical;
                    left = scaledDrawable;
                    break;
                case ImageOrientation.ImageToRight:
                    targetButton.Gravity = GravityFlags.Right | GravityFlags.CenterVertical;
                    right = scaledDrawable;
                    break;
                case ImageOrientation.ImageOnTop:
                    top = scaledDrawable;
                    break;
                case ImageOrientation.ImageOnBottom:
                    bottom = scaledDrawable;
                    break;
                }

                targetButton.SetCompoundDrawables (left, top, right, bottom);
            }
        }