/// <summary> /// Sets the image source. /// </summary> /// <param name="targetButton">The target button.</param> /// <param name="model">The model.</param> private void SetImageSource(global::Android.Widget.Button targetButton, ImageButton model) { var packageName = Context.PackageName; const int padding = 10; const string resourceType = "drawable"; var resId = Resources.GetIdentifier(model.ImagePath, 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> /// <returns>A <see cref="Task"/> for the awaited operation.</returns> private async Task SetImageSourceAsync(global::Android.Widget.Button targetButton, CustomImageButton 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).ConfigureAwait(false)) { 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 != 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; //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); } } } }