Created
April 26, 2023 12:38
-
-
Save DarkIrata/3918afe7af8ad4731a2747863ac3c15a to your computer and use it in GitHub Desktop.
Android Rippleeffect and Clickable StackLayout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code | |
// Example for ImageButton Background Rippleeffect | |
Microsoft.Maui.Handlers.ImageButtonHandler.Mapper.AppendToMapping(nameof(IView.Background), (handler, _) => | |
{ // Todo: Needs Update after Theme Change so it uses the different Color | |
#if ANDROID | |
var contextResourceColor = AndroidX.Core.Content.ContextCompat.GetColor(handler.MauiContext.Context, Resource.Color.ripple_material_light); | |
var rippleColor = new Android.Graphics.Color(contextResourceColor); | |
var rippleDrawable = RippleDrawableHelper.GetRippleDrawableByStates(rippleColor); | |
handler.PlatformView.SetBackground(rippleDrawable); | |
#endif | |
}); | |
// Code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Android.Content; | |
using Android.Content.Res; | |
using Android.Graphics.Drawables; | |
using Microsoft.Maui.Platform; | |
using AndroidGraphics = Android.Graphics; | |
// Add in Maui Project to Platforms/Android | |
using AndroidResource = Android.Resource; | |
using AndroidUtil = Android.Util; | |
namespace IPUP.Maui.Android.Effects | |
{ | |
public static class RippleDrawableHelper | |
{ | |
private static int[][] GetStates(int[] rawStates) | |
{ | |
if (rawStates == null || rawStates.Length == 0) | |
{ | |
throw new ArgumentException("Possible States cant be empty"); | |
} | |
return rawStates.Select(s => new int[] { s }).ToArray(); | |
} | |
private static int[] GetColors(AndroidGraphics.Color[] rawColors) | |
{ | |
if (rawColors == null || rawColors.Length == 0) | |
{ | |
throw new ArgumentException("Colors cant be empty"); | |
} | |
return rawColors.Select(c => (int)c).ToArray(); | |
} | |
public static RippleDrawable GetRippleDrawableByStates(AndroidGraphics.Color pressedColor) | |
=> GetRippleDrawableByStates(new Dictionary<int, AndroidGraphics.Color> { | |
{ AndroidResource.Attribute.StatePressed, pressedColor }, | |
{ AndroidResource.Attribute.StateFocused, Color.FromArgb("#00000000").ToPlatform() }, | |
}); | |
public static RippleDrawable GetRippleDrawableByStates(Dictionary<int, AndroidGraphics.Color> colorToStateMapping) | |
{ | |
var states = GetStates(colorToStateMapping.Keys.ToArray()); | |
var colors = GetColors(colorToStateMapping.Values.ToArray()); | |
var colorStateList = new ColorStateList(states, colors); | |
return new RippleDrawable(colorStateList, null, null); | |
} | |
public static Drawable? GetRippleDrawableFromAttribute(Context context, int attribute = AndroidResource.Attribute.SelectableItemBackground) | |
{ | |
AndroidGraphics.Drawables.Drawable? drawable = null; | |
if (context != null) | |
{ | |
using (var typedValue = new AndroidUtil.TypedValue()) | |
{ | |
context.Theme!.ResolveAttribute(attribute, typedValue, true); | |
var resourceId = typedValue.ResourceId; | |
drawable = context.Theme!.GetDrawable(resourceId); | |
} | |
} | |
return drawable; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment