做網(wǎng)站包含的技術(shù)個人網(wǎng)站素材圖片
鶴壁市浩天電氣有限公司
2026/01/24 17:16:14
做網(wǎng)站包含的技術(shù),個人網(wǎng)站素材圖片,用c 做畢業(yè)設(shè)計的音樂網(wǎng)站,xampp wordpress安裝教程Harmony開發(fā)之服務(wù)卡片開發(fā)——解鎖原子化服務(wù)
引入#xff1a;桌面卡片的便捷交互
當我們使用手機時#xff0c;經(jīng)常會發(fā)現(xiàn)一些應(yīng)用在桌面上提供了小巧精致的卡片#xff0c;比如天氣卡片顯示實時溫度、運動卡片展示今日步數(shù)、音樂卡片提供播放控制。這些就是HarmonyOS的服…Harmony開發(fā)之服務(wù)卡片開發(fā)——解鎖原子化服務(wù)引入桌面卡片的便捷交互當我們使用手機時經(jīng)常會發(fā)現(xiàn)一些應(yīng)用在桌面上提供了小巧精致的卡片比如天氣卡片顯示實時溫度、運動卡片展示今日步數(shù)、音樂卡片提供播放控制。這些就是HarmonyOS的服務(wù)卡片Service Widget它們無需打開完整應(yīng)用就能提供核心信息并支持快捷操作極大地提升了用戶體驗和操作效率。一、服務(wù)卡片核心概念1.1 什么是服務(wù)卡片服務(wù)卡片是HarmonyOS原子化服務(wù)的一種呈現(xiàn)形式是界面展示的控件。它作為應(yīng)用的重要入口通過在外圍提供快捷訪問特定功能的能力實現(xiàn)應(yīng)用功能的原子化。1.2 服務(wù)卡片的優(yōu)勢免安裝使用用戶無需安裝完整應(yīng)用即可使用核心功能即用即走輕量化設(shè)計快速響應(yīng)多設(shè)備協(xié)同卡片可在手機、平板、手表等多設(shè)備間流轉(zhuǎn)動態(tài)更新支持定時更新或數(shù)據(jù)驅(qū)動更新1.3 卡片類型與規(guī)格HarmonyOS支持多種規(guī)格的服務(wù)卡片常見的有2x2、2x4、4x4等不同尺寸開發(fā)者需要根據(jù)功能需求選擇合適的卡片規(guī)格。二、卡片創(chuàng)建與配置2.1 創(chuàng)建卡片工程在DevEco Studio中創(chuàng)建服務(wù)卡片項目時選擇Service Widget模板// 文件entry/src/main/ets/entryability/EntryAbility.ets import { UIAbility } from ohos.app.ability.UIAbility; import { widgetManager } from ohos.app.ability.widgetManager; export default class EntryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { console.info(EntryAbility onCreate); } // 注冊卡片更新回調(diào) onAddForm(want: Want): FormBindingData { let formData: Recordstring, Object { title: 健康步數(shù), steps: 8,256, target: 10,000, progress: 82 }; return new FormBindingData(JSON.stringify(formData)); } }2.2 卡片配置文件// 文件entry/src/main/resources/base/profile/form_config.json { forms: [ { name: widget_steps, description: 健康步數(shù)卡片, src: ./ets/widgets/StepsWidget.ets, window: { designWidth: 720, autoDesignWidth: true }, colorMode: auto, isDefault: true, updateEnabled: true, scheduledUpdateTime: 10:00, updateDuration: 1, defaultDimension: 2 * 2, supportDimensions: [2 * 2, 2 * 4] } ] } // 文件entry/src/main/module.json5 { module: { abilities: [ { name: EntryAbility, srcEntry: ./ets/entryability/EntryAbility.ets, formsEnabled: true, forms: [ { name: widget_steps, description: 健康步數(shù)卡片, src: ./ets/widgets/StepsWidget.ets, window: { designWidth: 720, autoDesignWidth: true }, colorMode: auto, isDefault: true, updateEnabled: true, scheduledUpdateTime: 10:00, updateDuration: 1, defaultDimension: 2 * 2, supportDimensions: [2 * 2, 2 * 4] } ] } ] } }三、卡片布局開發(fā)3.1 基礎(chǔ)卡片組件// 文件entry/src/main/ets/widgets/StepsWidget.ets Entry Component struct StepsWidget { State steps: string 0 State progress: number 0 build() { Column() { // 標題欄 Row() { Image($r(app.media.ic_footprint)) .width(20) .height(20) .margin({ right: 8 }) Text(今日步數(shù)) .fontSize(16) .fontColor(#000000) .fontWeight(FontWeight.Medium) Blank() Image($r(app.media.ic_refresh)) .width(16) .height(16) .onClick(() { this.updateStepsData() }) } .width(100%) .padding({ left: 12, right: 12, top: 8, bottom: 8 }) // 進度區(qū)域 Column() { Text(this.steps) .fontSize(24) .fontColor(#007DFF) .fontWeight(FontWeight.Bold) .margin({ bottom: 4 }) Text(目標: 10,000步) .fontSize(12) .fontColor(#99000000) .margin({ bottom: 8 }) // 進度條 Stack() { // 背景條 Row() .width(100%) .height(6) .backgroundColor(#20007DFF) .borderRadius(3) // 進度條 Row() .width(${this.progress}%) .height(6) .backgroundColor(#007DFF) .borderRadius(3) } .width(100%) .height(6) Text(${this.progress}%完成) .fontSize(10) .fontColor(#99000000) .margin({ top: 4 }) } .width(100%) .padding({ left: 12, right: 12, bottom: 12 }) } .width(100%) .height(100%) .backgroundColor(#FFFFFF) .borderRadius(12) } // 更新步數(shù)數(shù)據(jù) updateStepsData() { // 模擬數(shù)據(jù)更新 const newSteps Math.floor(Math.random() * 10000).toLocaleString() const newProgress Math.floor(Math.random() * 100) this.steps newSteps this.progress newProgress } }3.2 交互式卡片// 文件entry/src/main/ets/widgets/MusicWidget.ets Entry Component struct MusicWidget { State isPlaying: boolean false State currentSong: string HarmonyOS主題曲 State artist: string 華為音樂 State progress: number 40 build() { Column() { // 歌曲信息 Row() { Image($r(app.media.ic_music_cover)) .width(40) .height(40) .borderRadius(8) .margin({ right: 12 }) Column() { Text(this.currentSong) .fontSize(14) .fontColor(#000000) .fontWeight(FontWeight.Medium) .margin({ bottom: 2 }) Text(this.artist) .fontSize(12) .fontColor(#99000000) } .alignItems(HorizontalAlign.Start) Blank() } .width(100%) .padding({ left: 12, right: 12, top: 12 }) // 控制按鈕 Row() { Image($r(app.media.ic_skip_previous)) .width(24) .height(24) .onClick(() this.previousSong()) Blank() .width(20) Image(this.isPlaying ? $r(app.media.ic_pause) : $r(app.media.ic_play)) .width(32) .height(32) .onClick(() this.togglePlay()) Blank() .width(20) Image($r(app.media.ic_skip_next)) .width(24) .height(24) .onClick(() this.nextSong()) } .width(100%) .padding({ bottom: 12 }) .justifyContent(FlexAlign.Center) } .width(100%) .height(100%) .backgroundColor(#FFFFFF) .borderRadius(12) } togglePlay() { this.isPlaying !this.isPlaying // 實際開發(fā)中這里應(yīng)該控制音樂播放 } previousSong() { // 上一首邏輯 } nextSong() { // 下一首邏輯 } }四、卡片更新機制4.1 定時更新配置{ forms: [ { name: weather_widget, updateEnabled: true, scheduledUpdateTime: 08:00, updateDuration: 1, supportDimensions: [2 * 2] } ] }4.2 手動更新實現(xiàn)// 文件entry/src/main/ets/utils/WidgetUtils.ets import { formHost } from ohos.app.ability.formHost; export class WidgetUtils { // 更新指定卡片 static async updateWidget(formId: string): Promisevoid { try { const formBindingData { temperature: 25°C, weather: 晴朗, location: 深圳市, updateTime: new Date().toLocaleTimeString() }; await formHost.updateForm(formId, { data: JSON.stringify(formBindingData) }); } catch (error) { console.error(更新卡片失敗:, error); } } // 獲取所有卡片ID static async getAllFormIds(): Promisestring[] { try { const formInfos await formHost.getAllFormsInfo(); return formInfos.map(info info.formId); } catch (error) { console.error(獲取卡片信息失敗:, error); return []; } } }4.3 數(shù)據(jù)驅(qū)動更新// 文件entry/src/main/ets/widgets/WeatherWidget.ets Entry Component struct WeatherWidget { State temperature: string -- State weather: string 加載中... State updateTime: string aboutToAppear() { this.fetchWeatherData() } build() { Column() { Text(this.temperature) .fontSize(28) .fontColor(#007DFF) .fontWeight(FontWeight.Bold) .margin({ bottom: 4 }) Text(this.weather) .fontSize(14) .fontColor(#666666) .margin({ bottom: 8 }) Text(更新: ${this.updateTime}) .fontSize(10) .fontColor(#999999) } .width(100%) .height(100%) .padding(12) .backgroundColor(#FFFFFF) .borderRadius(12) } async fetchWeatherData() { try { // 模擬API調(diào)用 const response await this.mockWeatherAPI() this.temperature response.temperature this.weather response.weather this.updateTime new Date().toLocaleTimeString() } catch (error) { console.error(獲取天氣數(shù)據(jù)失敗:, error) } } async mockWeatherAPI() { return new Promise{temperature: string, weather: string}((resolve) { setTimeout(() { resolve({ temperature: 25°C, weather: 晴朗 }) }, 1000) }) } }五、卡片跳轉(zhuǎn)與交互5.1 卡片跳轉(zhuǎn)配置// 文件entry/src/main/ets/widgets/NewsWidget.ets Entry Component struct NewsWidget { State newsItems: Array{id: string, title: string, time: string} [ {id: 1, title: HarmonyOS 4.0發(fā)布新特性, time: 10:30}, {id: 2, title: 開發(fā)者大會即將舉行, time: 09:15}, {id: 3, title: 新版本開發(fā)工具更新, time: 昨天} ] build() { Column() { Text(最新資訊) .fontSize(16) .fontColor(#000000) .fontWeight(FontWeight.Medium) .margin({ bottom: 8, left: 12, top: 12 }) List({ space: 4 }) { ForEach(this.newsItems, (item: {id: string, title: string, time: string}) { ListItem() { Row() { Column() { Text(item.title) .fontSize(12) .fontColor(#000000) .maxLines(2) .textOverflow({ overflow: TextOverflow.Ellipsis }) Text(item.time) .fontSize(10) .fontColor(#999999) } .alignItems(HorizontalAlign.Start) Blank() Image($r(app.media.ic_arrow_right)) .width(12) .height(12) } .width(100%) .padding({ left: 12, right: 12, top: 8, bottom: 8 }) .onClick(() { this.navigateToDetail(item.id) }) } }, (item: {id: string, title: string, time: string}) item.id) } .width(100%) .layoutWeight(1) } .width(100%) .height(100%) .backgroundColor(#FFFFFF) .borderRadius(12) } navigateToDetail(newsId: string) { // 跳轉(zhuǎn)到詳情頁 let want { deviceId: , // 默認設(shè)備 bundleName: com.example.newsapp, abilityName: NewsDetailAbility, parameters: { newsId: newsId } }; // 使用featureAbility進行跳轉(zhuǎn) featureAbility.startAbility({ want: want }) .then(() { console.info(跳轉(zhuǎn)成功); }) .catch((error) { console.error(跳轉(zhuǎn)失敗:, error); }); } }5.2 原子化服務(wù)配置// 文件entry/src/main/module.json5 { module: { abilities: [ { name: EntryAbility, srcEntry: ./ets/entryability/EntryAbility.ets, formsEnabled: true, forms: [ { name: news_widget, description: 資訊卡片, src: ./ets/widgets/NewsWidget.ets, isDefault: true, updateEnabled: true, scheduledUpdateTime: 09:00, defaultDimension: 2 * 4, supportDimensions: [2 * 4] } ], metadata: [ { name: ohos.extension.form, resource: $profile:form_config } ] } ], distro: { deliveryWithInstall: true, moduleName: entry, moduleType: entry }, reqCapabilities: [] } }六、多設(shè)備適配與流轉(zhuǎn)6.1 響應(yīng)式布局設(shè)計// 文件entry/src/main/ets/widgets/UniversalWidget.ets Entry Component struct UniversalWidget { State deviceType: string phone aboutToAppear() { this.detectDeviceType() } build() { Column() { if (this.deviceType watch) { this.buildWatchLayout() } else if (this.deviceType tablet) { this.buildTabletLayout() } else { this.buildPhoneLayout() } } .width(100%) .height(100%) .backgroundColor(#FFFFFF) .borderRadius(12) } Builder buildPhoneLayout() { Column() { Text(手機版卡片) .fontSize(16) .fontColor(#000000) // 手機專用布局... } .padding(8) } Builder buildTabletLayout() { Column() { Text(平板版卡片) .fontSize(20) .fontColor(#000000) // 平板專用布局... } .padding(16) } Builder buildWatchLayout() { Column() { Text(手表版卡片) .fontSize(12) .fontColor(#000000) // 手表專用布局... } .padding(4) } detectDeviceType() { // 根據(jù)屏幕尺寸判斷設(shè)備類型 const screenWidth vp2px(display.getDefaultDisplaySync().width); if (screenWidth 600) { this.deviceType watch; } else if (screenWidth 1200) { this.deviceType phone; } else { this.deviceType tablet; } } }6.2 卡片流轉(zhuǎn)處理// 文件entry/src/main/ets/entryability/EntryAbility.ets import { distributedDeviceManager } from ohos.distributedDeviceManager; export default class EntryAbility extends UIAbility { onConnect(want: Want): formBindingData.FormBindingData { // 處理卡片流轉(zhuǎn)連接 console.info(卡片流轉(zhuǎn)連接建立); return this.handleFormRequest(want); } onDisconnect(want: Want): void { // 處理卡片流轉(zhuǎn)斷開 console.info(卡片流轉(zhuǎn)連接斷開); } // 處理跨設(shè)備卡片請求 handleFormRequest(want: Want): formBindingData.FormBindingData { const deviceId want.parameters?.deviceId as string; const formId want.parameters?.formId as string; console.info(處理來自設(shè)備 ${deviceId} 的卡片請求); // 根據(jù)設(shè)備能力返回不同的卡片數(shù)據(jù) return this.getAdaptiveFormData(deviceId); } getAdaptiveFormData(deviceId: string): formBindingData.FormBindingData { // 獲取設(shè)備信息并返回適配的數(shù)據(jù) const deviceInfo this.getDeviceInfo(deviceId); let formData: Recordstring, Object {}; if (deviceInfo.deviceType watch) { formData this.getWatchFormData(); } else { formData this.getDefaultFormData(); } return new formBindingData.FormBindingData(JSON.stringify(formData)); } }七、調(diào)試與優(yōu)化7.1 卡片調(diào)試技巧// 文件entry/src/main/ets/utils/DebugUtils.ets export class DebugUtils { // 卡片性能監(jiān)控 static startPerformanceMonitor(formId: string): void { const startTime new Date().getTime(); // 監(jiān)控卡片加載性能 setTimeout(() { const loadTime new Date().getTime() - startTime; console.info(卡片 ${formId} 加載耗時: ${loadTime}ms); if (loadTime 1000) { console.warn(卡片加載時間過長建議優(yōu)化); } }, 0); } // 內(nèi)存使用檢查 static checkMemoryUsage(): void { const memoryInfo process.getMemoryInfo(); console.info(內(nèi)存使用情況: ${JSON.stringify(memoryInfo)}); if (memoryInfo.availMem 100 * 1024 * 1024) { // 100MB console.warn(可用內(nèi)存不足可能影響卡片性能); } } }7.2 性能優(yōu)化建議圖片資源優(yōu)化使用適當尺寸的圖片避免過大資源數(shù)據(jù)緩存合理使用緩存減少網(wǎng)絡(luò)請求布局簡化避免過于復雜的布局層次按需更新只在必要時更新卡片內(nèi)容總結(jié)服務(wù)卡片是HarmonyOS原子化服務(wù)的核心載體通過提供輕量級、即用即走的用戶體驗極大地增強了應(yīng)用的便捷性和實用性。本文從卡片創(chuàng)建、布局開發(fā)、更新機制、交互跳轉(zhuǎn)到多設(shè)備適配等方面全面介紹了服務(wù)卡片的開發(fā)流程。行動建議根據(jù)功能需求合理選擇卡片尺寸和更新策略注重卡片的視覺設(shè)計和用戶體驗實現(xiàn)多設(shè)備適配確保在不同設(shè)備上都有良好表現(xiàn)優(yōu)化卡片性能確??焖偌虞d和流暢交互充分利用原子化服務(wù)的優(yōu)勢提供免安裝使用體驗通過精心設(shè)計的服務(wù)卡片可以為用戶提供更加便捷高效的服務(wù)入口增強應(yīng)用的用戶粘性和使用價值。