Beispiel #1
0
        public PinWidget(Context context, IAttributeSet attribs, int defStyleAttr) : base(context, attribs, defStyleAttr)
        {
            ShouldRedraw      = false;
            this.FillViewport = true;
            ScrollBarStyle    = ScrollbarStyles.OutsideInset;

            TypedArray array = context.ObtainStyledAttributes(attribs, Resource.Styleable.PinView);

            var method = array.GetString(Resource.Styleable.PinView_onPinCompleted);

            PinListener = new PrivatePinListener(method, this);

            DigitCount = (uint)array.GetInt(Resource.Styleable.PinView_digitCount, PinDefaults.DIGIT_COUNT);

            DisplayMetrics metrics = Resources.DisplayMetrics;

            // Digit dimensions
            DigitWidth   = array.GetDimensionPixelSize(Resource.Styleable.PinView_digitWidth, (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, PinDefaults.DIGIT_WIDTH, metrics));
            DigitHeight  = array.GetDimensionPixelSize(Resource.Styleable.PinView_digitHeight, (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, PinDefaults.DIGIT_HEIGHT, metrics));
            DigitSpacing = array.GetDimensionPixelSize(Resource.Styleable.PinView_digitSpacing, (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, PinDefaults.DIGIT_SPACING, metrics));
            TextSize     = array.GetDimensionPixelSize(Resource.Styleable.PinView_textSize, (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, PinDefaults.TEXT_SIZE, metrics));

            TypedValue resolvedColor = new TypedValue();

            Resources.Theme theme = context.Theme;

            // Colors
            theme.ResolveAttribute(Android.Resource.Attribute.TextColorPrimary, resolvedColor, true);
            TextColor = array.GetColor(Resource.Styleable.PinView_textColor,
                                       resolvedColor.ResourceId > 0 ? ContextCompat.GetColor(context, resolvedColor.ResourceId) : resolvedColor.Data);

            DigitBorderColor = array.GetColor(Resource.Styleable.PinView_borderColor, TextColor);

            resolvedColor = new TypedValue();
            theme.ResolveAttribute(Android.Resource.Attribute.ColorAccent, resolvedColor, true);
            AccentColor = array.GetColor(Resource.Styleable.PinView_accentColor,
                                         resolvedColor.ResourceId > 0 ? ContextCompat.GetColor(context, resolvedColor.ResourceId) : resolvedColor.Data);

            resolvedColor = new TypedValue();
            theme.ResolveAttribute(Android.Resource.Attribute.WindowBackground, resolvedColor, true);
            DigitBackgroundColor = array.GetColor(Resource.Styleable.PinView_digitBackgroundColor,
                                                  resolvedColor.ResourceId > 0 ? ContextCompat.GetColor(context, resolvedColor.ResourceId) : resolvedColor.Data);

            AccentHeight = (int)array.GetDimension(Resource.Styleable.PinView_accentHeight, TypedValue.ApplyDimension(ComplexUnitType.Dip, PinDefaults.ACCENT_HEIGHT, metrics));

            array.Recycle();

            _mainLayout    = new LinearLayout(Context);
            _pinInputField = new EditText(Context);

            var container = new FrameLayout(Context);

            container.AddView(_mainLayout);
            container.AddView(_pinInputField);
            this.AddView(container);

            ShouldRedraw = true;
        }
        int GetColorPrimaryDark()
        {
            FormsAppCompatActivity context = this;
            int id = global::Android.Resource.Attribute.ColorPrimaryDark;

            using (var value = new TypedValue())
            {
                try
                {
                    Resources.Theme theme = context.Theme;
                    if (theme != null && theme.ResolveAttribute(id, value, true))
                    {
                        if (value.Type >= DataType.FirstInt && value.Type <= DataType.LastInt)
                        {
                            return(value.Data);
                        }
                        if (value.Type == DataType.String)
                        {
                            return(ContextCompat.GetColor(context, value.ResourceId));
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Warning("Xamarin.Forms.Platform.Android.FormsAppCompatActivity", "Error retrieving color resource: {0}", ex);
                }

                return(-1);
            }
        }
        protected void UpdateBackgroundColor()
        {
            if (Element == null)
            {
                return;
            }

            Color backgroundColor = Element.BackgroundColor;

            if (backgroundColor.IsDefault)
            {
                if (SupportBackgroundTintList != null)
                {
                    Context context = Context;
                    int     id      = GlobalResource.Attribute.ButtonTint;
                    unchecked
                    {
                        using (var value = new TypedValue())
                        {
                            try
                            {
                                Resources.Theme theme = context.Theme;
                                if (theme != null && theme.ResolveAttribute(id, value, true))
#pragma warning disable 618
                                {
                                    SupportBackgroundTintList = Resources.GetColorStateList(value.Data);
                                }
#pragma warning restore 618
                                else
                                {
                                    SupportBackgroundTintList = new ColorStateList(ColorExtensions.States,
                                                                                   new[] { (int)0xffd7d6d6, 0x7fd7d6d6 });
                                }
                            }
                            catch (Exception ex)
                            {
                                Internals.Log.Warning("Xamarin.Forms.Platform.Android.ButtonRenderer",
                                                      "Could not retrieve button background resource: {0}", ex);
                                SupportBackgroundTintList = new ColorStateList(ColorExtensions.States,
                                                                               new[] { (int)0xffd7d6d6, 0x7fd7d6d6 });
                            }
                        }
                    }
                }
            }
            else
            {
                int intColor     = backgroundColor.ToAndroid().ToArgb();
                int disableColor = backgroundColor.MultiplyAlpha(0.5).ToAndroid().ToArgb();
                SupportBackgroundTintList = new ColorStateList(ColorExtensions.States, new[] { intColor, disableColor });
            }
        }