protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                var button = (CustomButton)e.NewElement;

                button.SizeChanged += (s, args) =>
                {
                    // Estado normal
                    _normal = new GradientDrawable();
                    _normal.SetColor(Android.Graphics.Color.DarkBlue);
                    _normal.SetCornerRadius(100);

                    // Estado pulsado
                    _pressed = new GradientDrawable();
                    _pressed.SetColor(Android.Graphics.Color.DarkCyan);
                    _pressed.SetCornerRadius(100);

                    var sld = new StateListDrawable();
                    sld.AddState(new int[] { Android.Resource.Attribute.StatePressed }, _pressed);
                    sld.AddState(new int[] { }, _normal);
                    Control.SetBackgroundDrawable(sld);
                    Control.SetTextSize(ComplexUnitType.Px, 60);
                    Control.SetTextColor(Android.Graphics.Color.Silver);
                };
            }
        }
		async Task BuildBackground()
		{
			using (var statesBackground = new StateListDrawable ()) {
				if (BaseElement.NormalImage != null) {
					var normalHandler = BaseElement.NormalImage.GetHandler ();
					using (var imgNormal = await normalHandler.LoadImageAsync (BaseElement.NormalImage, base.Context)) {
						statesBackground.AddState (
							new int[]{
								-global::Android.Resource.Attribute.StatePressed,
								global::Android.Resource.Attribute.StateEnabled
							},
							new BitmapDrawable(imgNormal)
						);
						if (BaseElement.PressedImage == null) {
							statesBackground.AddState (
								new int[] {
									global::Android.Resource.Attribute.StatePressed,
									global::Android.Resource.Attribute.StateEnabled
								},
								new BitmapDrawable(imgNormal)
							);
						}
						if (BaseElement.DisableImage == null) {
							statesBackground.AddState (
								new int[] {
									-global::Android.Resource.Attribute.StateEnabled
								},
								new BitmapDrawable(imgNormal)
							);
						}
					}
				}

				if (BaseElement.PressedImage != null) {
					var pressedHandler = BaseElement.PressedImage.GetHandler ();
					using (var imgPressed = await pressedHandler.LoadImageAsync (BaseElement.PressedImage, base.Context)) {
						statesBackground.AddState (
							new int[] {
								global::Android.Resource.Attribute.StatePressed,
								global::Android.Resource.Attribute.StateEnabled
							},
							new BitmapDrawable(imgPressed)
						);
					}
				}
				if (BaseElement.DisableImage != null) {
					var disableHandler = BaseElement.DisableImage.GetHandler ();
					using (var imgDisable = await disableHandler.LoadImageAsync (BaseElement.DisableImage, base.Context)) {
						statesBackground.AddState (
							new int[] {
								-global::Android.Resource.Attribute.StateEnabled
							},
							new BitmapDrawable(imgDisable)
						);
					}
				}
				if (Control != null)
					Control.Background = statesBackground;
			}
		}
        private static void SetColors(ExtendedSwitch entry, Android.Widget.Switch control)
        {
            var track = (StateListDrawable)control.TrackDrawable;

            track.SetColorFilter(entry.TrackColor.ToAndroid(), PorterDuff.Mode.Multiply);

            //lollipop
            if (control.ThumbDrawable is AnimatedStateListDrawable)
            {
                var thumb = (AnimatedStateListDrawable)control.ThumbDrawable;
            }
            else
            {
                var thumb = new StateListDrawable();
                thumb.AddState(
                    new[] { Android.Resource.Attribute.StateChecked },
                    new ColorDrawable(entry.OnColor.ToAndroid()));
                thumb.AddState(
                    new[] { -Android.Resource.Attribute.StateEnabled },
                    new ColorDrawable(entry.DisabledColor.ToAndroid()));
                thumb.AddState(
                    new int[0],
                    new ColorDrawable(entry.OffColor.ToAndroid()));

                control.ThumbDrawable = thumb;
            }
        }
		protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
		{
			base.OnElementChanged(e);

			if (Control != null)
			{
				var button = e.NewElement;

				// Create a drawable for the button's normal state
				_normal = new Android.Graphics.Drawables.GradientDrawable();

				if (button.BackgroundColor.R == -1.0 && button.BackgroundColor.G == -1.0 && button.BackgroundColor.B == -1.0)
					_normal.SetColor(Android.Graphics.Color.ParseColor("#ff2c2e2f"));
				else
					_normal.SetColor(button.BackgroundColor.ToAndroid());

				_normal.SetStroke((int)button.BorderWidth, button.BorderColor.ToAndroid());
				_normal.SetCornerRadius(button.BorderRadius);

				// Create a drawable for the button's pressed state
				_pressed = new Android.Graphics.Drawables.GradientDrawable();
				var highlight = Context.ObtainStyledAttributes(new int[] { Android.Resource.Attribute.ColorActivatedHighlight }).GetColor(0, Android.Graphics.Color.Gray);
				_pressed.SetColor(highlight);
				_pressed.SetStroke((int)button.BorderWidth, button.BorderColor.ToAndroid());
				_pressed.SetCornerRadius(button.BorderRadius);

				// Add the drawables to a state list and assign the state list to the button
				var sld = new StateListDrawable();
				sld.AddState(new int[] { Android.Resource.Attribute.StatePressed }, _pressed);
				sld.AddState(new int[] { }, _normal);
				Control.SetBackgroundDrawable(sld);
			}
		}
Ejemplo n.º 5
0
		private void init(Context context, IAttributeSet attrs) {
			mNormalDrawable = new StateListDrawable();
			if (attrs != null) {
				initAttributes(context, attrs);
			}
			mNormalText = Text.ToString ();
			setBackgroundCompat(mNormalDrawable);
		}
        protected override async void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
        {
            base.OnElementChanged(e);
            UpdateAlignment();
            UpdateFont();
            UpdatePadding();
            //UpdateMargin();

            _density = Resources.DisplayMetrics.Density;
            var targetButton = Control;
            if (targetButton != null) targetButton.SetOnTouchListener(TouchListener.Instance.Value);

            if (Element != null && Element.Font != Font.Default && targetButton != null) targetButton.Typeface = Element.Font.ToExtendedTypeface(Context);

            if (Element != null && BaseButton.Source != null) await SetImageSourceAsync(targetButton, BaseButton).ConfigureAwait(false);


            if (Control != null)
            {
                var button = e.NewElement;


                // Create a drawable for the button's normal state
                _normal = new Android.Graphics.Drawables.GradientDrawable();

                if (button.BackgroundColor.R == -1.0 && button.BackgroundColor.G == -1.0 && button.BackgroundColor.B == -1.0)
                    _normal.SetColor(Android.Graphics.Color.ParseColor(buttonWhite));
                else
                    _normal.SetColor(button.BackgroundColor.ToAndroid());

                _normal.SetStroke((int)button.BorderWidth, button.BorderColor.ToAndroid());
                _normal.SetCornerRadius(button.BorderRadius);


                // Create a drawable for the button's pressed state
                _pressed = new Android.Graphics.Drawables.GradientDrawable();
                var highlight = Android.Graphics.Color.ParseColor(buttonGrey);
                //var highlight = Context.ObtainStyledAttributes(new int[] { Android.Resource.Attribute.ColorActivatedHighlight }).GetColor(0, Android.Graphics.Color.ParseColor(buttonWhite));
                _pressed.SetColor(highlight);
                _pressed.SetStroke((int)button.BorderWidth, button.BorderColor.ToAndroid());
                _pressed.SetCornerRadius(button.BorderRadius);

                // Add the drawables to a state list and assign the state list to the button
                var sld = new StateListDrawable();
                sld.AddState(new int[] { Android.Resource.Attribute.StatePressed }, _pressed);
                sld.AddState(new int[] { }, _normal);
                Control.SetBackgroundDrawable(sld);
            }
        }
		public static void SetTheme(CheckBox checkBox, FlatTheme theme, 
			FlatUI.FlatFontFamily fontFamily, FlatUI.FlatFontWeight fontWeight, int radius, int size, int border)
		{
			// creating unchecked-enabled state drawable
			GradientDrawable uncheckedEnabled = new GradientDrawable();
			uncheckedEnabled.SetCornerRadius(radius);
			uncheckedEnabled.SetSize(size, size);
			uncheckedEnabled.SetColor(Color.Transparent);
			uncheckedEnabled.SetStroke(border, theme.LightAccentColor);

			// creating checked-enabled state drawable
			GradientDrawable checkedOutside = new GradientDrawable();
			checkedOutside.SetCornerRadius(radius);
			checkedOutside.SetSize(size, size);
			checkedOutside.SetColor(Color.Transparent);
			checkedOutside.SetStroke(border, theme.LightAccentColor);

			PaintDrawable checkedCore = new PaintDrawable(theme.LightAccentColor);
			checkedCore.SetCornerRadius(radius);
			checkedCore.SetIntrinsicHeight(size);
			checkedCore.SetIntrinsicWidth(size);
			InsetDrawable checkedInside = new InsetDrawable(checkedCore, border + 2, border + 2, border + 2, border + 2);

			Drawable[] checkedEnabledDrawable = {checkedOutside, checkedInside};
			LayerDrawable checkedEnabled = new LayerDrawable(checkedEnabledDrawable);

			// creating unchecked-enabled state drawable
			GradientDrawable uncheckedDisabled = new GradientDrawable();
			uncheckedDisabled.SetCornerRadius(radius);
			uncheckedDisabled.SetSize(size, size);
			uncheckedDisabled.SetColor(Color.Transparent);
			uncheckedDisabled.SetStroke(border, theme.VeryLightAccentColor);

			// creating checked-disabled state drawable
			GradientDrawable checkedOutsideDisabled = new GradientDrawable();
			checkedOutsideDisabled.SetCornerRadius(radius);
			checkedOutsideDisabled.SetSize(size, size);
			checkedOutsideDisabled.SetColor(Color.Transparent);
			checkedOutsideDisabled.SetStroke(border, theme.VeryLightAccentColor);

			PaintDrawable checkedCoreDisabled = new PaintDrawable(theme.VeryLightAccentColor);
			checkedCoreDisabled.SetCornerRadius(radius);
			checkedCoreDisabled.SetIntrinsicHeight(size);
			checkedCoreDisabled.SetIntrinsicWidth(size);
			InsetDrawable checkedInsideDisabled = new InsetDrawable(checkedCoreDisabled, border + 2, border + 2, border + 2, border + 2);

			Drawable[] checkedDisabledDrawable = {checkedOutsideDisabled, checkedInsideDisabled};
			LayerDrawable checkedDisabled = new LayerDrawable(checkedDisabledDrawable);


			StateListDrawable states = new StateListDrawable();
			states.AddState(new int[]{-Android.Resource.Attribute.StateChecked, Android.Resource.Attribute.StateEnabled}, uncheckedEnabled);
			states.AddState(new int[]{Android.Resource.Attribute.StateChecked, Android.Resource.Attribute.StateEnabled}, checkedEnabled);
			states.AddState(new int[]{-Android.Resource.Attribute.StateChecked, -Android.Resource.Attribute.StateEnabled}, uncheckedDisabled);
			states.AddState(new int[]{Android.Resource.Attribute.StateChecked, -Android.Resource.Attribute.StateEnabled}, checkedDisabled);
			checkBox.SetButtonDrawable(states);

			// setting padding for avoiding text to be appear on icon
			checkBox.SetPadding(size / 4 * 5, 0, 0, 0);
			checkBox.SetTextColor(theme.LightAccentColor);

			var typeface = FlatUI.GetFont(checkBox.Context, fontFamily, fontWeight);
			if (typeface != null) 
				checkBox.SetTypeface(typeface, TypefaceStyle.Normal);
		}
Ejemplo n.º 8
0
 private Drawable CreateSelector(Color color)
 {
     ShapeDrawable darkerCircle = new ShapeDrawable(new OvalShape());
     darkerCircle.Paint.Color = TranslucentColor(ShiftColorUp(color));
     StateListDrawable stateListDrawable = new StateListDrawable();
     stateListDrawable.AddState(new int[] { Android.Resource.Attribute.StatePressed }, darkerCircle);
     return stateListDrawable;
 }
        private void UpdateColors()
        {
            var button = Element as ShadowButton;

            var normal = new Android.Graphics.Drawables.GradientDrawable ();
            normal.SetColor (button.BackgroundColor.ToAndroid ());
            normal.SetStroke ((int)button.BorderWidth, button.BorderColor.ToAndroid ());
            normal.SetCornerRadius (button.BorderRadius);
            var shadow = new Android.Graphics.Drawables.GradientDrawable ();
            shadow.SetColor (button.ShadowColor.ToAndroid ());
            shadow.SetStroke ((int)button.BorderWidth, button.ShadowColor.ToAndroid ());
            shadow.SetCornerRadius (button.BorderRadius);

            _normal = new Android.Graphics.Drawables.LayerDrawable (
                new Drawable [] { shadow, normal }
            );

            var density = 3;

            _normal.SetLayerInset (1, 0, 0, 0, 2 * density);

            _pressed = new Android.Graphics.Drawables.GradientDrawable ();

            var highlight = Context.ObtainStyledAttributes (new int [] {
                Android.Resource.Attribute.ColorActivatedHighlight
            }).GetColor(0, Android.Graphics.Color.Gray);
            _pressed.SetColor (highlight);
            _pressed.SetStroke ((int)button.BorderWidth, button.BorderColor.ToAndroid ());
            _pressed.SetCornerRadius (button.BorderRadius);

            var sld = new StateListDrawable ();
            sld.AddState (new int[] { Android.Resource.Attribute.StatePressed }, _pressed);
            sld.AddState (new int[] {}, _normal );

            Control.SetPaddingRelative (0, 0, 2, 0);
            Control.SetBackgroundDrawable (sld);
        }
Ejemplo n.º 10
0
        private void InitializeImages()
        {
            if(_pressedImage != null)
            {
                _pressedImage.Recycle();
                _pressedImage.Dispose();
            }
            if(_unpressedImage != null)
            {
                _unpressedImage.Recycle();
                _unpressedImage.Dispose();
            }

            _unpressedImage = Bitmap.CreateBitmap(Width, Settings.IsSquared ? Width : Height, Bitmap.Config.Argb8888);
            _pressedImage = Bitmap.CreateBitmap(Width, Settings.IsSquared ? Width : Height, Bitmap.Config.Argb8888);
            Canvas unpressedCanvas = new Canvas(_unpressedImage);
            Canvas pressedCanvas = new Canvas(_pressedImage);

            Settings.SetTextSize(ComplexUnitType.Px, Settings.TextSize == 0f ? Height * Settings.TextSizeRatio : Settings.TextSize);
            Settings.StrokeBorderWidth = Height * Settings.StrokeBorderWidthRatio;
            Settings.StrokeTextWidth = Height * Settings.StrokeTextWidthRatio;

            // Background fill paint
            Paint fillBackPaint = new Paint();
            fillBackPaint.Color = Settings.FillColor;
            fillBackPaint.AntiAlias = true;

            // Background stroke paint
            Paint strokeBackPaint = new Paint();
            strokeBackPaint.Color = Settings.StrokeColor;
            strokeBackPaint.SetStyle(Paint.Style.Stroke);
            strokeBackPaint.StrokeWidth = Settings.StrokeBorderWidth;
            strokeBackPaint.AntiAlias = true;

            // Text paint
            Paint textPaint = new Paint();
            textPaint.Color = Settings.IsTextStroked ? Settings.FillColor : Settings.StrokeColor;
            textPaint.TextAlign = Paint.Align.Center;
            textPaint.TextSize = Settings.TextSize;
            textPaint.SetTypeface(Settings.Typeface);
            textPaint.AntiAlias = true;

            // Text stroke paint
            Paint strokePaint = new Paint();
            strokePaint.Color = Settings.StrokeColor;
            strokePaint.TextAlign = Paint.Align.Center;
            strokePaint.TextSize = Settings.TextSize;
            strokePaint.SetTypeface(Settings.Typeface);
            strokePaint.SetStyle(Paint.Style.Stroke);
            strokePaint.StrokeWidth = Settings.StrokeTextWidth;
            strokePaint.AntiAlias = true;

            // Background bounds
            Rect local = new Rect();
            this.GetLocalVisibleRect(local);
            RectF bounds = new RectF(local);
            bounds.Top += Settings.StrokeBorderWidth/2;
            bounds.Left += Settings.StrokeBorderWidth/2;
            bounds.Right -= Settings.StrokeBorderWidth/2;
            bounds.Bottom -= Settings.StrokeBorderWidth/2;

            while(bounds.Top > Height)
            {
                bounds.Top -= Height;
            }
            while(bounds.Bottom > Height)
            {
                bounds.Bottom -= Height;
            }
            while(bounds.Left > Width)
            {
                bounds.Left -= Width;
            }
            while(bounds.Right > Width)
            {
                bounds.Right -= Width;
            }

            // Text location
            Rect r = new Rect();
            strokePaint.GetTextBounds(Text, 0, Text.Length, r);
            while(r.Width() + Settings.StrokeTextWidth >= bounds.Width())
            {
                Settings.SetTextSize(ComplexUnitType.Px, Settings.TextSize-1);
                textPaint.TextSize = Settings.TextSize;
                strokePaint.TextSize = Settings.TextSize;
                strokePaint.GetTextBounds(Text, 0, Text.Length, r);
            }

            float x=0, y=0;
            switch (Settings.Gravity)
            {
            case GravityFlags.Top:
                y = PaddingTop + r.Height()/2;
                break;
            case GravityFlags.Bottom:
                y = Height - r.Height()/2 - PaddingBottom;
                break;
            default:
                y = Height / 2f + r.Height() / 2f - r.Bottom;
                break;
            }
            switch (Settings.Gravity)
            {
            case GravityFlags.Left:
                x = PaddingLeft + r.Width()/2;
                break;
            case GravityFlags.Right:
                x = Width - r.Width()/2 - PaddingRight;
                break;
            default:
                x = Width/2;
                break;
            }

            // Draw unpressed
            DrawBackground(unpressedCanvas, bounds, fillBackPaint, strokeBackPaint);
            if(Settings.IsTextStroked)
                unpressedCanvas.DrawText(Text, x, y, strokePaint);
            unpressedCanvas.DrawText(Text, x, y, textPaint);

            // Change colors
            fillBackPaint.Color = Settings.StrokeColor;
            strokeBackPaint.Color = Settings.FillColor;
            strokePaint.Color = Settings.FillColor;
            textPaint.Color = Settings.IsTextStroked ? Settings.StrokeColor : Settings.FillColor;

            // Draw pressed
            DrawBackground(pressedCanvas, bounds, fillBackPaint, strokeBackPaint);
            if(Settings.IsTextStroked)
                pressedCanvas.DrawText(Text, x, y, strokePaint);
            pressedCanvas.DrawText(Text, x, y, textPaint);

            // Set images for states
            StateListDrawable states = new StateListDrawable();
            states.AddState(new int[] {Android.Resource.Attribute.StatePressed}, new BitmapDrawable(_pressedImage));
            states.AddState(new int[] {Android.Resource.Attribute.StateFocused}, new BitmapDrawable(_pressedImage));
            states.AddState(new int[] {Android.Resource.Attribute.StateSelected}, new BitmapDrawable(_pressedImage));
            states.AddState(new int[] { }, new BitmapDrawable(_unpressedImage));
            SetBackgroundDrawable(states);

            strokePaint.Dispose();
            textPaint.Dispose();
            strokeBackPaint.Dispose();
            fillBackPaint.Dispose();
            unpressedCanvas.Dispose();
            pressedCanvas.Dispose();
        }
 private void UpdateBackground ()
 {
   var drawable = new StateListDrawable ();
   drawable.AddState (new int[] { Android.Resource.Attribute.StatePressed }, CreateDrawable (colorPressed));
   drawable.AddState (new int[] { -Android.Resource.Attribute.StateEnabled }, CreateDrawable (colorDisabled));
   drawable.AddState (new int[] { }, CreateDrawable (colorNormal));
   SetBackgroundCompat (drawable);
 }
		public static void SetTheme (ToggleButton toggleButton, FlatTheme theme, int padding, int size)
		{
			toggleButton.SetWidth(size * 5);
			toggleButton.SetHeight(size);

			//setTextOff("");
			//setTextOn("");

			int radius = size - 4;

			float[] outerR = new float[]{radius, radius, radius, radius, radius, radius, radius, radius};

			// creating unchecked-enabled state drawable
			var uncheckedEnabledFrontCore = new ShapeDrawable(new RoundRectShape(outerR, null, null));
			uncheckedEnabledFrontCore.Paint.Color = theme.LightAccentColor;
			var uncheckedEnabledFront = new InsetDrawable(uncheckedEnabledFrontCore, 5);

			var uncheckedEnabledBack = new ShapeDrawable(new RoundRectShape(outerR, null, null));
			uncheckedEnabledBack.Paint.Color = Color.ParseColor("#f2f2f2");
			uncheckedEnabledBack.SetPadding(0, 0, size / 2 * 5, 0);

			Drawable[] d1 = {uncheckedEnabledBack, uncheckedEnabledFront};
			var uncheckedEnabled = new LayerDrawable(d1);

			// creating checked-enabled state drawable
			var checkedEnabledFrontCore = new ShapeDrawable(new RoundRectShape(outerR, null, null));
			checkedEnabledFrontCore.Paint.Color = theme.LightAccentColor;
			var checkedEnabledFront = new InsetDrawable(checkedEnabledFrontCore, 5);

			ShapeDrawable checkedEnabledBack = new ShapeDrawable(new RoundRectShape(outerR, null, null));
			checkedEnabledBack.Paint.Color = theme.VeryLightAccentColor;
			checkedEnabledBack.SetPadding(size / 2 * 5, 0, 0, 0);

			Drawable[] d2 = {checkedEnabledBack, checkedEnabledFront};
			LayerDrawable checkedEnabled = new LayerDrawable(d2);

			// creating unchecked-disabled state drawable
			ShapeDrawable uncheckedDisabledFrontCore = new ShapeDrawable(new RoundRectShape(outerR, null, null));
			uncheckedDisabledFrontCore.Paint.Color = Color.ParseColor("#d2d2d2");
			InsetDrawable uncheckedDisabledFront = new InsetDrawable(uncheckedDisabledFrontCore, 5);

			ShapeDrawable uncheckedDisabledBack = new ShapeDrawable(new RoundRectShape(outerR, null, null));
			uncheckedDisabledBack.Paint.Color = Color.ParseColor("#f2f2f2");
			uncheckedDisabledBack.SetPadding(0, 0, size / 2 * 5, 0);

			Drawable[] d3 = {uncheckedDisabledBack, uncheckedDisabledFront};
			LayerDrawable uncheckedDisabled = new LayerDrawable(d3);

			// creating checked-disabled state drawable
			ShapeDrawable checkedDisabledFrontCore = new ShapeDrawable(new RoundRectShape(outerR, null, null));
			checkedDisabledFrontCore.Paint.Color = theme.VeryLightAccentColor;
			InsetDrawable checkedDisabledFront = new InsetDrawable(checkedDisabledFrontCore, 5);

			ShapeDrawable checkedDisabledBack = new ShapeDrawable(new RoundRectShape(outerR, null, null));
			checkedDisabledBack.Paint.Color = Color.ParseColor("#f2f2f2");
			checkedDisabledBack.SetPadding(size / 2 * 5, 0, 0, 0);

			Drawable[] d4 = {checkedDisabledBack, checkedDisabledFront};
			LayerDrawable checkedDisabled = new LayerDrawable(d4);

			toggleButton.SetPadding(0, padding, 0, padding);

			PaintDrawable paintDrawable = new PaintDrawable(theme.BackgroundColor);
			paintDrawable.SetIntrinsicHeight(size);
			paintDrawable.SetIntrinsicWidth(size);
			paintDrawable.SetPadding(size, 0, 0, 0);

			StateListDrawable states = new StateListDrawable();

			states.AddState(new int[]{-Android.Resource.Attribute.StateChecked, Android.Resource.Attribute.StateEnabled},
				new InsetDrawable(uncheckedEnabled, padding * 2));
			states.AddState(new int[]{Android.Resource.Attribute.StateChecked, Android.Resource.Attribute.StateEnabled},
				new InsetDrawable(checkedEnabled, padding * 2));
			states.AddState(new int[]{-Android.Resource.Attribute.StateChecked, -Android.Resource.Attribute.StateEnabled},
				new InsetDrawable(uncheckedDisabled, padding * 2));
			states.AddState(new int[]{Android.Resource.Attribute.StateChecked, -Android.Resource.Attribute.StateEnabled},
				new InsetDrawable(checkedDisabled, padding * 2));

			toggleButton.SetBackgroundDrawable(states);

			toggleButton.SetTextSize(ComplexUnitType.Sp, 0);
		}
Ejemplo n.º 13
0
		public static void SetTheme(Button button, FlatTheme theme, FlatUI.FlatTextAppearance textAppearance,
			FlatUI.FlatFontFamily fontFamily, FlatUI.FlatFontWeight fontWeight, bool isFullFlat, int padding, int radius)
		{
			var bottom = 5;

			float[] outerR = {radius, radius, radius, radius, radius, radius, radius, radius};

			// creating normal state drawable
			var normalFront = new ShapeDrawable(new RoundRectShape(outerR, null, null));
			normalFront.Paint.Color = theme.LightAccentColor;
			normalFront.SetPadding(padding, padding, padding, padding);

			var normalBack = new ShapeDrawable(new RoundRectShape(outerR, null, null));
			normalBack.Paint.Color = theme.BackgroundColor;

			if (isFullFlat) 
				bottom = 0;

			normalBack.SetPadding(0, 0, 0, bottom);

			Drawable[] d = {normalBack, normalFront};
			var normal = new LayerDrawable(d);

			// creating pressed state drawable
			var pressedFront = new ShapeDrawable(new RoundRectShape(outerR, null, null));
			pressedFront.Paint.Color = theme.BackgroundColor;

			var pressedBack = new ShapeDrawable(new RoundRectShape(outerR, null, null));
			pressedBack.Paint.Color = theme.DarkAccentColor;

			if (!isFullFlat) 
				pressedBack.SetPadding(0, 0, 0, 3);

			Drawable[] d2 = {pressedBack, pressedFront};
			var pressed = new LayerDrawable(d2);

			// creating disabled state drawable
			var disabledFront = new ShapeDrawable(new RoundRectShape(outerR, null, null));
			disabledFront.Paint.Color = theme.VeryLightAccentColor;

			var disabledBack = new ShapeDrawable(new RoundRectShape(outerR, null, null));
			disabledBack.Paint.Color = theme.LightAccentColor;
			if (!isFullFlat) disabledBack.SetPadding(0, 0, 0, padding);

			Drawable[] d3 = {disabledBack, disabledFront};
			var disabled = new LayerDrawable(d3);

			var states = new StateListDrawable();

			states.AddState(new []{ Android.Resource.Attribute.StatePressed, Android.Resource.Attribute.StateEnabled }, pressed);
			states.AddState(new []{ Android.Resource.Attribute.StateFocused, Android.Resource.Attribute.StateEnabled }, pressed);
			states.AddState(new []{ Android.Resource.Attribute.StateEnabled }, normal);
			states.AddState(new []{-Android.Resource.Attribute.StateEnabled}, disabled);

			button.SetBackgroundDrawable (states);

			if (textAppearance == FlatUI.FlatTextAppearance.Dark) 
				button.SetTextColor(theme.DarkAccentColor);
			else if (textAppearance == FlatUI.FlatTextAppearance.Light) 
				button.SetTextColor(theme.VeryLightAccentColor);
			else 
				button.SetTextColor(Android.Graphics.Color.White);

			var typeface = FlatUI.GetFont(button.Context, fontFamily, fontWeight);
			if (typeface != null)
				button.SetTypeface(typeface, Android.Graphics.TypefaceStyle.Normal);
		}