private Drawable GetMaterialUiThumbDrawable(Color color)
        {
            var           circle = Circle(color);
            LayerDrawable ld     = new LayerDrawable(new Drawable[] { circle });

            ld.SetLayerInset(0, 19, 19, 19, 19);
            ld.SetBounds(0, 0, 64, 64);
            return(ld);
        }
        private Drawable GetDisabledThumbDrawable()
        {
            if (MaterialUI)
            {
                return(GetMaterialUiThumbDrawable(DefaultColor));
            }
            else
            {
                var outerCircle = Circle(DefaultGrayColor);
                var innerCircle = Circle(DefaultDarkBlueColor);

                LayerDrawable ld = new LayerDrawable(new Drawable[] { outerCircle, innerCircle });
                ld.SetLayerInset(0, 8, 8, 8, 8);
                ld.SetLayerInset(1, 28, 28, 28, 28);
                ld.SetBounds(0, 0, 64, 64);
                return(ld);
            }
        }
        private Drawable GetThumbDrawable()
        {
            if (MaterialUI)
            {
                return(GetMaterialUiThumbDrawable(ActiveColor));
            }
            else
            {
                var           outerCircle = Circle(DefaultLightBlueColor);
                var           innerCircle = Circle(DefaultDarkBlueColor);
                LayerDrawable ld          = new LayerDrawable(new Drawable[] { outerCircle, innerCircle });

                ld.SetLayerInset(0, 4, 4, 4, 4);
                ld.SetLayerInset(1, 23, 23, 23, 23);
                ld.SetBounds(0, 0, 64, 64);
                return(ld);
            }
        }
        private Drawable GetPressedThumbDrawable()
        {
            if (MaterialUI)
            {
                return(GetMaterialUiThumbDrawable(ActiveColor));
            }
            else
            {
                var outerCircle = Circle(DefaultDarkBlueColor);
                outerCircle.Paint.StrokeWidth = 4;
                outerCircle.Paint.SetStyle(Paint.Style.Stroke);
                var middleCircle = Circle(DefaultLightestBlueColor);
                var innerCircle  = Circle(DefaultDarkBlueColor);

                LayerDrawable ld = new LayerDrawable(new Drawable[] { outerCircle, middleCircle, innerCircle });
                ld.SetLayerInset(0, 4, 4, 4, 4);
                ld.SetLayerInset(1, 4, 4, 4, 4);
                ld.SetLayerInset(2, 23, 23, 23, 23);
                ld.SetBounds(0, 0, 64, 64);
                return(ld);
            }
        }
        private IDisposable BuildDrawableLayer()
        {
            if (_controlHeight == 0 || _controlWidth == 0)
            {
                return(Disposable.Empty);
            }

            var drawables = new List <Drawable>();

            var path = GetPath();

            if (path == null)
            {
                return(Disposable.Empty);
            }

            // Scale the path using its Stretch
            Android.Graphics.Matrix matrix = new Android.Graphics.Matrix();
            switch (this.Stretch)
            {
            case Media.Stretch.Fill:
            case Media.Stretch.None:
                matrix.SetScale((float)_scaleX, (float)_scaleY);
                break;

            case Media.Stretch.Uniform:
                var scale = Math.Min(_scaleX, _scaleY);
                matrix.SetScale((float)scale, (float)scale);
                break;

            case Media.Stretch.UniformToFill:
                scale = Math.Max(_scaleX, _scaleY);
                matrix.SetScale((float)scale, (float)scale);
                break;
            }
            path.Transform(matrix);

            // Move the path using its alignements
            var translation = new Android.Graphics.Matrix();

            var pathBounds = new RectF();

            // Compute the bounds. This is needed for stretched shapes and stroke thickness translation calculations.
            path.ComputeBounds(pathBounds, true);

            if (Stretch == Stretch.None)
            {
                // Since we are not stretching, ensure we are using (0, 0) as origin.
                pathBounds.Left = 0;
                pathBounds.Top  = 0;
            }

            if (!ShouldPreserveOrigin)
            {
                //We need to translate the shape to take in account the stroke thickness
                translation.SetTranslate((float)(-pathBounds.Left + PhysicalStrokeThickness * 0.5f), (float)(-pathBounds.Top + PhysicalStrokeThickness * 0.5f));
            }

            path.Transform(translation);

            // Draw the fill
            var drawArea = new Foundation.Rect(0, 0, _controlWidth, _controlHeight);

            var imageBrushFill = Fill as ImageBrush;

            if (imageBrushFill != null)
            {
                var bitmapDrawable = new BitmapDrawable(Context.Resources, imageBrushFill.TryGetBitmap(drawArea, () => RefreshShape(forceRefresh: true), path));
                drawables.Add(bitmapDrawable);
            }
            else
            {
                var fill      = Fill ?? SolidColorBrushHelper.Transparent;
                var fillPaint = fill.GetFillPaint(drawArea);

                var lineDrawable = new PaintDrawable();
                lineDrawable.Shape       = new PathShape(path, (float)_controlWidth, (float)_controlHeight);
                lineDrawable.Paint.Color = fillPaint.Color;
                lineDrawable.Paint.SetShader(fillPaint.Shader);
                lineDrawable.Paint.SetStyle(Paint.Style.Fill);
                lineDrawable.Paint.Alpha = fillPaint.Alpha;

                this.SetStrokeDashEffect(lineDrawable.Paint);

                drawables.Add(lineDrawable);
            }

            // Draw the contour
            if (Stroke != null)
            {
                using (var strokeBrush = new Paint(Stroke.GetStrokePaint(drawArea)))
                {
                    var lineDrawable = new PaintDrawable();
                    lineDrawable.Shape       = new PathShape(path, (float)_controlWidth, (float)_controlHeight);
                    lineDrawable.Paint.Color = strokeBrush.Color;
                    lineDrawable.Paint.SetShader(strokeBrush.Shader);
                    lineDrawable.Paint.StrokeWidth = (float)PhysicalStrokeThickness;
                    lineDrawable.Paint.SetStyle(Paint.Style.Stroke);
                    lineDrawable.Paint.Alpha = strokeBrush.Alpha;

                    this.SetStrokeDashEffect(lineDrawable.Paint);

                    drawables.Add(lineDrawable);
                }
            }

            var layerDrawable = new LayerDrawable(drawables.ToArray());

            // Set bounds must always be called, otherwise the android layout engine can't determine
            // the rendering size. See Drawable documentation for details.
            layerDrawable.SetBounds(0, 0, (int)_controlWidth, (int)_controlHeight);

            return(SetOverlay(this, layerDrawable));
        }
Beispiel #6
0
        Bitmap drawableToBitmap(Drawable drawable)
        {
            Bitmap bitmap = null;

            if (drawable is BitmapDrawable)
            {
                BitmapDrawable bitmapDrawable = (BitmapDrawable)drawable;
                if (bitmapDrawable.Bitmap != null)
                {
                    return(bitmapDrawable.Bitmap);
                }
            }
            if (drawable is AdaptiveIconDrawable)
            {
                AdaptiveIconDrawable adaptiveIconDrawable = (AdaptiveIconDrawable)drawable;
                if (!(adaptiveIconDrawable.IntrinsicWidth <= 0 || adaptiveIconDrawable.IntrinsicHeight <= 0))
                {
                    //bitmap = Bitmap.CreateBitmap(drawable.IntrinsicWidth, drawable.IntrinsicHeight, Bitmap.Config.Argb8888);
                    //Canvas c = new Canvas(bitmap);
                    var bd = adaptiveIconDrawable.Background;
                    var fd = adaptiveIconDrawable.Foreground;

                    Drawable[] drr = new Drawable[2];
                    drr[0] = bd;
                    drr[1] = fd;

                    LayerDrawable layerDrawable = new LayerDrawable(drr);

                    int width  = layerDrawable.IntrinsicWidth;
                    int height = layerDrawable.IntrinsicHeight;

                    bitmap = Bitmap.CreateBitmap(width, height, Bitmap.Config.Argb8888);

                    Canvas c = new Canvas(bitmap);

                    layerDrawable.SetBounds(0, 0, c.Width, c.Height);
                    layerDrawable.Draw(c);

                    return(bitmap);

                    //adaptiveIconDrawable.SetBounds(0, 0, c.Width, c.Height);
                    //adaptiveIconDrawable.Draw(c);

                    return(bitmap);
                }
            }

            if (drawable.IntrinsicWidth <= 0 || drawable.IntrinsicHeight <= 0)
            {
                bitmap = Bitmap.CreateBitmap(1, 1, Bitmap.Config.Argb8888); // Single color bitmap will be created of 1x1 pixel
            }
            else
            {
                bitmap = Bitmap.CreateBitmap(drawable.IntrinsicWidth, drawable.IntrinsicHeight, Bitmap.Config.Argb8888);
            }

            Canvas canvas = new Canvas(bitmap);

            drawable.SetBounds(0, 0, canvas.Width, canvas.Height);
            drawable.Draw(canvas);
            return(bitmap);
        }
Beispiel #7
0
        private void Init(Context context, IAttributeSet attrs)
        {
            Color thumbShadowColor;
            var   defaultShadowColor   = Color.Argb(75, 0, 0, 0);
            var   defaultShadowYOffset = PixelUtil.DpToPx(context, 2);
            var   defaultShadowXOffset = PixelUtil.DpToPx(context, 0);
            var   defaultShadowBlur    = PixelUtil.DpToPx(context, 2);

            _distanceToTop = PixelUtil.DpToPx(context, DefaultTextDistanceToTopInDp);

            if (attrs == null)
            {
                SetRangeToDefaultValues();
                _internalPad            = PixelUtil.DpToPx(context, InitialPaddingInDp);
                _barHeight              = PixelUtil.DpToPx(context, LineHeightInDp);
                ActiveColor             = DefaultDarkBlueColor;
                DefaultColor            = Color.Gray;
                AlwaysActive            = false;
                ShowTextAboveThumbs     = true;
                TextAboveThumbsColor    = Color.White;
                thumbShadowColor        = defaultShadowColor;
                ThumbShadowXOffset      = defaultShadowXOffset;
                ThumbShadowYOffset      = defaultShadowYOffset;
                _thumbShadowBlur        = defaultShadowBlur;
                ActivateOnDefaultValues = false;
                TextSizeInSp            = DefaultTextSizeInSp;
            }
            else
            {
                var a = Context.ObtainStyledAttributes(attrs, Resource.Styleable.RangeSliderControl, 0, 0);
                try
                {
                    SetRangeValues(ExtractNumericValueFromAttributes(a, Resource.Styleable.RangeSliderControl_absoluteMinValue, DefaultMinimum),
                                   ExtractNumericValueFromAttributes(a, Resource.Styleable.RangeSliderControl_absoluteMaxValue, DefaultMaximum));
                    ShowTextAboveThumbs  = a.GetBoolean(Resource.Styleable.RangeSliderControl_valuesAboveThumbs, true);
                    TextAboveThumbsColor = a.GetColor(Resource.Styleable.RangeSliderControl_textAboveThumbsColor, Color.White);
                    MinThumbHidden       = a.GetBoolean(Resource.Styleable.RangeSliderControl_minThumbHidden, false);
                    MaxThumbHidden       = a.GetBoolean(Resource.Styleable.RangeSliderControl_maxThumbHidden, false);
                    ShowLabels           = a.GetBoolean(Resource.Styleable.RangeSliderControl_showRangeLabels, true);
                    _internalPad         = a.GetDimensionPixelSize(Resource.Styleable.RangeSliderControl_internalPadding, InitialPaddingInDp);
                    _barHeight           = a.GetDimensionPixelSize(Resource.Styleable.RangeSliderControl_barHeight, LineHeightInDp);
                    ActiveColor          = a.GetColor(Resource.Styleable.RangeSliderControl_activeColor, DefaultDarkBlueColor);
                    DefaultColor         = a.GetColor(Resource.Styleable.RangeSliderControl_defaultColor, Color.Gray);
                    AlwaysActive         = a.GetBoolean(Resource.Styleable.RangeSliderControl_alwaysActive, false);
                    StepValue            = ExtractNumericValueFromAttributes(a,
                                                                             Resource.Styleable.RangeSliderControl_stepValue, DefaultStepValue);
                    StepValueContinuously = a.GetBoolean(Resource.Styleable.RangeSliderControl_stepValueContinuously,
                                                         false);

                    var normalDrawable = a.GetDrawable(Resource.Styleable.RangeSliderControl_thumbNormal);
                    if (normalDrawable != null)
                    {
                        ThumbImage = BitmapUtil.DrawableToBitmap(normalDrawable);
                    }
                    var disabledDrawable = a.GetDrawable(Resource.Styleable.RangeSliderControl_thumbDisabled);
                    if (disabledDrawable != null)
                    {
                        ThumbDisabledImage = BitmapUtil.DrawableToBitmap(disabledDrawable);
                    }
                    var pressedDrawable = a.GetDrawable(Resource.Styleable.RangeSliderControl_thumbPressed);
                    if (pressedDrawable != null)
                    {
                        ThumbPressedImage = BitmapUtil.DrawableToBitmap(pressedDrawable);
                    }
                    ThumbShadow        = a.GetBoolean(Resource.Styleable.RangeSliderControl_thumbShadow, false);
                    thumbShadowColor   = a.GetColor(Resource.Styleable.RangeSliderControl_thumbShadowColor, defaultShadowColor);
                    ThumbShadowXOffset = a.GetDimensionPixelSize(Resource.Styleable.RangeSliderControl_thumbShadowXOffset, defaultShadowXOffset);
                    ThumbShadowYOffset = a.GetDimensionPixelSize(Resource.Styleable.RangeSliderControl_thumbShadowYOffset, defaultShadowYOffset);
                    _thumbShadowBlur   = a.GetDimensionPixelSize(Resource.Styleable.RangeSliderControl_thumbShadowBlur, defaultShadowBlur);

                    ActivateOnDefaultValues = a.GetBoolean(Resource.Styleable.RangeSliderControl_activateOnDefaultValues, false);
                    TextSizeInSp            = a.GetInt(Resource.Styleable.RangeSliderControl_textSize, DefaultTextSizeInSp);
                }
                finally
                {
                    a.Recycle();
                }
            }

            if (ThumbImage == null)
            {
                var           outerCircle = Circle(DefaultLightBlueColor);
                var           innerCircle = Circle(DefaultDarkBlueColor);
                LayerDrawable ld          = new LayerDrawable(new Drawable[] { outerCircle, innerCircle });

                ld.SetLayerInset(0, 4, 4, 4, 4);
                ld.SetLayerInset(1, 23, 23, 23, 23);
                ld.SetBounds(0, 0, 64, 64);
                ThumbImage = BitmapUtil.DrawableToBitmap(ld);
            }
            if (ThumbPressedImage == null)
            {
                var outerCircle = Circle(DefaultDarkBlueColor);
                outerCircle.Paint.StrokeWidth = 4;
                outerCircle.Paint.SetStyle(Paint.Style.Stroke);
                var middleCircle = Circle(DefaultLightestBlueColor);
                var innerCircle  = Circle(DefaultDarkBlueColor);


                LayerDrawable ld = new LayerDrawable(new Drawable[] { outerCircle, middleCircle, innerCircle });
                ld.SetLayerInset(0, 4, 4, 4, 4);
                ld.SetLayerInset(1, 4, 4, 4, 4);
                ld.SetLayerInset(2, 23, 23, 23, 23);
                ld.SetBounds(0, 0, 64, 64);

                ThumbPressedImage = BitmapUtil.DrawableToBitmap(ld);
            }
            if (ThumbDisabledImage == null)
            {
                var outerCircle = Circle(DefaultGrayColor);
                var innerCircle = Circle(DefaultDarkBlueColor);

                LayerDrawable ld = new LayerDrawable(new Drawable[] { outerCircle, innerCircle });
                ld.SetLayerInset(0, 8, 8, 8, 8);
                ld.SetLayerInset(1, 28, 28, 28, 28);
                ld.SetBounds(0, 0, 64, 64);

                ThumbDisabledImage = BitmapUtil.DrawableToBitmap(ld);
            }

            _thumbHalfWidth  = 0.5f * ThumbImage.Width;
            _thumbHalfHeight = 0.5f * ThumbImage.Height;

            SetBarHeight(_barHeight);

            // make RangeSliderControl focusable. This solves focus handling issues in case EditText widgets are being used along with the RangeSliderControl within ScrollViews.
            Focusable            = true;
            FocusableInTouchMode = true;
            _scaledTouchSlop     = ViewConfiguration.Get(Context).ScaledTouchSlop;

            if (ThumbShadow)
            {
                // We need to remove hardware acceleration in order to blur the shadow
                SetLayerType(LayerType.Software, null);
                _shadowPaint.Color = thumbShadowColor;
                _shadowPaint.SetMaskFilter(new BlurMaskFilter(_thumbShadowBlur, BlurMaskFilter.Blur.Normal));
                _thumbShadowPath = new Path();
                _thumbShadowPath.AddCircle(0, 0, _thumbHalfHeight, Path.Direction.Cw);
            }
        }