• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Development Guidelines on Clock Apps<a name="EN-US_TOPIC_0000001115417926"></a>
2
3## Overview<a name="section11522349121115"></a>
4
5This document describes how to quickly set up a development environment \(using the Hi3516D V300 development board\) and develop a clock app running on OpenHarmony. You can click [here](https://gitee.com/openharmony/applications_app_samples/tree/master/code/Solutions/Tools/JsClock) to obtain the sample code.
6
7The clock app displays the current time, as shown in the following figure.
8
9**Figure  1** Clock display effect<a name="fig7763172132019"></a>
10
11
12![](figures/clock.png)
13
14## Preparations<a name="section6592121861218"></a>
15
16Download and install DevEco Studio. For details, see the [DevEco Studio User Guide](../../application-dev/quick-start/deveco-studio-user-guide-for-openharmony.md).
17
18## How to Develop<a name="section19901741111312"></a>
19
20The clock app displays the current time through a clock face and numbers.
21
22As shown in [Figure 1](#fig7763172132019), the UI consists of two parts:
23
24-   Clock face area: displays a dynamic analog clock whose hands rotate accurately.
25-   Digital time area: displays the current time in numerals.
26
27To build such an app, we can create a page that has a flexible layout with two rows vertically arranged. The development procedure is as follows:
28
291. Add a root component **<div\>** to the **.hml** file. Note that each **.hml** file can contain only one root component. The sample code is as follows:
30
31    ```
32    <div class="container">
33    </div>
34    ```
35
36   **class="container"** indicates the style used by the component. The **container** is a style class defined in the **index.css** file.
37
38    ```
39    .container {
40        flex-direction: column;
41        justify-content: center;
42        align-items: center;
43        width: 100%;
44        height: 100%;
45    }
46    ```
47
48    The height and width of the root component **<div\>** are set in the style class. Note that the height and width must be explicitly specified \(except for some components, such as **<text\>**\). Otherwise, the component may fail to display. In the **container** style class, the **flex-direction** attribute is set to **column**, which means that child components of **<div\>** are vertically arranged from top to bottom for implementing the flexible page layout.
49
502. Implement clock hand rotation using the **<stack\>** component. The **<stack\>** component provides a stack container where child components are successively stacked and the latter one overwrites the previous one.
51
52    Add a stack container to the root component. The sample code is as follows:
53
54    ```
55    <div class="container">
56        <stack class="stack">
57            <image src="/common/clock_bg.png" class="clock-bg"></image> <!--Set the clock face image.-->
58            <image src="/common/hour_hand.png" class="clock-hand"
59                   style="transform : rotate({{ hour * 30 + minute / 2 }}deg);"></image> <!--Set the hour hand image. (hour * 30) indicates that the hour hand rotates 30 degrees every hour. (minute / 2) indicates the rotation degrees per minute.-->
60            <image src="/common/minute_hand.png" class="clock-hand"
61                   style="transform : rotate({{ minute * 6 + second / 10 }}deg);"></image> <!--Set the minute hand image. (minute * 6) indicates that the minute hand rotates 6 degrees every minute. (second / 10) indicates the rotation degrees per second.-->
62            <image src="/common/second_hand.png" class="clock-hand"
63                   style="transform : rotate({{ second * 6 }}deg);"></image> <!--Set the second hand image. (second * 6) indicates that the second hand rotates 6 degrees per second.-->
64       </stack>
65    </div>
66    ```
67
68   **style="transform: rotate\(\{\{ second \* 6 \}\}deg\)** sets the rotation event of a component. **transform** translates, rotates, or scales an image. **rotate** rotates an image. You can set an image to rotate around its x-axis or y-axis.
69
70    Set attributes, such as the height, width, and position, of the stack component in the CSS file. The sample code is as follows:
71
72    ```
73    .stack {
74        flex-direction: column;
75        justify-content: center;
76        align-items: center;
77        width: 100%;
78        height: 50%;
79    }
80    ```
81
82    Set attributes, such as the height and width, of the clock-bg component in the CSS file. The sample code is as follows:
83
84    ```
85    .clock-bg {
86        width: 80%;
87        height: 80%;
88        object-fit: scale-down;
89    }
90    ```
91
92    Set attributes, such as the height and width of the hour, minute, and second hands, of the clock-hand component in the CSS file. The sample code is as follows:
93
94    ```
95    .clock-hand {
96        width: 25%;
97        height: 65%;
98        object-fit: contain;
99    }
100    ```
101
102    Add a timer in the **index.js** file to update the hour, minute, and second variables in real time so that the time can be automatically updated on the app UI. The sample code is as follows:
103
104    ```
105    export default {
106        timer: undefined,
107        // Define parameters.
108        data: {
109          hour: 0,   // Define hours.
110          minute: 0, // Define minutes.
111          second: 0  // Define seconds.
112        },
113        onInit () {
114            this.updateTime();
115            this.timer = setInterval(this.updateTime, 1000)// Set the timer to 1 second.
116        },
117        updateTime: function () {
118            var nowTime = new Date()
119            this.hour = nowTime.getHours()
120            this.minute = nowTime.getMinutes()
121            this.second = nowTime.getSeconds()
122            if (this.hour < 10) {
123                this.hour = '0' + this.hour
124            }
125            if (this.minute < 10) {
126                this.minute = '0' + this.minute
127            }
128            if (this.second < 10) {
129                this.second = '0' + this.second
130            }
131        },
132    }
133    ```
134
1353. Display the current time in numerals under the analog clock. Add the text component at the end of the root layout. The following example shows the UI structure:
136
137    ```
138    <text class="digit-clock"> {{ hour }}:{{ minute }}:{{ second }}</text>
139    ```
140
141    class=**"digit-clock"** sets the height, width, and font size of the component. The sample code is as follows:
142
143    ```
144    .digit-clock {
145        font-size: 58px;
146        width: 100%;
147        margin-top: 0px;
148        text-align: center;
149    }
150    ```
151
1524.  Set the style, animation effect, and dynamic data binding for all components. The complete sample code is as follows:
153    -  **index.hml**
154
155        ```
156        <div class="container">
157            <stack class="stack">
158                <image src="/common/clock_bg.png" class="clock-bg"></image>
159                <image src="/common/hour_hand.png" class="clock-hand"
160                       style="transform : rotate({{ hour * 30 + minute / 2 }}deg);"></image>
161                <image src="/common/minute_hand.png" class="clock-hand"
162                       style="transform : rotate({{ minute * 6 + second / 10 }}deg);"></image>
163                <image src="/common/second_hand.png" class="clock-hand"
164                       style="transform : rotate({{ second * 6 }}deg);"></image>
165            </stack>
166            <text class="digit-clock">{{ hour }}:{{ minute }}:{{ second }}</text>
167        </div>
168        ```
169
170    -  **index.css**
171
172        ```
173        .container {
174            flex-direction: column;
175            justify-content: center;
176            align-items: center;
177            width: 100%;
178            height: 100%;
179        }
180
181        .stack {
182            flex-direction: column;
183            justify-content: center;
184            align-items: center;
185            width: 100%;
186            height: 50%;
187        }
188
189        .digit-clock {
190            font-size: 58px;
191            width: 100%;
192            margin-top: 0px;
193            text-align: center;
194        }
195
196        .clock-bg {
197            width: 80%;
198            height: 80%;
199            object-fit: scale-down;
200        }
201
202        .clock-hand {
203            width: 25%;
204            height: 65%;
205            object-fit: contain;
206        }
207        ```
208
209    -  **index.js**
210
211        A **.js** file is used to implement logic interactions of the clock app. The following **.js** file implements the function of periodically obtaining the system time.
212
213        ```
214        export default {
215            timer: undefined,
216            data: {
217                hour: 0,
218                minute: 0,
219                second: 0
220            },
221            onInit() {
222                this.updateTime()
223                this.timer = setInterval(this.updateTime, 1000)
224            },
225            updateTime: function () {
226                var nowTime = new Date()
227                this.hour = nowTime.getHours()
228                this.minute = nowTime.getMinutes()
229                this.second = nowTime.getSeconds()
230                if (this.hour < 10) {
231                    this.hour = '0' + this.hour
232                }
233                if (this.minute < 10) {
234                    this.minute = '0' + this.minute
235                }
236                if (this.second < 10) {
237                    this.second = '0' + this.second
238                }
239            },
240            onDestroy() {
241                clearInterval(this.timer);
242            }
243        }
244        ```
245
246
247
248## Signing and Packaging<a name="section10601181101516"></a>
249
250After finishing writing the app code, you need to sign and package the app before running it on a real device. For details, see [Signing and Packaging Guide](https://developer.harmonyos.com/en/docs/documentation/doc-guides/ohos-debugging-and-running-0000001263040487#section17660437768).
251
252## Running on the Real Device<a name="section092721731511"></a>
253
254Before you install the app and run it on the development board, install the DevEco Device Tool by following operations provided in [DevEco Device Tool User Guide](https://device.harmonyos.com/en/docs/ide/user-guides/service_introduction-0000001050166905). Burn OpenHarmony into the development board and run it. For details about how to build, burn, and run an image, see [Getting Started with the Standard System with Hi3516 (IDE Mode)](../quick-start/quickstart-appendix-hi3516-ide.md). After the image is running normally and the system is started properly, perform the following steps to install or uninstall the app:
255
2561. Obtain the HDC client from the following path:
257
258    ```
259    developtools/hdc_standard/prebuilt/windows/hdc_std.exe
260    ```
261
262    Change the HDC client name to **hdc.exe** and add the path above to the system environment variable **path**.
263
2642. Open the **cmd** window, and run the following commands to push the HAP file to the device directory, and install it:
265
266    ```
267    hdc install clock.hap
268    ```
269
2703. Run the following command to start the app. **ohos.samples.clock** indicates the app package name, and **MainAbility** indicates the ability started by the app.
271
272    ```
273    hdc shell aa start -d 123 -a ohos.samples.clock.MainAbility -b ohos.samples.clock
274    ```
275
2764. \(Optional\) Run the following command to uninstall the app. **ohos.samples.clock** indicates the app package name.
277
278    ```
279    hdc shell bm uninstall -n ohos.samples.clock
280    ```
281
282
283## FAQs<a name="section1122413460153"></a>
284
285### hdc\_std Fails to Connect to a Device<a name="section1922725151614"></a>
286
287-  **Symptom**
288
289   **\[Empty\]** is displayed in the output after the **hdc\_std list targets** command is run.
290
291-  **Possible Causes and Solutions**
292     - The device fails to be identified.
293
294       Check whether **HDC Device** exists in the universal serial bus device of the device manager. If **HDC Device** does not exist, the device cannot be connected. In this case, remove and then insert the device or burn the latest image for the device.
295
296     - hdc\_std works improperly.
297
298       Run the **hdc kill** or **hdc start -r** command to kill or restart the HDC service, and then run the **hdc list targets** command to check whether device information is obtained.
299
300     - hdc\_std does not match the device.
301
302       If the latest image is burnt for the device, hdc\_std must also be of the latest version. As hdc\_std is updated continuously, obtain hdc\_std of the latest version from the **developtools\_hdc\_standard** repository in the **prebuilt** directory.
303
304
305
306### hdc\_std Fails to Run<a name="section15657547131615"></a>
307
308-  **Symptom**
309
310    The **hdc\_std.exe** file does not run after being clicked.
311
312-  **Possible Causes and Solutions**
313
314   **hdc\_std.exe** requires no installation and can be directly used on a disk. It can also be added to environment variables. Open the **cmd** window and run the **hdc\_std** command to use **hdc\_std.exe**.