Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6539565
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: Android平台

2018-09-03 20:56:33

一些乐趣

正如您现在可能看到的那样,AbsoluteLayout通常用于某些特殊目的,否则就不容易了。 其中一些可能实际上被归类为“有趣”。
DotMatrixClock使用模拟的5×7点阵显示器显示当前时间的数字。 每个点都是一个BoxView,单独调整尺寸并定位在屏幕上,并根据点是打开还是关闭而着色为红色或浅灰色。 可以想象,这个时钟的点可以用嵌套的StackLayout元素或Grid组织,但每个BoxView都需要给出一个大小。 这些视图的绝对数量和规律性表明程序员比布局类更了解如何在屏幕上排列它们,因为StackLayout和Grid需要以更通用的方式执行位置计算。 因此,这是AbsoluteLayout的理想工作。
XAML文件在页面上设置一个小填充,并准备AbsoluteLayout以按代码填充:

点击(此处)折叠或打开

  1. <ContentPage xmlns=""
  2.              xmlns:x=""
  3.              x:Class="DotMatrixClock.DotMatrixClockPage"
  4.              Padding="10"
  5.              SizeChanged="OnPageSizeChanged">
  6.     <AbsoluteLayout x:Name="absoluteLayout"
  7.                     VerticalOptions="Center" />
  8.  
  9. </ContentPage>
代码隐藏文件包含几个字段,包括两个名为numberPatterns和colonPattern的数组,它们定义10位数的点阵模式和冒号分隔符:

点击(此处)折叠或打开

  1. public partial class DotMatrixClockPage : ContentPage
  2. {
  3.     // Total dots horizontally and vertically.
  4.     const int horzDots = 41;
  5.     const int vertDots = 7;
  6.     // 5 x 7 dot matrix patterns for 0 through 9.
  7.     static readonly int[,,] numberPatterns = new int[10,7,5]
  8.     {
  9.         {
  10.             { 0, 1, 1, 1, 0}, { 1, 0, 0, 0, 1}, { 1, 0, 0, 1, 1}, { 1, 0, 1, 0, 1},
  11.             { 1, 1, 0, 0, 1}, { 1, 0, 0, 0, 1}, { 0, 1, 1, 1, 0}
  12.         },
  13.         {
  14.             { 0, 0, 1, 0, 0}, { 0, 1, 1, 0, 0}, { 0, 0, 1, 0, 0}, { 0, 0, 1, 0, 0},
  15.             { 0, 0, 1, 0, 0}, { 0, 0, 1, 0, 0}, { 0, 1, 1, 1, 0}
  16.         },
  17.         {
  18.             { 0, 1, 1, 1, 0}, { 1, 0, 0, 0, 1}, { 0, 0, 0, 0, 1}, { 0, 0, 0, 1, 0},
  19.             { 0, 0, 1, 0, 0}, { 0, 1, 0, 0, 0}, { 1, 1, 1, 1, 1}
  20.         },
  21.         {
  22.             { 1, 1, 1, 1, 1}, { 0, 0, 0, 1, 0}, { 0, 0, 1, 0, 0}, { 0, 0, 0, 1, 0},
  23.             { 0, 0, 0, 0, 1}, { 1, 0, 0, 0, 1}, { 0, 1, 1, 1, 0}
  24.         },
  25.         {
  26.             { 0, 0, 0, 1, 0}, { 0, 0, 1, 1, 0}, { 0, 1, 0, 1, 0}, { 1, 0, 0, 1, 0},
  27.             { 1, 1, 1, 1, 1}, { 0, 0, 0, 1, 0}, { 0, 0, 0, 1, 0}
  28.         },
  29.         {
  30.             { 1, 1, 1, 1, 1}, { 1, 0, 0, 0, 0}, { 1, 1, 1, 1, 0}, { 0, 0, 0, 0, 1},
  31.             { 0, 0, 0, 0, 1}, { 1, 0, 0, 0, 1}, { 0, 1, 1, 1, 0}
  32.         },
  33.         {
  34.             { 0, 0, 1, 1, 0}, { 0, 1, 0, 0, 0}, { 1, 0, 0, 0, 0}, { 1, 1, 1, 1, 0},
  35.             { 1, 0, 0, 0, 1}, { 1, 0, 0, 0, 1}, { 0, 1, 1, 1, 0}
  36.         },
  37.         {
  38.             { 1, 1, 1, 1, 1}, { 0, 0, 0, 0, 1}, { 0, 0, 0, 1, 0}, { 0, 0, 1, 0, 0},
  39.             { 0, 1, 0, 0, 0}, { 0, 1, 0, 0, 0}, { 0, 1, 0, 0, 0}
  40.         },
  41.         {
  42.             { 0, 1, 1, 1, 0}, { 1, 0, 0, 0, 1}, { 1, 0, 0, 0, 1}, { 0, 1, 1, 1, 0},
  43.             { 1, 0, 0, 0, 1}, { 1, 0, 0, 0, 1}, { 0, 1, 1, 1, 0}
  44.         },
  45.         {
  46.             { 0, 1, 1, 1, 0}, { 1, 0, 0, 0, 1}, { 1, 0, 0, 0, 1}, { 0, 1, 1, 1, 1},
  47.             { 0, 0, 0, 0, 1}, { 0, 0, 0, 1, 0}, { 0, 1, 1, 0, 0}
  48.         },
  49.     };
  50.     // Dot matrix pattern for a colon.
  51.     static readonly int[,] colonPattern = new int[7, 2]
  52.     {
  53.         { 0, 0 }, { 1, 1 }, { 1, 1 }, { 0, 0 }, { 1, 1 }, { 1, 1 }, { 0, 0 }
  54.     };
  55.     // BoxView colors for on and off.
  56.     static readonly Color colorOn = Color.Red;
  57.     static readonly Color colorOff = new Color(0.5, 0.5, 0.5, 0.25);
  58.     // Box views for 6 digits, 7 rows, 5 columns.
  59.     BoxView[,,] digitBoxViews = new BoxView[6, 7, 5];
  60.     ...
  61. }
还为BoxView对象的数组定义字段,用于时间的六位数字 - 两位数字,分别用于小时,分钟和秒。水平点的总数(设置为horzDots)包括六个数字中的每一个的五个点,小时和分钟之间的冒号四个点,分钟和秒之间的冒号四个点,以及数字之间的一个点宽度除此以外。
程序的构造函数(如下所示)创建了总共238个BoxView对象并将它们添加到AbsoluteLayout,但它也为digitBoxViews数组中的数字保存了BoxView对象。 (理论上,稍后可以通过索引AbsoluteLayout的Children集合来引用BoxView对象。但是在该集合中,它们看起来只是一个线性列表。将它们存储在多维数组中可以更容易地识别和引用它们。 )所有定位和尺寸均基于AbsoluteLayout成比例,假设其长宽比为41到7,其中包含41个BoxView宽度和7个BoxView高度。

点击(此处)折叠或打开

  1. public partial class DotMatrixClockPage : ContentPage
  2. {
  3.     ...
  4.     public DotMatrixClockPage()
  5.     {
  6.         InitializeComponent();
  7.         // BoxView dot dimensions.
  8.         double height = 0.85 / vertDots;
  9.         double width = 0.85 / horzDots;
  10.         // Create and assemble the BoxViews.
  11.         double xIncrement = 1.0 / (horzDots - 1);
  12.         double yIncrement = 1.0 / (vertDots - 1);
  13.         double x = 0;
  14.         for (int digit = 0; digit < 6; digit++)
  15.         {
  16.             for (int col = 0; col < 5; col++)
  17.             {
  18.                 double y = 0;
  19.                 for (int row = 0; row < 7; row++)
  20.                 {
  21.                     // Create the digit BoxView and add to layout.
  22.                     BoxView boxView = new BoxView();
  23.                     digitBoxViews[digit, row, col] = boxView;
  24.                     absoluteLayout.Children.Add(boxView,
  25.                                                 new Rectangle(x, y, width, height),
  26.                                                 AbsoluteLayoutFlags.All);
  27.                      y += yIncrement;
  28.                 }
  29.             x += xIncrement;
  30.         }
  31.         x += xIncrement;
  32.         // Colons between the hour, minutes, and seconds.
  33.         if (digit == 1 || digit == 3)
  34.         {
  35.             int colon = digit / 2;
  36.             for (int col = 0; col < 2; col++)
  37.             {
  38.                 double y = 0;
  39.                 for (int row = 0; row < 7; row++)
  40.                 {
  41.                     // Create the BoxView and set the color.
  42.                     BoxView boxView = new BoxView
  43.                         {
  44.                              Color = colonPattern[row, col] == 1 ?
  45.                                         colorOn : colorOff
  46.                         };
  47.                         absoluteLayout.Children.Add(boxView,
  48.                                                     new Rectangle(x, y, width, height),
  49.                                                     AbsoluteLayoutFlags.All);
  50.                         y += yIncrement;
  51.                     }
  52.                     x += xIncrement;
  53.                 }
  54.                 x += xIncrement;
  55.             }
  56.         }
  57.         // Set the timer and initialize with a manual call.
  58.         Device.StartTimer(TimeSpan.FromSeconds(1), OnTimer);
  59.         OnTimer();
  60.     }
  61.     ...
  62. }
你会记得,horzDots和vertDots常量分别设置为41和7。 要填充AbsoluteLayout,每个BoxView需要占用宽度的一小部分,等于1 / horzDots,高度的一小部分等于1 / vertDots。 设置到每个BoxView的高度和宽度是该值的85%,以便将点分开,以便它们不会相互碰撞:

点击(此处)折叠或打开

  1. double height = 0.85 / vertDots;
  2. double width = 0.85 / horzDots;
要定位每个BoxView,构造函数计算比例xIncrement和yIncrement值,如下所示:

点击(此处)折叠或打开

  1. double xIncrement = 1.0 / (horzDots - 1);
  2. double yIncrement = 1.0 / (vertDots - 1);
这里的分母是40和6,因此最终的X和Y位置坐标是1的值。
时间数字的BoxView对象在构造函数中根本没有着色,但是两个冒号的BoxView对象基于colonPattern数组被赋予Color属性。 DotMatrixClockPage构造函数以一秒钟的计时器结束。
页面的SizeChanged处理程序是从XAML文件设置的。 AbsoluteLayout会自动水平拉伸以填充页面的宽度(减去填充),因此HeightRequest实际上只设置了宽高比:

点击(此处)折叠或打开

  1. public partial class DotMatrixClockPage : ContentPage
  2. {
  3.     ...
  4.     void OnPageSizeChanged(object sender, EventArgs args)
  5.     {
  6.         // No chance a display will have an aspect ratio > 41:7
  7.         absoluteLayout.HeightRequest = vertDots * Width / horzDots;
  8.     }
  9.     ...
  10. }
似乎Device.StartTimer事件处理程序应该相当复杂,因为它负责根据当前时间的数字设置每个BoxView的Color属性。 但是,numberPatterns数组和digitBoxViews数组的定义之间的相似性令人惊讶地直截了当:

点击(此处)折叠或打开

  1. public partial class DotMatrixClockPage : ContentPage
  2. {
  3.     ...
  4.  
  5.     bool OnTimer()
  6.     {
  7.         DateTime dateTime = DateTime.Now;
  8.         // Convert 24-hour clock to 12-hour clock.
  9.         int hour = (dateTime.Hour + 11) % 12 + 1;
  10.         // Set the dot colors for each digit separately.
  11.         SetDotMatrix(0, hour / 10);
  12.         SetDotMatrix(1, hour % 10);
  13.         SetDotMatrix(2, dateTime.Minute / 10);
  14.         SetDotMatrix(3, dateTime.Minute % 10);
  15.         SetDotMatrix(4, dateTime.Second / 10);
  16.         SetDotMatrix(5, dateTime.Second % 10);
  17.         return true;
  18.     }
  19.     void SetDotMatrix(int index, int digit)
  20.     {
  21.         for (int row = 0; row < 7; row++)
  22.         for (int col = 0; col < 5; col++)
  23.         {
  24.             bool isOn = numberPatterns[digit, row, col] == 1;
  25.             Color color = isOn ? colorOn : colorOff;
  26.             digitBoxViews[index, row, col].Color = color;
  27.         }
  28.     }
  29. }
这是结果:
2018_09_03_144922
当然,越大越好,所以你可能想要将手机(或书)侧身转向足够大的东西,从房间的另一端读取:
2018_09_03_145007
适用于AbsoluteLayout的另一种特殊类型的应用是动画。 BouncingText程序使用其XAML文件来实例化两个Label元素:

点击(此处)折叠或打开

  1. <ContentPage xmlns=""
  2.              xmlns:x=""
  3.              x:Class="BouncingText.BouncingTextPage">
  4.     <AbsoluteLayout>
  5.         <Label x:Name="label1"
  6.                Text="BOUNCE"
  7.                FontSize="Large"
  8.                AbsoluteLayout.LayoutFlags="PositionProportional" />
  9.         <Label x:Name="label2"
  10.                Text="BOUNCE"
  11.                FontSize="Large"
  12.                AbsoluteLayout.LayoutFlags="PositionProportional" />
  13.  
  14.     </AbsoluteLayout>
  15. </ContentPage>
请注意,AbsoluteLayout.LayoutFlags属性设置为PositionProportional。 Label会计算自己的大小,但定位是成比例的。 介于0和1之间的值可以将两个Label元素放置在页面内的任何位置。
代码隐藏文件以15毫秒的持续时间启动计时器。 这相当于每秒约60个滴答,这通常是视频显示的刷新率。 15毫秒的计时器持续时间是执行动画的理想选择:

点击(此处)折叠或打开

  1. public partial class BouncingTextPage : ContentPage
  2. {
  3.     const double period = 2000; // in milliseconds
  4.     readonly DateTime startTime = DateTime.Now;
  5.     public BouncingTextPage()
  6.     {
  7.         InitializeComponent();
  8.         Device.StartTimer(TimeSpan.FromMilliseconds(15), OnTimerTick);
  9.     }
  10.     bool OnTimerTick()
  11.     {
  12.         TimeSpan elapsed = DateTime.Now - startTime;
  13.         double t = (elapsed.TotalMilliseconds % period) / period; // 0 to 1
  14.         t = 2 * (t < 0.5 ? t : 1 - t); // 0 to 1 to 0
  15.         AbsoluteLayout.SetLayoutBounds(label1,
  16.             new Rectangle(t, 0.5, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize));
  17.         AbsoluteLayout.SetLayoutBounds(label2,
  18.             new Rectangle(0.5, 1 - t, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize));
  19.         return true;
  20.     }
  21. }
OnTimerTick处理程序计算自程序启动以来经过的时间,并将其转换为每两秒从0到1的值t(对于时间)。 t的第二次计算使其从0增加到1,然后每两秒减少回0。 该值直接传递给两个AbsoluteLayout.SetLayoutBounds调用中的Rectangle构造函数。 结果是第一个标签水平移动穿过屏幕中心,似乎从左侧和右侧反弹。 第二个标签垂直上下移动到屏幕中心,似乎从顶部和底部反弹:
2018_09_03_145355
Windows 10 Mobile截图确认,两个Label视图每秒都会在中心短暂相遇。
从现在开始,我们的Xamarin.Forms应用程序的页面将变得更加活跃,动画和动态。 在下一章中,您将看到Xamarin.Forms的交互式视图如何在用户和应用程序之间建立通信方式。
阅读(3538) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~