Beispiel #1
0
        public static void UpdateBackground(this GradientDrawable gradientDrawable, Brush brush, int height, int width)
        {
            if (gradientDrawable == null || brush == null || brush.IsEmpty)
            {
                return;
            }

            if (brush is SolidColorBrush solidColorBrush)
            {
                Color bgColor = solidColorBrush.Color;
                gradientDrawable.SetColor(bgColor.IsDefault ? Color.Default.ToAndroid() : bgColor.ToAndroid());
            }

            if (brush is LinearGradientBrush linearGradientBrush)
            {
                var p1 = linearGradientBrush.StartPoint;
                var x1 = (float)p1.X;
                var y1 = (float)p1.Y;

                var p2 = linearGradientBrush.EndPoint;
                var x2 = (float)p2.X;
                var y2 = (float)p2.Y;

                const double Rad2Deg = 180.0 / Math.PI;
                var          angle   = Math.Atan2(y2 - y1, x2 - x1) * Rad2Deg;

                var gradientBrushData = linearGradientBrush.GetGradientBrushData();
                var colors            = gradientBrushData.Item1;

                if (colors.Length < 2)
                {
                    return;
                }

                gradientDrawable.SetGradientType(GradientType.LinearGradient);
                gradientDrawable.SetColors(colors);
                gradientDrawable.SetGradientOrientation(angle);
            }

            if (brush is RadialGradientBrush radialGradientBrush)
            {
                var   center  = radialGradientBrush.Center;
                float centerX = (float)center.X;
                float centerY = (float)center.Y;
                float radius  = (float)radialGradientBrush.Radius;

                var gradientBrushData = radialGradientBrush.GetGradientBrushData();
                var colors            = gradientBrushData.Item1;

                if (colors.Length < 2)
                {
                    return;
                }

                gradientDrawable.SetGradientType(GradientType.RadialGradient);
                gradientDrawable.SetGradientCenter(centerX, centerY);
                gradientDrawable.SetGradientRadius(Math.Max(height, width) * radius);
                gradientDrawable.SetColors(colors);
            }
        }
Beispiel #2
0
        /**
         * 创建阴影的GradientDrawable
         */
        private void createDrawable()
        {
            var uintValue = 0xb0333333;

            int[] color = { 0x333333, (int)uintValue };
            mFolderShadowDrawableRL = new GradientDrawable(GradientDrawable.Orientation.RightLeft, color);
            mFolderShadowDrawableRL.SetGradientType(GradientType.LinearGradient);

            mFolderShadowDrawableLR = new GradientDrawable(GradientDrawable.Orientation.LeftRight, color);
            mFolderShadowDrawableLR.SetGradientType(GradientType.LinearGradient);
            var uintValue2 = 0xff111111;

            mBackShadowColors     = new int[] { (int)uintValue2, 0x111111 };
            mBackShadowDrawableRL = new GradientDrawable(GradientDrawable.Orientation.RightLeft, mBackShadowColors);
            mBackShadowDrawableRL.SetGradientType(GradientType.LinearGradient);

            mBackShadowDrawableLR = new GradientDrawable(GradientDrawable.Orientation.LeftRight, mBackShadowColors);
            mBackShadowDrawableLR.SetGradientType(GradientType.LinearGradient);
            var uintValue3 = 0x80111111;

            mFrontShadowColors      = new int[] { (int)uintValue3, 0x111111 };
            mFrontShadowDrawableVLR = new GradientDrawable(GradientDrawable.Orientation.LeftRight, mFrontShadowColors);
            mFrontShadowDrawableVLR.SetGradientType(GradientType.LinearGradient);
            mFrontShadowDrawableVRL = new GradientDrawable(GradientDrawable.Orientation.RightLeft, mFrontShadowColors);
            mFrontShadowDrawableVRL.SetGradientType(GradientType.LinearGradient);

            mFrontShadowDrawableHTB = new GradientDrawable(GradientDrawable.Orientation.TopBottom, mFrontShadowColors);
            mFrontShadowDrawableHTB.SetGradientType(GradientType.LinearGradient);

            mFrontShadowDrawableHBT = new GradientDrawable(GradientDrawable.Orientation.BottomTop, mFrontShadowColors);
            mFrontShadowDrawableHBT.SetGradientType(GradientType.LinearGradient);
        }
        protected override void OnElementChanged(ElementChangedEventArgs <Xamarin.Forms.Button> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                try
                {
                    almeButton = e.NewElement as AlmeButton;
                    GradientDrawable gradientDrawable = new GradientDrawable();
                    gradientDrawable.SetShape(ShapeType.Rectangle);
                    gradientDrawable.SetCornerRadius(almeButton.BorderRadius * 2);
                    if (almeButton.CenterColor != Xamarin.Forms.Color.Transparent)
                    {
                        gradientDrawable.SetColors(new int[] { almeButton.EndColor.ToAndroid(),
                                                               almeButton.CenterColor.ToAndroid(), almeButton.StartColor.ToAndroid() });
                    }
                    else
                    {
                        gradientDrawable.SetColors(new int[] { almeButton.EndColor.ToAndroid(),
                                                               almeButton.StartColor.ToAndroid() });
                    }
                    gradientDrawable.SetGradientType(GradientType.LinearGradient);
                    gradientDrawable.SetOrientation(GradientDrawable.Orientation.RightLeft);
#pragma warning disable CS0618 // Type or member is obsolete
                    Control.SetBackgroundDrawable(gradientDrawable);
#pragma warning restore CS0618 // Type or member is obsolete
                    Control.SetTextColor(almeButton.TextColor.ToAndroid());
                    if (almeButton.FontFamily != null)
                    {
                        Control.Typeface = Typeface.CreateFromAsset(Forms.Context.ApplicationContext.Assets, almeButton.FontFamily.Split('#')[0]);
                    }
                }
                catch { }
            }
        }
Beispiel #4
0
        protected override void OnElementChanged(ElementChangedEventArgs <Button> e)
        {
            base.OnElementChanged(e);


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

                // See ButtonRendererWithNavFix.cs
                CustomClickHandler(button);

                // Applies a "blue" gradient to our Android navigation buttons
                var gradientDrawable = new GradientDrawable();

                gradientDrawable.SetOrientation(GradientDrawable.Orientation.TlBr);

                gradientDrawable.SetColors(new int[] { Color.Argb(255, 20, 143, 194), Color.Argb(255, 0, 44, 119) });
                gradientDrawable.SetGradientType(GradientType.RadialGradient);
                gradientDrawable.SetGradientRadius(300);
                gradientDrawable.SetGradientCenter(0.5f, 0);
                button.SetBackgroundDrawable(gradientDrawable);


                // Sets a small drop shadow to the buttons' text
                button.SetShadowLayer(1, 1, 1, Color.Black);
            }
        }
Beispiel #5
0
        protected override void OnElementChanged(ElementChangedEventArgs <Label> e)
        {
            base.OnElementChanged(e);

            LabelWithBorder model = (LabelWithBorder)Element;

            if (e.OldElement != null)
            {
                // Unsubscribe from event handlers and cleanup any resources
            }

            if (e.NewElement != null)
            {
                if (Control != null)
                {
                    GradientDrawable gd = new GradientDrawable();

                    if (!string.IsNullOrEmpty(model.Text))
                    {
                        gd.SetShape(ShapeType.Rectangle);
                        gd.SetGradientType(GradientType.LinearGradient);
                        gd.SetCornerRadius(model.CornerRadius * 4);
                        gd.SetStroke(model.BorderThickness, model.BorderColor.ToAndroid());
                        gd.SetColor(model.DrawableColor.ToAndroid());

                        Control.SetPadding(2, 2, 2, 2);
                        Control.SetBackground(gd);
                    }
                }
            }
        }
        private void UpdateGradientBackground()
        {
            var button = this.Element as AppButton;

            if (button != null && button.GradientEnabled)
            {
                int[] colors;

                if (button.MiddleColorEnabled)
                {
                    colors = new int[] { button.StartColor.ToAndroid(), button.MiddleColor.ToAndroid(), button.EndColor.ToAndroid() };
                }
                else
                {
                    colors = new int[] { button.StartColor.ToAndroid(), button.EndColor.ToAndroid() };
                }

                var gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TopBottom, colors);

                switch (button.Orientation)
                {
                case "TopBottom":
                {
                    gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TopBottom, colors);
                    break;
                }

                case "BottomTop":
                {
                    gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.BottomTop, colors);
                    break;
                }

                case "RightLeft":
                {
                    gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.RightLeft, colors);
                    break;
                }

                case "LeftRight":
                {
                    gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.LeftRight, colors);
                    break;
                }
                }

                gradientDrawable.SetGradientType(GradientType.LinearGradient);
                gradientDrawable.SetShape(ShapeType.Rectangle);
                gradientDrawable.SetCornerRadius(DpToPixels(this.Context, Convert.ToSingle(button.FixedBorderRadius)));
                gradientDrawable.SetStroke((int)button.BorderWidth, button.BorderColor.ToAndroid());
                this.Control.Background = gradientDrawable;
            }
        }
Beispiel #7
0
        void UpdateEntry()
        {
            var element = Element as AppEntry;

            int[] colors           = new int[] { element.StartColor.ToAndroid(), element.EndColor.ToAndroid() };
            var   gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TopBottom, colors);

            gradientDrawable.SetGradientType(GradientType.LinearGradient);
            gradientDrawable.SetShape(ShapeType.Rectangle);
            gradientDrawable.SetCornerRadius(DpToPixels(this.Context, Convert.ToSingle(element.BorderRadius)));
            gradientDrawable.SetStroke((int)element.BorderWidth, element.BorderColor.ToAndroid());
            this.Control.Background = gradientDrawable;
        }
        protected override void OnAttached()
        {
            if (gradientDrawable == null)
            {
                gradientDrawable   = new GradientDrawable();
                Control.Background = gradientDrawable;
                var effect = (PQGradiantEffect)Element.Effects.FirstOrDefault(e => e is PQGradiantEffect);
                if (effect == null)
                {
                    return;
                }
                SetColors();
                if (!effect.IsRadial)
                {
                    switch (effect.Oriantaion)
                    {
                    case Helpers.Shared.GradiantOriantaion.LeftToRight:
                        gradientDrawable.SetOrientation(GradientDrawable.Orientation.LeftRight);
                        break;

                    case Helpers.Shared.GradiantOriantaion.RightToLeft:
                        gradientDrawable.SetOrientation(GradientDrawable.Orientation.RightLeft);

                        break;

                    case Helpers.Shared.GradiantOriantaion.TopToBottom:
                        gradientDrawable.SetOrientation(GradientDrawable.Orientation.TopBottom);

                        break;

                    case Helpers.Shared.GradiantOriantaion.BottomToTop:
                        gradientDrawable.SetOrientation(GradientDrawable.Orientation.BottomTop);

                        break;

                    default:
                        gradientDrawable.SetOrientation(GradientDrawable.Orientation.LeftRight);

                        break;
                    }
                }
                gradientDrawable.SetGradientRadius(150);
                gradientDrawable.SetShape(ShapeType.Rectangle);

                gradientDrawable.SetCornerRadius(effect.Radius);
                gradientDrawable.SetGradientType(effect.IsRadial ? GradientType.RadialGradient : GradientType.LinearGradient);
            }
        }
            // Properties
            public override void AddUnderline( )
            {
                if (UnderlineView == null)
                {
                    // Define a gradiant underline that will be shown underneath the text
                    int[]            colors = new int[] { int.MaxValue, int.MaxValue };
                    GradientDrawable border = new GradientDrawable(GradientDrawable.Orientation.LeftRight, colors);
                    border.SetGradientType(GradientType.LinearGradient);

                    UnderlineView = new View(Rock.Mobile.PlatformSpecific.Android.Core.Context);
                    UnderlineView.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
                    UnderlineView.Background       = border;

                    UnderlineView.LayoutParameters.Height = 2;
                }
            }
        private void UpdateGradientBackground()
        {
            var button = Element as AppButton;

            if (button != null && button.GradientEnabled)
            {
                int[] colors           = new int[] { button.StartColor.ToAndroid(), button.EndColor.ToAndroid() };
                var   gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TopBottom, colors);

                gradientDrawable.SetGradientType(GradientType.LinearGradient);
                gradientDrawable.SetShape(ShapeType.Rectangle);
                gradientDrawable.SetCornerRadius(DpToPixels(this.Context, Convert.ToSingle(button.FixedBorderRadius)));
                gradientDrawable.SetStroke((int)button.BorderWidth, button.BorderColor.ToAndroid());
                this.Control.Background = gradientDrawable;
            }
        }
        public OverlappedWidget(Context context, string bookId,
                                List <BookMixAToc.MixToc.Chapters> chaptersList,
                                IOnReadStateChangeListener listener) : base(context, bookId, chaptersList, listener)
        {
            mTouch.X = 0.01f;
            mTouch.Y = 0.01f;

            mPath0 = new Path();
            var uintValue = 0xaa666666;

            int[] mBackShadowColors = new int[] { (int)uintValue, 0x666666 };
            mBackShadowDrawableRL = new GradientDrawable(GradientDrawable.Orientation.RightLeft, mBackShadowColors);
            mBackShadowDrawableRL.SetGradientType(GradientType.LinearGradient);

            mBackShadowDrawableLR = new GradientDrawable(GradientDrawable.Orientation.LeftRight, mBackShadowColors);
            mBackShadowDrawableLR.SetGradientType(GradientType.LinearGradient);
        }
Beispiel #12
0
        protected override void OnElementChanged(ElementChangedEventArgs <Xamarin.Forms.Image> e)
        {
            base.OnElementChanged(e);

            int startColor = BaseControl.StartColor.ToAndroid().ToArgb();
            int endColor   = BaseControl.EndColor.ToAndroid().ToArgb();

            int[] colors = { startColor, endColor };
            //float[] radii = { 5, 5, 5, 5, 5, 5, 5, 5 };

            GradientDrawable.Orientation            orientation = null;
            Android.Graphics.Drawables.GradientType gradientType;

            switch (BaseControl.Orientation)
            {
            case GradientOrientation.BottomTop: orientation = GradientDrawable.Orientation.BottomTop; break;

            case GradientOrientation.LeftRight: orientation = GradientDrawable.Orientation.LeftRight; break;

            case GradientOrientation.RightLeft: orientation = GradientDrawable.Orientation.RightLeft; break;

            case GradientOrientation.TopBottom: orientation = GradientDrawable.Orientation.TopBottom; break;

            default: orientation = GradientDrawable.Orientation.TopBottom; break;
            }

            switch (BaseControl.GradientStyle)
            {
            case GradientType.Linear: gradientType = Android.Graphics.Drawables.GradientType.LinearGradient; break;

            case GradientType.Sweep: gradientType = Android.Graphics.Drawables.GradientType.SweepGradient; break;

            case GradientType.Radial: gradientType = Android.Graphics.Drawables.GradientType.RadialGradient; break;

            default: gradientType = gradientType = Android.Graphics.Drawables.GradientType.LinearGradient; break;
            }

            GradientDrawable drawable = new GradientDrawable(orientation, colors);

            drawable.SetGradientType(gradientType);
            //drawable.SetCornerRadii(radii);
            //drawable.SetStroke(1, Android.Graphics.Color.ParseColor("#FF000000"));
            drawable.SetCornerRadius(0f);
            this.Control.SetBackgroundDrawable(drawable);
        }
Beispiel #13
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <param name="colors">new int[]{Color.LightGreen,Color.DarkGreen,}</param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="showBorderWhite">Set 3 pixels width solid color border</param>
        /// <param name="showCornerRadius">Set CornerRadius 10</param>
        /// <returns></returns>
        public static (GradientDrawable, Bitmap) GetGradientDrawable(Activity context, int[] colors, int width,
                                                                     int height, bool showBorderWhite = false, bool showCornerRadius = false)
        {
            try
            {
                // Initialize a new GradientDrawable
                var gd = new GradientDrawable();

                // Set the color array to draw gradient
                gd.SetColors(colors);

                // Set the GradientDrawable gradient type linear gradient
                gd.SetGradientType(GradientType.LinearGradient);

                // Set GradientDrawable shape is a rectangle
                gd.SetShape(ShapeType.Rectangle);

                // Set 3 pixels width solid color border
                if (showBorderWhite)
                {
                    gd.SetStroke(3, Color.White);
                }

                // Set GradientDrawable width and in pixels
                gd.SetSize(width, height); // Width 450 pixels and height 150 pixels

                // Set CornerRadius
                if (showCornerRadius)
                {
                    gd.SetCornerRadius(10);
                }

                var bitmap = BitmapUtil.ConvertToBitmap(gd, width, height);

                return(gd, bitmap);
            }
            catch (Exception e)
            {
                Methods.DisplayReportResultTrack(e);
                return(null, null);
            }
        }
        public static Drawable CreateBackgroundGradient(Color startColor, Color endColor, Color centerColor,
                                                        float cornerRadius, GradientDrawable.Orientation orientation)
        {
            GradientDrawable gradientDrawable = new GradientDrawable();

            gradientDrawable.SetShape(ShapeType.Rectangle);
            gradientDrawable.SetCornerRadius(cornerRadius);
            if (centerColor != Xamarin.Forms.Color.Transparent.ToAndroid())
            {
                gradientDrawable.SetColors(new int[] { startColor,
                                                       centerColor, endColor });
            }
            else
            {
                gradientDrawable.SetColors(new int[] { startColor, endColor });
            }
            gradientDrawable.SetGradientType(GradientType.LinearGradient);
            gradientDrawable.SetOrientation(orientation);
            return(gradientDrawable);
        }
        protected override void OnElementChanged(ElementChangedEventArgs <Button> e)
        {
            base.OnElementChanged(e);

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

                // Applies a "gray" gradient to our Android popup buttons
                var gradientDrawable = new GradientDrawable();
                gradientDrawable.SetOrientation(GradientDrawable.Orientation.TlBr);

                gradientDrawable.SetColors(new int[] { Color.Argb(255, 167, 169, 172), Color.Argb(255, 76, 76, 76) });
                gradientDrawable.SetGradientType(GradientType.RadialGradient);
                gradientDrawable.SetGradientRadius(300);
                gradientDrawable.SetGradientCenter(0.5f, 0);
                button.SetBackgroundDrawable(gradientDrawable);

                // Sets a small drop shadow to the buttons' text
                button.SetShadowLayer(1, 1, 1, Color.Black);
            }
        }
Beispiel #16
0
        void SetGradientOrientation()
        {
            var control = (GradientTabbedPage)this.Element;

            gradient.SetGradientType(GradientType.LinearGradient);

            if (control.Orientation == GradientOrientation.Horizontal)
            {
                gradient.SetOrientation(GradientDrawable.Orientation.LeftRight);
                gradient.SetColors(new int[] { control.StartColor.ToAndroid(), control.EndColor.ToAndroid() });
            }
            else if (control.Orientation == GradientOrientation.Vertical)
            {
                gradient.SetOrientation(GradientDrawable.Orientation.TopBottom);
                gradient.SetColors(new int[] { control.StartColor.ToAndroid(), control.EndColor.ToAndroid() });
            }
            else if (control.Orientation == GradientOrientation.BlTr)
            {
                gradient.SetOrientation(GradientDrawable.Orientation.BlTr);
                gradient.SetColors(new int[] { control.StartColor.ToAndroid(), control.EndColor.ToAndroid() });
            }
            else if (control.Orientation == GradientOrientation.BrTl)
            {
                gradient.SetOrientation(GradientDrawable.Orientation.BlTr);
                gradient.SetColors(new int[] { control.StartColor.ToAndroid(), control.EndColor.ToAndroid() });
            }
            else if (control.Orientation == GradientOrientation.TlBr)
            {
                gradient.SetOrientation(GradientDrawable.Orientation.TlBr);
                gradient.SetColors(new int[] { control.StartColor.ToAndroid(), control.EndColor.ToAndroid() });
            }
            else if (control.Orientation == GradientOrientation.TrBl)
            {
                gradient.SetOrientation(GradientDrawable.Orientation.TrBl);
                gradient.SetColors(new int[] { control.StartColor.ToAndroid(), control.EndColor.ToAndroid() });
            }
        }