第二十七章:自定义渲染器(四)

简介: 渲染器和属性(2)现在,对于iOS,EllipseUIView类是存在的,可以使用EllipseUIView作为本机控件来编写EllipseViewRenderer。 从结构上讲,这个类几乎与Windows渲染器相同:using System.

渲染器和属性(2)

现在,对于iOS,EllipseUIView类是存在的,可以使用EllipseUIView作为本机控件来编写EllipseViewRenderer。 从结构上讲,这个类几乎与Windows渲染器相同:

using System.ComponentModel;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(Xamarin.FormsBook.Platform.EllipseView), 
                                 typeof(Xamarin.FormsBook.Platform.iOS.EllipseViewRenderer))]
namespace Xamarin.FormsBook.Platform.iOS
{
    public class EllipseViewRenderer : ViewRenderer<EllipseView, EllipseUIView>
    {
        protected override void OnElementChanged(ElementChangedEventArgs<EllipseView> args)
        {
            base.OnElementChanged(args);
            if (Control == null)
            {
                SetNativeControl(new EllipseUIView());
            }
            if (args.NewElement != null)
            {
                SetColor();
            }
        }
        protected override void OnElementPropertyChanged(object sender,
                                                         PropertyChangedEventArgs args)
        {
            base.OnElementPropertyChanged(sender, args);
            if (args.PropertyName == EllipseView.ColorProperty.PropertyName)
            {
                SetColor();
            }
        }
        void SetColor()
        {
            if (Element.Color != Color.Default)
            {
                Control.SetColor(Element.Color.ToUIColor());
            }
            else
            {
                Control.SetColor(UIColor.Clear);
            }
        }
    }
}

此渲染器和Windows版本之间唯一的真正区别是Control属性设置为ColorUIView的实例,底部的SetColor方法的主体是不同的。 它现在调用ColorUIView中的SetColor方法。 这个SetColor方法也可以在名为ToUIColor的Xamarin.Forms.Platform.iOS库中使用公共扩展方法
将Xamarin.Forms颜色转换为iOS颜色。
您可能已经注意到,Windows渲染器和iOS渲染器都不必担心调整大小。 您很快就会看到,EllipseView可以设置为各种大小,并且在Xamarin.Forms布局系统中计算的大小将变为本机控件的大小。
遗憾的是,这不是Android渲染器的情况。 Android渲染器需要一些大小调整逻辑。 与iOS一样,Android也缺少呈现椭圆的本机控件。 因此,Xamarin.FormsBook.Platform.Android库包含一个名为EllipseDrawableView的类,它从View派生并绘制一个椭圆:

using Android.Content;
using Android.Views;
using Android.Graphics.Drawables;
using Android.Graphics.Drawables.Shapes;
using Android.Graphics;
namespace Xamarin.FormsBook.Platform.Android
{
    public class EllipseDrawableView : View
    {
        ShapeDrawable drawable;
        public EllipseDrawableView(Context context) : base(context)
        {
            drawable = new ShapeDrawable(new OvalShape());
        }
        protected override void OnDraw(Canvas canvas)
        {
            base.OnDraw(canvas);
            drawable.Draw(canvas);
        }
        public void SetColor(Xamarin.Forms.Color color)
        {
            drawable.Paint.SetARGB((int)(255 * color.A),
                                   (int)(255 * color.R),
                                   (int)(255 * color.G),
                                   (int)(255 * color.B));
            Invalidate();
        }
        public void SetSize(double width, double height)
        {
            float pixelsPerDip = Resources.DisplayMetrics.Density;
            drawable.SetBounds(0, 0, (int)(width * pixelsPerDip),
                                     (int)(height * pixelsPerDip));
            Invalidate();
        }
    }
}

在结构上,这类似于为iOS定义的EllipseUIView类,除了构造函数为椭圆创建一个ShapeDrawable对象,并且OnDraw覆盖渲染它。
此类有两种方法来设置此椭圆的属性。 SetColor方法转换Xamarin.Forms颜色以设置ShapeDrawable对象的Paint属性,SetSize方法将设备无关单位的大小转换为像素,用于设置ShapeDrawable对象的边界。 SetColor和SetSize都以对Invalidate的调用结束,以使绘图表面无效并生成对OnDraw的另一个调用。
Android渲染器使用EllipseDrawableView类作为其本机对象:

using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(Xamarin.FormsBook.Platform.EllipseView), 
                          typeof(Xamarin.FormsBook.Platform.Android.EllipseViewRenderer))]
namespace Xamarin.FormsBook.Platform.Android
{
    public class EllipseViewRenderer : ViewRenderer<EllipseView, EllipseDrawableView>
    {
        double width, height;
        protected override void OnElementChanged(ElementChangedEventArgs<EllipseView> args)
        {
            base.OnElementChanged(args);
            if (Control == null)
            {
                SetNativeControl(new EllipseDrawableView(Context));
            }
            if (args.NewElement != null)
            {
                SetColor();
                SetSize();
            }
        }
        protected override void OnElementPropertyChanged(object sender,
                                                         PropertyChangedEventArgs args)
        {
            base.OnElementPropertyChanged(sender, args);
            if (args.PropertyName == VisualElement.WidthProperty.PropertyName)
            {
                width = Element.Width;
                SetSize();
            }
            else if (args.PropertyName == VisualElement.HeightProperty.PropertyName)
            {
                height = Element.Height;
                SetSize();
            }
            else if (args.PropertyName == EllipseView.ColorProperty.PropertyName)
            {
                SetColor();
            }
        }
        void SetColor()
        {
            Control.SetColor(Element.Color);
        }
        void SetSize()
        {
            Control.SetSize(width, height);
        }
    }
}

请注意,OnElementPropertyChanged方法需要检查Width和Height属性的更改并将它们保存在字段中,以便可以将它们组合到SetSize调用EllipseDrawableView的单个Bounds设置中。
所有渲染器到位后,是时候看它是否有效。 EllipseDemo解决方案还包含指向Xamarin.FormsBook.Platform解决方案的各个项目的链接,EllipseDemo中的每个项目都包含对Xamarin.FormsBook.Platform中相应库项目的引用。
EllipseDemo中的每个项目还包含对相应库项目中的Toolkit.Init方法的调用。这并不总是必要的。但请记住,各种渲染器不会被任何项目中的任何代码直接引用,并且某些优化可能导致代码在运行时无法使用。对Toolkit.Init的调用避免了这种情况。
EllipseDemo中的XAML文件创建了几个具有不同颜色和大小的EllipseView对象,其中一些受到大小限制,而其他对象则允许填充其容器:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:platform=
                 "clr-namespace:Xamarin.FormsBook.Platform;assembly=Xamarin.FormsBook.Platform"
             x:Class="EllipseDemo.EllipseDemoPage">
    <Grid>
        <platform:EllipseView Color="Aqua" />
        <StackLayout>
            <StackLayout.Padding>
                <OnPlatform x:TypeArguments="Thickness"
                            iOS="0, 20, 0, 0" />
            </StackLayout.Padding>
            <platform:EllipseView Color="Red"
                                  WidthRequest="40"
                                  HeightRequest="80"
                                  HorizontalOptions="Center" />
            <platform:EllipseView Color="Green"
                                  WidthRequest="160"
                                  HeightRequest="80"
                                  HorizontalOptions="Start" />
            <platform:EllipseView Color="Blue"
                                  WidthRequest="160"
                                  HeightRequest="80"
                                  HorizontalOptions="End" />
            <platform:EllipseView Color="#80FF0000"
                                  HorizontalOptions="Center" />
            <ContentView Padding="50"
                         VerticalOptions="FillAndExpand">
                <platform:EllipseView Color="Red"
                                      BackgroundColor="#80FF0000" />

            </ContentView>
        </StackLayout>
    </Grid>
</ContentPage>

请特别注意倒数第二个EllipseView,它给自己一个半透明的红色。 对于填充页面的大椭圆的Aqua,这应该呈现为中灰色。
最后一个EllipseView为自己提供半透明红色的BackgroundColor设置。 同样,这应该在大的Aqua椭圆上呈灰色,但在白色背景下呈浅红色,在黑色背景下呈暗红色。 他们来了:
2019_06_11_093306
一旦你有一个EllipseView,当然你会想要写一个弹跳球程序。 BouncingBall解决方案还包含指向Xamarin.FormsBook.Platform解决方案中所有项目的链接,并且所有应用程序项目都引用了相应的库项目。 BouncingBall PCL还引用了一个名为Vector2的结构的Xamarin.FormsBook.Toolkit库,这是一个二维向量。
XAML文件将EllipseView定位在页面的中心:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:platform=
                 "clr-namespace:Xamarin.FormsBook.Platform;assembly=Xamarin.FormsBook.Platform"
             x:Class="BouncingBall.BouncingBallPage">
    <platform:EllipseView x:Name="ball"
                          WidthRequest="100"
                          HeightRequest="100"
                          HorizontalOptions="Center"
                          VerticalOptions="Center" />

</ContentPage>

代码隐藏文件启动了两个“永远”运行的动画。第一个动画在构造函数中定义,并动画弹跳球的Color属性,每隔10秒通过彩虹的颜色。
第二个动画在屏幕的四个“墙壁”上弹回球。 对于通过while循环的每个循环,代码首先确定它将首先击中哪个墙以及以与设备无关的单位到该墙的距离。 朝向while循环结束的中心的新计算是球撞击墙壁时的位置。 新的矢量计算基于现有矢量和垂直于其击中的表面的矢量(称为法向矢量)确定偏转矢量:

public partial class BouncingBallPage : ContentPage
{
    public BouncingBallPage()
    {
        InitializeComponent();
        // Color animation: cycle through rainbow every 10 seconds.
        new Animation(callback: v => ball.Color = Color.FromHsla(v, 1, 0.5),
        start: 0,
       end: 1
       ).Commit(owner: this,
        name: "ColorAnimation",
        length: 10000,
        repeat: () => true);
        BounceAnimationLoop();
    }
    async void BounceAnimationLoop()
    {
        // Wait until the dimensions are good.
        while (Width == -1 && Height == -1)
        {
            await Task.Delay(100);
        }
        // Initialize points and vectors.
        Point center = new Point();
        Random rand = new Random();
        Vector2 vector = new Vector2(rand.NextDouble(), rand.NextDouble());
        vector = vector.Normalized;
        Vector2[] walls = { new Vector2(1, 0), new Vector2(0, 1), // left, top
                            new Vector2(-1, 0), new Vector2(0, -1) }; // right, bottom
        while (true)
        {
            // The locations of the four "walls" (taking ball size into account).
            double right = Width / 2 - ball.Width / 2;
            double left = -right;
            double bottom = Height / 2 - ball.Height / 2;
            double top = -bottom;
            // Find the number of steps till a wall is hit.
            double nX = Math.Abs(((vector.X > 0 ? right : left) - center.X) / vector.X);
            double nY = Math.Abs(((vector.Y > 0 ? bottom : top) - center.Y) / vector.Y);
            double n = Math.Min(nX, nY);
            // Find the wall that's being hit.
            Vector2 wall = walls[nX < nY ? (vector.X > 0 ? 2 : 0) : (vector.Y > 0 ? 3 : 1)];
            // New center and vector after animation.
            center += n * vector;
            vector -= 2 * Vector2.DotProduct(vector, wall) * wall;
            // Animate at 3 msec per unit.
            await ball.TranslateTo(center.X, center.Y, (uint)(3 * n));
        }
    }
}

当然,静态照片无法捕捉到动画的激动人心的动作:
2019_06_11_093633

目录
相关文章
|
8月前
|
域名解析 搜索推荐 网络协议
一级域名与二级域名的区别 功能及优缺点全解析
本文全面解析一级域名与二级域名的区别,详细介绍二者在所有权、管理方式、品牌价值、SEO权重等方面的差异,分析各自功能及优缺点,并给出实用的域名规划建议,同时提供专业的二级域名租用与管理解决方案,助力个人与企业合理选择域名。
8320 12
一级域名与二级域名的区别 功能及优缺点全解析
|
3月前
|
弹性计算 固态存储
阿里云3台爆款云服务器配置指南:38元/99元/199元、带宽与购买门槛全梳理
2026年阿里云三款爆款云服务器:新用户秒杀轻量机2核2G仅38元/年(200M带宽);新老同享ECS经济型2核2G 99元/年(续费同价);企业专享u1实例2核4G 199元/年(5M带宽,需企业认证)。
|
3月前
|
人工智能 自然语言处理 API
阿里云Token Plan个人版与企业版支持模型、优惠价格对比与选购指南:新手选择必看
阿里云千问AI平台的Token Plan订阅服务正在进行限时优惠活动(活动期:2026年7月20日—9月30日),个人版三档套餐低至39元/月,团队版标准坐席低至150元/席位/月。面对"个人版"和"团队版(企业版)"两条产品线、六个套餐档位、30余款可选模型,很多新手用户第一次接触时都会陷入选择困难:我到底该买哪个版本?哪个档位?模型够用吗?价格划算吗?本文将基于官方文档,从产品特性、支持模型、价格对比、活动规则、省钱技巧五个维度,为你做一次系统性的梳理,帮助你快速做出最适合自己的选择。
阿里云Token Plan个人版与企业版支持模型、优惠价格对比与选购指南:新手选择必看
|
3月前
|
人工智能 搜索推荐 定位技术
2026年9月28号麟哥|工厂制造业短视频抖音获客精准流量落地 31 个核心方法?
麟哥31法聚焦抖音搜索长尾流量,以询盘留资为核心,涵盖账号基建、关键词布局、5W1H短视频生产、AI提效及私域承接SOP,专为制造业、建材、工程等B端实体设计,零基础可落地,自然流精准获客。(239字)
|
4月前
|
运维 安全 Linux
阿里云CLI新版完全指南:插件化架构、安装配置与自动化运维实战与排错全手册
在云资源运维与自动化工作流当中,命令行工具凭借执行高效、可脚本化、便于流水线集成的优势,是运维工程师、开发人员必不可少的工具。阿里云CLI作为官方统一命令行工具,经过重大版本迭代,3.3.0及以上版本全面启用**插件化架构**,解决旧版CLI程序包体积臃肿、更新繁琐、各产品命令风格不统一的痛点。本文从底层架构特性、多平台安装部署、身份凭证配置、插件生命周期管理、命令基础用法、高级能力、真实业务实战、故障排查多个维度完整讲解新版阿里云CLI,附带大量可直接复制运行的命令脚本,帮助使用者快速上手,完成云资源批量管理与自动化运维落地。
461 1
|
10月前
|
机器学习/深度学习 人工智能 自然语言处理
微调与推理:解锁大模型定制化能力的核心密钥
本文深入浅出讲解大模型微调与推理的核心原理与实践:微调(如LoRA)让通用模型适配垂直场景,推理则实现价值落地。以LLaMA为例,手把手演示数据准备、平台微调、在线测试与效果评估,零代码门槛,助初学者快速打造专属AI模型。(239字)
|
10月前
|
传感器 算法 安全
【无人机控制】基于S函数和Simulink的6自由度无人机飞行模拟与控制附matlab代码
✅作者简介:热爱科研的Matlab仿真开发者,擅长数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。 🍎 往期回顾关注个人主页:Matlab科研工作室 👇 关注我领取海量matlab电子书和数学建模资料 🍊个人信条:格物致知,完整Matlab代码获取及仿真咨询内容私信。 🔥 内容介绍 在科技飞速发展的当下,无人机技术凭借其独特优势,在众多领域得到了广泛应用。从农业植保、物流配送,到影视拍摄、测绘勘探,无人机的身影无处不在,为各行业带来了全新的发展机遇与变革。其灵活、高效、低成本的特点,使其成为解决许多复杂任务的得力工具。 然而,无人机飞行控制绝非易事。在三维
390 2
|
4月前
|
人工智能 自然语言处理 开发者
Token Plan个人版Pro套餐499元/月,适合什么人用?额度有多大?
阿里云百炼Token Plan个人版Pro套餐499元/月(原价600元),7天40,000 Credits,支持6-8个Agent并发,专为高频使用AI编程/智能体工具的重度个人开发者设计。阿里云Token Plan官网:https://t.aliyun.com/U/EsRjVx
|
6月前
|
监控 Java Sentinel
【Spring全家桶】Spring Cloud 2023.0.x:熔断降级限流:Resilience4j、Sentine(附《思维导图》+《面试高频考点清单》)
本文系统梳理Spring Cloud 2023.0.x(Leyton)熔断、降级、限流核心知识,深度对比Resilience4j(轻量函数式、官方推荐)与Sentinel(阿里系、全链路流量治理),涵盖原理、配置、集成及生产最佳实践,助力高可用微服务架构落地。

热门文章

最新文章