Xamarin Forms:如何修改自定义动画页面渲染器的背景颜色?
问题描述
我实现了一个自定义页面渲染器,用于创建页面导航时从右到左的滑动动画,但遇到一个问题:动画的背景颜色会根据Android的深色/浅色主题设置自动变为黑色或白色,我希望动画背景颜色能和页面实际的背景颜色保持一致。另外,Animation.BackroundColor = Color.Red这种设置方式已经被标记为过时且废弃。
当前使用的自定义渲染器代码如下:
[assembly: ExportRenderer(typeof(ContentPage), typeof(CustomPageRenderer))] namespace uCue_Game.Droid { public class CustomPageRenderer : PageRenderer { public CustomPageRenderer(Context context) : base(context) { } protected override void OnAttachedToWindow() { var myValue = Preferences.Get("ModelAnimation", false); if (myValue) { var metrics = Resources.DisplayMetrics; Android.Views.Animations.Animation translateAnimation = new TranslateAnimation(metrics.HeightPixels, 0, 0, 0) { Duration = 1500 }; Animation = translateAnimation; translateAnimation.Start(); } base.OnAttachedToWindow(); } } }
启用动画的代码:
Preferences.Set("ModelAnimation", true);
复现步骤:
- 将上述渲染器代码添加到
*.Android项目中作为类文件 - 创建两个空白ContentPage
- 在执行
PushAsync前调用Preferences.Set("ModelAnimation", true)启用动画 - 切换页面时即可看到黑/白色的动画背景
解决方案
因为Animation.BackgroundColor已被废弃,可通过以下两种方式解决动画背景与页面背景不一致的问题:
方法一:绑定页面背景色到渲染器
直接获取ContentPage的背景色,转换为Android平台颜色后设置给PageRenderer,动画执行时的背景会自动匹配页面背景:
[assembly: ExportRenderer(typeof(ContentPage), typeof(CustomPageRenderer))] namespace uCue_Game.Droid { public class CustomPageRenderer : PageRenderer { public CustomPageRenderer(Context context) : base(context) { } protected override void OnElementChanged(ElementChangedEventArgs<Page> e) { base.OnElementChanged(e); if (e.NewElement != null) { var page = (ContentPage)e.NewElement; if (page.BackgroundColor != Xamarin.Forms.Color.Default) { SetBackgroundColor(page.BackgroundColor.ToAndroid()); } } } protected override void OnAttachedToWindow() { var myValue = Preferences.Get("ModelAnimation", false); if (myValue) { var metrics = Resources.DisplayMetrics; Android.Views.Animations.Animation translateAnimation = new TranslateAnimation(metrics.HeightPixels, 0, 0, 0) { Duration = 1500 }; Animation = translateAnimation; translateAnimation.Start(); } base.OnAttachedToWindow(); } } }
方法二:自定义动画背景视图
如果页面背景是渐变、图片等复杂样式,可创建一个与页面背景完全一致的视图,将动画应用到该视图上:
[assembly: ExportRenderer(typeof(ContentPage), typeof(CustomPageRenderer))] namespace uCue_Game.Droid { public class CustomPageRenderer : PageRenderer { private View _animationBackgroundView; public CustomPageRenderer(Context context) : base(context) { } protected override void OnElementChanged(ElementChangedEventArgs<Page> e) { base.OnElementChanged(e); if (e.NewElement != null) { _animationBackgroundView = new View(Context); var page = (ContentPage)e.NewElement; // 匹配页面纯色背景 if (page.BackgroundColor != Xamarin.Forms.Color.Default) { _animationBackgroundView.SetBackgroundColor(page.BackgroundColor.ToAndroid()); } // 若页面有背景图/渐变,需在此处对应实现样式复制逻辑 AddView(_animationBackgroundView); } } protected override void OnLayout(bool changed, int l, int t, int r, int b) { base.OnLayout(changed, l, t, r, b); if (_animationBackgroundView != null) { _animationBackgroundView.Layout(l, t, r, b); } } protected override void OnAttachedToWindow() { var myValue = Preferences.Get("ModelAnimation", false); if (myValue && _animationBackgroundView != null) { var metrics = Resources.DisplayMetrics; Android.Views.Animations.Animation translateAnimation = new TranslateAnimation(metrics.HeightPixels, 0, 0, 0) { Duration = 1500 }; _animationBackgroundView.StartAnimation(translateAnimation); } base.OnAttachedToWindow(); } } }
说明:方法一适合纯色背景页面,实现简单;方法二适配复杂背景场景,能精准匹配页面视觉效果。
内容的提问来源于stack exchange,提问作者ThisQRequiresASpecialist
相关产品推荐
相关产品推荐

