private void SetBackgroundCompat(Drawable drawable) { if (HasLollipopApi) { #if __ANDROID_21__ var elevation = 0.0f; if (hasShadow) { elevation = Elevation > 0.0f ? Elevation : Resources.GetDimensionPixelSize(Resource.Dimension.fab_elevation_lollipop); } Elevation = elevation; var states = new int[][] { new int[] { } }; var rippleDrawable = new RippleDrawable(new Android.Content.Res.ColorStateList(states, new int[] { colorRipple }), drawable, null); OutlineProvider = new MyOutlineProvider(Resources, size); ClipToOutline = true; Background = rippleDrawable; #endif } else if (HasJellyBeanApi) { #if __ANDROID_16__ Background = drawable; #endif } else { ViewCompat.SetBackground(this, drawable); } }
protected override void OnElementChanged(ElementChangedEventArgs <StackLayout> e) { base.OnElementChanged(e); if (e.OldElement != null || Element == null) { return; } try { var stack = e.NewElement as GradientStackLayout; this.StartColor = stack.StartColor; this.EndColor = stack.EndColor; var gradient = new GradientDrawable( GradientDrawable.Orientation.TopBottom, new[] { this.StartColor.ToAndroid().ToArgb(), this.EndColor.ToAndroid().ToArgb() } ); ViewCompat.SetBackground(this, gradient); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(@"ERROR:", ex.Message); } }
private void setTabBG(int tab1, int tab2) { if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.JellyBeanMr1) { ViewGroup tabStrip = (ViewGroup)tabLayout.GetChildAt(0); View tabView1 = tabStrip.GetChildAt(0); View tabView2 = tabStrip.GetChildAt(1); if (tabView1 != null) { int paddingStart = tabView1.PaddingStart; int paddingTop = tabView1.PaddingTop; int paddingEnd = tabView1.PaddingEnd; int paddingBottom = tabView1.PaddingBottom; ViewCompat.SetBackground(tabView1, AppCompatResources.GetDrawable(this.Context, tab1)); ViewCompat.SetPaddingRelative(tabView1, paddingStart, paddingTop, paddingEnd, paddingBottom); } if (tabView2 != null) { int paddingStart = tabView2.PaddingStart; int paddingTop = tabView2.PaddingTop; int paddingEnd = tabView2.PaddingEnd; int paddingBottom = tabView2.PaddingBottom; ViewCompat.SetBackground(tabView2, AppCompatResources.GetDrawable(this.Context, tab2)); ViewCompat.SetPaddingRelative(tabView2, paddingStart, paddingTop, paddingEnd, paddingBottom); } } }
protected virtual void SetBackgroundColor(BottomNavigationView bottomView, Color color) { var menuView = bottomView.GetChildAt(0) as BottomNavigationMenuView; var oldBackground = bottomView.Background; var colorDrawable = oldBackground as ColorDrawable; var colorChangeRevealDrawable = oldBackground as ColorChangeRevealDrawable; AColor lastColor = colorChangeRevealDrawable?.EndColor ?? colorDrawable?.Color ?? Colors.Transparent.ToPlatform(); AColor newColor; if (color == null) { newColor = ShellView.DefaultBottomNavigationViewBackgroundColor.ToPlatform(); } else { newColor = color.ToPlatform(); } if (menuView == null) { if (colorDrawable != null && lastColor == newColor) { return; } if (lastColor != newColor || colorDrawable == null) { // taken from android source code var backgroundColor = new MaterialShapeDrawable(); backgroundColor.FillColor = ColorStateList.ValueOf(newColor); backgroundColor.InitializeElevationOverlay(bottomView.Context); ViewCompat.SetBackground(bottomView, backgroundColor); } } else { if (colorChangeRevealDrawable != null && lastColor == newColor) { return; } var index = ((IShellItemController)_shellItem).GetItems().IndexOf(_shellItem.CurrentItem); var menu = bottomView.Menu; index = Math.Min(index, menu.Size() - 1); var child = menuView.GetChildAt(index); if (child == null) { return; } var touchPoint = new Point(child.Left + (child.Right - child.Left) / 2, child.Top + (child.Bottom - child.Top) / 2); ViewCompat.SetBackground(bottomView, new ColorChangeRevealDrawable(lastColor, newColor, touchPoint)); } }
protected override void OnElementChanged(ElementChangedEventArgs <Frame> e) { base.OnElementChanged(e); if (e.NewElement != null && Control != null && e.NewElement is GradientFrame frame) { var gradient = new GradientDrawable(GradientDrawable.Orientation.TlBr, new[] { frame.StartColor.ToAndroid().ToArgb(), frame.MiddleColor.ToAndroid().ToArgb(), frame.EndColor.ToAndroid().ToArgb() }); ViewCompat.SetBackground(this, gradient); UpdateCornerRadius(); } }
/// <summary> /// Method to set the color of the material design ripple when the button is clicked. /// If the Android version is less than Lollipop (5.0) then the ripple will be a solid color /// Note: the default is the default for material design. /// </summary> /// <params> /// Color /// </params> public void SetRippleColor(Color rippleColor) { ViewCompat.SetBackground(_button, GetImageButtonBackground(rippleColor)); }
protected override void DispatchDraw(global::Android.Graphics.Canvas canvas) { GradientLayout layout = (GradientLayout)this.Element; Color[] Colors = layout.Colors; GradientColorStackMode Mode = layout.Mode; int[] colors = new int[Colors.Length]; for (int i = 0, l = Colors.Length; i < l; i++) { colors[i] = Colors[i].ToAndroid().ToArgb(); } // temporary workaround for Android 9 PIE if (Android.OS.Build.VERSION.SdkInt > Android.OS.BuildVersionCodes.O) { GradientDrawable.Orientation orientation; switch (Mode) { default: case GradientColorStackMode.ToRight: orientation = GradientDrawable.Orientation.LeftRight; break; case GradientColorStackMode.ToTop: orientation = GradientDrawable.Orientation.BottomTop; break; case GradientColorStackMode.ToBottom: orientation = GradientDrawable.Orientation.TopBottom; break; case GradientColorStackMode.ToTopLeft: orientation = GradientDrawable.Orientation.BrTl; break; case GradientColorStackMode.ToTopRight: orientation = GradientDrawable.Orientation.BlTr; break; case GradientColorStackMode.ToBottomLeft: orientation = GradientDrawable.Orientation.TrBl; break; case GradientColorStackMode.ToBottomRight: orientation = GradientDrawable.Orientation.TlBr; break; } var gradient2 = new GradientDrawable(orientation, colors); ViewCompat.SetBackground(this, gradient2); base.DispatchDraw(canvas); return; } Android.Graphics.LinearGradient gradient; switch (Mode) { default: case GradientColorStackMode.ToRight: gradient = new Android.Graphics.LinearGradient(0, 0, Width, 0, colors, null, Android.Graphics.Shader.TileMode.Mirror); break; case GradientColorStackMode.ToLeft: gradient = new Android.Graphics.LinearGradient(Width, 0, 0, 0, colors, null, Android.Graphics.Shader.TileMode.Mirror); break; case GradientColorStackMode.ToTop: gradient = new Android.Graphics.LinearGradient(0, Height, 0, 0, colors, null, Android.Graphics.Shader.TileMode.Mirror); break; case GradientColorStackMode.ToBottom: gradient = new Android.Graphics.LinearGradient(0, 0, 0, Height, colors, null, Android.Graphics.Shader.TileMode.Mirror); break; case GradientColorStackMode.ToTopLeft: gradient = new Android.Graphics.LinearGradient(Width, Height, 0, 0, colors, null, Android.Graphics.Shader.TileMode.Mirror); break; case GradientColorStackMode.ToTopRight: gradient = new Android.Graphics.LinearGradient(0, Height, Width, 0, colors, null, Android.Graphics.Shader.TileMode.Mirror); break; case GradientColorStackMode.ToBottomLeft: gradient = new Android.Graphics.LinearGradient(Width, 0, 0, Height, colors, null, Android.Graphics.Shader.TileMode.Mirror); break; case GradientColorStackMode.ToBottomRight: gradient = new Android.Graphics.LinearGradient(0, 0, Width, Height, colors, null, Android.Graphics.Shader.TileMode.Mirror); break; } var paint = new Android.Graphics.Paint() { Dither = true, }; paint.SetShader(gradient); canvas.DrawPaint(paint); base.DispatchDraw(canvas); }
public void OnItemClear() { ViewCompat.SetBackground(_itemView, _initialColor); }