-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageButtonTintEffect.cs
73 lines (66 loc) · 2.59 KB
/
ImageButtonTintEffect.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Content.Res;
using Android.Graphics;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using static Barber.Controls.ImageButtonTintEffect;
using AWImageButton = Android.Widget.ImageButton;
[assembly: ResolutionGroupName("XfEffects")]
[assembly: ExportEffect(typeof(Barber.Droid.ImageButtonTintEffect), nameof(Barber.Controls.ImageButtonTintEffect))]
namespace Barber.Droid
{
public class ImageButtonTintEffect : PlatformEffect
{
private static readonly int[][] _colorStates =
{
new[] { global::Android.Resource.Attribute.StateEnabled },
new[] { -global::Android.Resource.Attribute.StateEnabled }, //disabled state
new[] { global::Android.Resource.Attribute.StatePressed } //pressed state
};
protected override void OnAttached()
{
UpdateTintColor();
}
protected override void OnDetached()
{
}
protected override void OnElementPropertyChanged(PropertyChangedEventArgs args)
{
if (args.PropertyName == ImageButtonTintEffectParameters.TintColorProperty.PropertyName)
UpdateTintColor();
if (args.PropertyName == Xamarin.Forms.ImageButton.SourceProperty.PropertyName)
UpdateTintColor();
}
private void UpdateTintColor()
{
try
{
if (this.Control is AWImageButton imageButton)
{
var androidColor = ImageButtonTintEffectParameters.GetTintColor(this.Element).ToAndroid();
var disabledColor = androidColor;
disabledColor.A = 0x1C; //140
var pressedColor = androidColor;
pressedColor.A = 0x1C; //140
imageButton.ImageTintList = new ColorStateList(_colorStates, new[] { pressedColor.ToArgb(), pressedColor.ToArgb(), pressedColor.ToArgb() });
imageButton.ImageTintMode = PorterDuff.Mode.SrcOver;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(
$"An error occurred when setting the {typeof(Barber.Controls.ImageButtonTintEffect)} effect: {ex.Message}\n{ex.StackTrace}");
}
}
}
}