Пример #1
0
        void ApplyMaxTrackTintColor(string propertyName, bool forceApply = false)
        {
            // We always want to force the application of the MaxTrackTintColor if we're running an API that is older
            // than Marshmallow and we're also using the AppCompat library so the track color will be appropriately
            // clamped.
            forceApply = forceApply ||
                         (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.M &&
                          XFGloss.Droid.Library.UsingAppCompat);

            if (propertyName == null ||
                propertyName == SliderGloss.MaxTrackTintColorProperty.PropertyName ||
                forceApply)
            {
                var maxTrackTintColor = (Color)Element.GetValue(SliderGloss.MaxTrackTintColorProperty);
                // Skip assigning anything if all properties are being applied and the color is set to the default value
                if (propertyName != null || maxTrackTintColor != Color.Default || forceApply)
                {
                    if (maxTrackTintColor != Color.Default)
                    {
                        maxTrackTintColor = new Color(maxTrackTintColor.R, maxTrackTintColor.G, maxTrackTintColor.B, 0.3);
                    }

                    var aMaxTrackTintColor =
                        (maxTrackTintColor == Color.Default) ?
                        ThemeUtil.IntToColor(ThemeUtil.ColorControlNormal(Control.Context,
                                                                          ThemeUtil.DefaultColorControlTrack)) :
                        maxTrackTintColor.ToAndroid();

                    if (!XFGloss.Droid.Library.UsingAppCompat || XFGloss.Droid.Library.UsingAppCompatAlpha)
                    {
                        // Clamp the track tint colors to 30% opacity - API 24 automatically does this. AppCompat doesn't.
                        aMaxTrackTintColor = new AColor(aMaxTrackTintColor.R,
                                                        aMaxTrackTintColor.G,
                                                        aMaxTrackTintColor.B,
                                                        (byte)77);
                    }

                    if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop)
                    {
                        // FYI: Lollipop (API 21) has the ProgressBackgroundTintList implemented but it is broken.
                        // Assigning a value to the property has no effect on API 21. It works as expected on API 22+.
                        Control.ProgressBackgroundTintList = ColorStateList.ValueOf(aMaxTrackTintColor);
                    }
                    else if (XFGloss.Droid.Library.UsingAppCompat)
                    {
                        ThemeUtil.SetLayerTint(Control.ProgressDrawable as LayerDrawable, 0, aMaxTrackTintColor);
                    }
                    else
                    {
                        Console.WriteLine(XFGloss.Droid.Library.appCompatWarning);
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Internal method used to do the work on behalf of the UpdateColorProperty extension method for both
        /// XFGlossSwitchExtensions and XFGlossSwitchCompatExtensions
        /// </summary>
        /// <param name="control">Control.</param>
        /// <param name="properties">Properties.</param>
        /// <param name="propertyName">Property name.</param>
        /// <typeparam name="TControl">The 1st type parameter.</typeparam>
        static void ApplyColorProperty <TControl>(TControl control, ISwitchGloss properties, string propertyName)
        {
            // We have to create a multiple state color list to set both the "off" and "on" (checked/unchecked)
            // states of the switch control.

            bool isSwitch = Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.M &&
                            control is ASwitch;

            bool isSwitchCompat = !isSwitch &&
                                  XFGloss.Droid.Library.UsingAppCompat &&
                                  control is ASwitchCompat;

            Android.Content.Context controlContext = (isSwitch) ? (control as ASwitch).Context :
                                                     (isSwitchCompat) ? (control as ASwitchCompat).Context :
                                                     null;

            int[][] states = new int[2][];
            int[]   colors = new int[2];

            if (propertyName == null ||
                propertyName == SwitchGloss.TintColorProperty.PropertyName ||
                propertyName == SwitchGloss.OnTintColorProperty.PropertyName)
            {
                var tintColor   = properties.TintColor;
                var onTintColor = properties.OnTintColor;

                // Skip assigning anything if all properties are being applied and the color is set to the default value
                if (isSwitchCompat || propertyName != null || tintColor != Color.Default || onTintColor != Color.Default)
                {
                    var aTintColor = (tintColor != Color.Default) ?
                                     tintColor.ToAndroid() :
                                     new AColor(ThemeUtil.ColorControlNormal(controlContext,
                                                                             ThemeUtil.DefaultColorControlTrack));

                    var aOnTintColor = (onTintColor != Color.Default) ?
                                       onTintColor.ToAndroid() :
                                       new AColor(ThemeUtil.ColorControlActivated(controlContext,
                                                                                  ThemeUtil.DefaultColorControlTrackActivated));

                    // Clamp the track tint colors to 30% opacity - API 24 automatically does this. AppCompat doesn't.
                    if (isSwitchCompat && XFGloss.Droid.Library.UsingAppCompatAlpha)
                    {
                        aTintColor   = new AColor(aTintColor.R, aTintColor.G, aTintColor.B, (byte)77);
                        aOnTintColor = new AColor(aOnTintColor.R, aOnTintColor.G, aOnTintColor.B, (byte)77);
                    }

                    states[0] = new int[] { -Android.Resource.Attribute.StateChecked };
                    colors[0] = aTintColor;

                    states[1] = new int[] { Android.Resource.Attribute.StateChecked };
                    colors[1] = aOnTintColor;

                    var colorList = new ColorStateList(states, colors);

                    if (isSwitch)
                    {
                        (control as ASwitch).TrackTintList = colorList;
                    }
                    else if (isSwitchCompat)
                    {
                        DrawableCompat.SetTintList((control as ASwitchCompat).TrackDrawable, colorList);
                    }
                    else
                    {
                        Console.WriteLine(XFGloss.Droid.Library.appCompatWarning);
                    }
                }
            }

            if (propertyName == null ||
                propertyName == SwitchGloss.ThumbTintColorProperty.PropertyName ||
                propertyName == SwitchGloss.ThumbOnTintColorProperty.PropertyName)
            {
                var thumbTintColor   = properties.ThumbTintColor;
                var thumbOnTintColor = properties.ThumbOnTintColor;

                // Skip assigning anything if all properties are being applied and the color is set to the default value
                if (propertyName != null || thumbTintColor != Color.Default || thumbOnTintColor != Color.Default)
                {
                    states[0] = new int[] { -Android.Resource.Attribute.StateChecked };
                    colors[0] = (thumbTintColor != Color.Default) ?
                                thumbTintColor.ToAndroid() :
                                ThemeUtil.DefaultColorControlThumb;

                    states[1] = new int[] { Android.Resource.Attribute.StateChecked };
                    colors[1] = (thumbOnTintColor != Color.Default) ?
                                thumbOnTintColor.ToAndroid() :
                                new AColor(ThemeUtil.ColorControlActivated(controlContext,
                                                                           ThemeUtil.DefaultColorControlThumbActivated));

                    var colorList = new ColorStateList(states, colors);

                    if (isSwitch)
                    {
                        (control as ASwitch).ThumbTintList = colorList;
                    }
                    else if (isSwitchCompat)
                    {
                        DrawableCompat.SetTintList((control as ASwitchCompat).ThumbDrawable, colorList);
                    }
                    else
                    {
                        Console.WriteLine(XFGloss.Droid.Library.appCompatWarning);
                    }
                }
            }
        }