xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

Gradient view issue on Mac OS Android emulator API level 28 (Pie) #3912

Open kaushalyacs opened 6 years ago

kaushalyacs commented 6 years ago

Description

Gradient view doesn't rendered properly on Mac OS Android 9 emulator (Older version of emulators work fine). But same project works as expected on Android 9 emulator on Windows.

Steps to Reproduce

I have attached a demo project.

Expected Behavior

Gradient view should be rendered only within the specified area of the view.

Actual Behavior

Gradient view rendered in entire screen as an input transparent view. User can interact with elements out side the Gradient view which are invisible. Only elements inside Gradient view are visible.

Basic Information

Screenshots

image Expected output (Windows emulator)

image Unexpected output (Mac OS emulator)

Reproduction Link

GradientViewDemo.zip

kingces95 commented 6 years ago

Reproduces as described on android macOS emulator.

kaushalyacs commented 6 years ago

Found a workaround for this. Rather than overriding DispatchDraw(Canvas canvas) set the background with: var orientation = GradientDrawable.Orientation.TopBottom; var gradient = new GradientDrawable(orientation, new[] { StartColor.ToAndroid().ToArgb(), EndColor.ToAndroid().ToArgb() }); ViewHelper.SetBackground(this, gradient)

dungbk55 commented 5 years ago

Found a workaround for this. Rather than overriding DispatchDraw(Canvas canvas) set the background with: var orientation = GradientDrawable.Orientation.TopBottom; var gradient = new GradientDrawable(orientation, new[] { StartColor.ToAndroid().ToArgb(), EndColor.ToAndroid().ToArgb() }); ViewHelper.SetBackground(this, gradient)

@kaushalyacs thanks for solution But i can't use ViewHelper. Plz help me!

yatespeter100 commented 5 years ago

@dungbk55 I recently found this issue was totally changing some of my pages and how they rendered on real devices, not just emulators. It is definitely a Android 9.0 issue.

Instead of ViewHelper, try this: ViewCompat.SetBackground(this, gradient); I set the gradient up inside the OnElementChanged method and it worked great

lewixlabs commented 5 years ago

@dungbk55 I recently found this issue was totally changing some of my pages and how they rendered on real devices, not just emulators. It is definitely a Android 9.0 issue.

Instead of ViewHelper, try this: ViewCompat.SetBackground(this, gradient); I set the gradient up inside the OnElementChanged method and it worked great

Totally agree!! šŸ„³ Hearing somebody had crash issues on old devices, I inserted a check to implement this workaround on new android P devices only

       protected override void DispatchDraw(Canvas canvas)
        {
            base.DispatchDraw(canvas);

            var boxView = (GradientBoxView)this.Element;

            // from Android 9 this is the workaround
            // https://github.com/xamarin/Xamarin.Forms/issues/3912
            if (Android.OS.Build.VERSION.SdkInt > Android.OS.BuildVersionCodes.O)
            {
                var orientation = GradientDrawable.Orientation.TopBottom;
                var gradient2 = new GradientDrawable(orientation, new[] { boxView.GradientStartColor.ToAndroid().ToArgb(), boxView.GradientEndColor.ToAndroid().ToArgb() });
                ViewCompat.SetBackground(this, gradient2);
                return;
            }

            var gradient = new Android.Graphics.LinearGradient(0, 0, 0, Height,
                boxView.GradientStartColor.ToAndroid(),
                boxView.GradientEndColor.ToAndroid(),
                Shader.TileMode.Mirror);
            var paint = new Android.Graphics.Paint()
            {
                Dither = true
            };
            paint.SetShader(gradient);
            canvas.DrawPaint(paint);
            base.DispatchDraw(canvas);
        }

Anyway @pauldipietro I hope this issue will be fixed in next Xamarin Android releases. Hi! Lewix

InquisitorJax commented 5 years ago

Thanks for this. I was getting a gradient bleed only on P in release mode (not mac vs windows related). This work-around fixed it for me :)