
【引言】
本文将介绍如何使用鸿蒙NEXT框架开发一个简单的光强仪应用,该应用能够实时监测环境光强度,并给出相应的场景描述和活动建议。
【环境准备】
电脑系统:windows 10
开发工具:DevEco Studio NEXT Beta1 Build Version: 5.0.3.806
工程版本:API 12
真机:mate60 pro
语言:ArkTS、ArkUI
【功能实现】
- 项目结构
本项目主要由以下几个部分组成:
LightIntensityItem 类:用于定义光强度范围及其相关信息,包括光强度的起始值、终止值、类型、描述和建议活动。通过构造函数初始化这些属性,便于后续使用。
LightIntensityMeter 组件:这是光强仪的核心,包含状态管理、传感器初始化和光强度更新等功能。组件使用 @State 装饰器来管理当前光强度值和类型,并在组件即将出现时获取传感器列表。
传感器数据处理:通过监听环境光传感器的数据,实时更新当前光强度值,并根据光强度范围更新当前类型。这一过程确保了用户能够获得最新的环境光信息。
- 界面布局
光强仪的用户界面使用了鸿蒙系统的布局组件,包括 Column 和 Row。界面展示了当前光强度值和类型,并通过仪表组件直观地显示光强度。用户可以清晰地看到光强度的变化,并获得相应的场景描述和活动建议。
仪表组件:用于显示当前光强度值,采用了动态更新的方式,确保用户能够实时看到光强度的变化。
信息展示:通过遍历光强度范围列表,展示每个类型的光强度范围、描述和建议活动。这一部分为用户提供了实用的信息,帮助他们根据环境光条件做出相应的决策。
- 总结
通过本案例,开发者可以学习到如何在鸿蒙系统中使用传感器服务和组件化开发方式,构建一个功能完整的光强仪应用。该应用不仅能够实时监测光强度,还能根据不同的光强度范围提供实用的建议,提升用户体验。
【完整代码】
import { sensor } from '@kit.SensorServiceKit';
import { BusinessError } from '@kit.BasicServicesKit';
class LightIntensityItem {
luxStart: number;
luxEnd: number;
type: string;
description: string;
recommendation: string;
constructor(luxStart: number, luxEnd: number, type: string, description: string, recommendation: string) {
this.luxStart = luxStart;
this.luxEnd = luxEnd;
this.type = type;
this.description = description;
this.recommendation = recommendation;
}
}
@Entry
@Component
struct LightIntensityMeter {
@State currentType: string = "";
@State currentIntensity: number = 0;
@State lightIntensityList: LightIntensityItem[] = [
new LightIntensityItem(0, 1, '极暗', '夜晚户外,几乎没有光源。', '不宜进行任何活动,适合完全休息。'),
new LightIntensityItem(1, 10, '很暗', '夜晚室内,只有微弱的灯光或月光。', '只适合睡觉,避免使用电子设备。'),
new LightIntensityItem(10, 50, '暗', '清晨或傍晚,自然光较弱。', '轻松休闲,避免长时间阅读,适合放松。'),
new LightIntensityItem(50, 100, '较暗', '白天阴天,室内光线柔和。', '日常生活,短时间阅读,适合轻度活动。'),
new LightIntensityItem(100, 300, '适中', '白天多云,室内光线适中。', '工作学习,适度阅读,适合大部分室内活动。'),
new LightIntensityItem(300, 500, '较亮', '白天晴朗,室内光线充足。', '正常工作学习,长时间阅读,适合大部分活动。'),
new LightIntensityItem(500, 1000, '亮', '阴天室外,自然光较强。', '户外活动,注意防晒,适合户外休闲。'),
new LightIntensityItem(1000, 100000, '爆表了', '夏季正午直射阳光,自然光极其强烈。',
'尽可能避免直视太阳,户外活动需戴太阳镜,注意防晒。'),
];
aboutToAppear(): void {
sensor.getSensorList((error: BusinessError) => {
if (error) {
console.error('获取传感器列表失败', error);
return;
}
this.startLightIntensityUpdates();
});
}
private startLightIntensityUpdates(): void {
sensor.on(sensor.SensorId.AMBIENT_LIGHT, (data) => {
console.info(`data.intensity: ${data.intensity}`);
this.currentIntensity = data.intensity;
for (const item of this.lightIntensityList) {
if (data.intensity >= item.luxStart && data.intensity <= item.luxEnd) {
this.currentType = item.type;
break;
}
}
}, { interval: 10000000 });
}
build() {
Column() {
Text("光强仪")
.width('100%')
.height(44)
.backgroundColor("#fe9900")
.textAlign(TextAlign.Center)
.fontColor(Color.White);
Row() {
Gauge({
value: this.currentIntensity > 1000 ? 1000 : this.currentIntensity,
min: 0,
max: 1000
}) {
Column() {
Text(`${Math.floor(this.currentIntensity)}`)
.fontSize(25)
.fontWeight(FontWeight.Medium)
.fontColor("#323232")
.height('30%')
.textAlign(TextAlign.Center)
.margin({ top: '22.2%' })
.textOverflow({ overflow: TextOverflow.Ellipsis })
.maxLines(1);
Text(`${this.currentType}`)
.fontSize(16)
.fontColor("#848484")
.fontWeight(FontWeight.Regular)
.width('47.4%')
.height('15%')
.textAlign(TextAlign.Center)
.backgroundColor("#e4e4e4")
.borderRadius(5);
}.width('100%');
}
.startAngle(225)
.endAngle(135)
.height(250)
.strokeWidth(18)
.description(null)
.trackShadow({ radius: 7, offsetX: 7, offsetY: 7 })
.padding({ top: 30 });
}.width('100%').justifyContent(FlexAlign.Center);
Column() {
ForEach(this.lightIntensityList, (item: LightIntensityItem, index: number) => {
Row() {
Text(`${item.luxStart}~${item.luxEnd}Lux `)
.fontSize('25lpx')
.textAlign(TextAlign.Start)
.fontColor("#3d3d3d")
.width('220lpx')
Text(`${item.description}\n${item.recommendation}`)
.fontSize('23lpx')
.textAlign(TextAlign.Start)
.fontColor("#3d3d3d")
.layoutWeight(1)
}.width('660lpx')
.padding({ bottom: 10, top: 10 })
.borderWidth({ bottom: 1 })
.borderColor("#737977");
});
}.width('100%');
}
.height('100%')
.width('100%');
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.