表示时间
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
表盘的主要任务是显示时间,借助表盘格式,您可以使用 AnalogClock
和 DigitalClock
元素创建模拟和数字时钟。
数字时钟
您可以在表盘 Scene
中使用 DigitalClock
和 TimeText
元素定义一个基本的数字时钟:
<DigitalClock x="100" y="100" width="200" height="50">
<TimeText x="0" y="0" width="200" height="50" format="hh:mm">
<Font family="SYNC_TO_DEVICE" size="16" />
</TimeText>
</DigitalClock>
借助 format
属性,您可以控制使用时、分、秒的组合来表示时间的方式。具体选项因所使用的表盘格式版本而异;版本 2 会扩展此处的选项。
在前面的示例中,Font 已设置为使用系统字体。
显示世界其他地点的时间(例如世界时钟)也很有用。这可以通过 Localization
元素实现:
<DigitalClock x="100" y="100" width="200" height="50">
<Localization timeZone="Europe/London" />
...
</DigitalClock>
指针式时钟
如需创建模拟时钟,请使用 AnalogClock
元素。每个 HourHand, MinuteHand
和 SecondHand
子元素都指定了应在渲染中使用的资源:
<AnalogClock x="0" y="0" width="200" height="200">
<HourHand resource="hour_hand" ... />
<MinuteHand resource="minute_hand" />
<SecondHand resource="second_hand" />
<!-- Optionally add a Tick or Sweep child element to define SecondHand behavior -->
</AnalogClock>
指定轴心点
随着时间的推移,小时、分钟和秒资源都会旋转,但请务必指定每个资源应围绕的正确点。
在某些情况下,轴心点并不完全位于手掌底部,而是水平居中。应将其指定为 <HourHand …
pivotY="(pivot_ratio)" />
,其中:
$$
pivot\_ratio = \frac{pivot\_height}{full\_height}
$$
为指针着色
为了让用户能够自定义表盘的外观,通常会为指针提供可配置的颜色。
实现此目的的方法是,通过每只手上的 tintColor
分别为每只手着色,或通过 AnalogClock
为所有手着上相同的颜色。
除了允许用户为特定颜色着色之外,您还可以向 tintColor
提供配置选项,以允许用户选择:
<AnalogClock ... tintColor="[CONFIGURATION.handColors.0]" />
阴影
为了实现逼真的指针效果,可以在每个指针后面使用阴影,以营造深度感。为此,请使用每种手型各两个,将其中一个手型置于另一个手型后面,并为阴影提供单独的资源:
<!-- Offset the shadow by 5 units in both directions -->
<HourHand resource="hour_hand_shadow" ... x="105" y="105">
<!-- ... -->
<HourHand resource="hour_hand" ... x="100" y="100">
面部装饰
模拟表盘通常在表盘周围有装饰,用于显示小时或分钟。为此,您可以采用以下两种方法:
添加全屏背景图片,其中包含预先绘制的表盘。请参阅使用图片。
<PartImage x="0" y="0" width="450" height="450" ...>
<Image resource="watch_face_dial" />
</PartImage>
绘制单独的装饰,并使用旋转将其放置在脸部周围。
<Group ... >
<!-- Content for the "12" marker -->
</Group>
<Group pivotX="..." pivotY="..." angle="30">
<!-- Content for the "1" marker -->
</Group>
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Represent the time\n\nThe primary job of a watch face is to show the time, and the Watch Face Format\nlets you create both analog and digital clocks using the\n[`AnalogClock`](/training/wearables/wff/clock/analog-clock) and [`DigitalClock`](/training/wearables/wff/clock/digital-clock) elements.\n\n### Digital clocks\n\nA basic digital clock can be defined using the `DigitalClock` and\n[`TimeText`](/training/wearables/wff/clock/time-text) element within your watch face `Scene`: \n\n \u003cDigitalClock x=\"100\" y=\"100\" width=\"200\" height=\"50\"\u003e\n \u003cTimeText x=\"0\" y=\"0\" width=\"200\" height=\"50\" format=\"hh:mm\"\u003e\n \u003cFont family=\"SYNC_TO_DEVICE\" size=\"16\" /\u003e\n \u003c/TimeText\u003e\n \u003c/DigitalClock\u003e\n\nThe `format` attribute lets you control how the time is represented using\na combination of hours, minutes, and seconds. The exact options differ depending\non the version of Watch Face Format being used; version 2 expands the options\nhere.\n\nIn the preceding example, [Font](/training/wearables/wff/group/part/text/font) is set to use the system font.\n\nIt can also be useful to show the time for other locations in the world, for\nexample a *world clock* . This can be achieved through the [`Localization`](/training/wearables/wff/common/localization)\nelement: \n\n \u003cDigitalClock x=\"100\" y=\"100\" width=\"200\" height=\"50\"\u003e\n \u003cLocalization timeZone=\"Europe/London\" /\u003e\n ...\n \u003c/DigitalClock\u003e\n\n### Analog clocks\n\nTo create an analog clock, use the [`AnalogClock`](/training/wearables/wff/clock/analog-clock) element. Each of the\n`HourHand, MinuteHand`, and `SecondHand` child elements specifies the resource\nthat should be used in the rendering: \n\n \u003cAnalogClock x=\"0\" y=\"0\" width=\"200\" height=\"200\"\u003e\n \u003cHourHand resource=\"hour_hand\" ... /\u003e\n \u003cMinuteHand resource=\"minute_hand\" /\u003e\n \u003cSecondHand resource=\"second_hand\" /\u003e\n \u003c!-- Optionally add a Tick or Sweep child element to define SecondHand behavior --\u003e\n \u003c/AnalogClock\u003e\n\n#### Specify the pivot point\n\nEach of the hour, minute, and second resources are rotated as time advances,\nbut it is important to specify the correct point around which each should pivot.\n\nIn some situations, the pivot point is not quite at the bottom of the hand, and\nis centered horizontally. This should be specified as `\u003cHourHand ...\npivotY=\"(pivot_ratio)\" /\u003e` where: \n$$ pivot\\\\_ratio = \\\\frac{pivot\\\\_height}{full\\\\_height} $$\n\n#### Color the watch hands\n\nTo allow the user to customize the appearance of the watch face, it is common to\nhave a configurable color for the watch hands.\n\nA way to achieve this is through the `tintColor` on each of the hands to\nseparately tint each hand, or on `AnalogClock`, to tint all hands the same\ncolor.\n\nIn addition to enabling users to tint specific colors, you can supply a\nconfiguration option to `tintColor` to allow the user to choose: \n\n \u003cAnalogClock ... tintColor=\"[CONFIGURATION.handColors.0]\" /\u003e\n\n#### Drop shadow\n\nFor a realistic watch hand effect, using a drop shadow behind each hand can give\nthe appearance of depth. To achieve this, use two of each hand type, offsetting\none behind the other, supplying a separate resource for the shadow: \n\n \u003c!-- Offset the shadow by 5 units in both directions --\u003e\n \u003cHourHand resource=\"hour_hand_shadow\" ... x=\"105\" y=\"105\"\u003e\n \u003c!-- ... --\u003e\n \u003cHourHand resource=\"hour_hand\" ... x=\"100\" y=\"100\"\u003e\n\n#### Face decorations\n\nAnalog watch faces often have decorations around the face showing the hours or\nminutes. To achieve this, there are two approaches:\n\n1. Include a full-screen background image, which contains your predrawn watch\n face. [See working with images](/training/wearables/wff/images).\n\n \u003cPartImage x=\"0\" y=\"0\" width=\"450\" height=\"450\" ...\u003e\n \u003cImage resource=\"watch_face_dial\" /\u003e\n \u003c/PartImage\u003e\n\n2. Draw separate decorations and position them around the face using rotations.\n\n \u003cGroup ... \u003e\n \u003c!-- Content for the \"12\" marker --\u003e\n \u003c/Group\u003e\n \u003cGroup pivotX=\"...\" pivotY=\"...\" angle=\"30\"\u003e\n \u003c!-- Content for the \"1\" marker --\u003e\n \u003c/Group\u003e"]]