HarmonyOS 鸿蒙Next 全屏启动页
HarmonyOS 鸿蒙Next 全屏启动页
如何配置一个全屏的启动页,
1、startWindowIcon 配置之后之后只会显示图片大小, 并且居中显示, 如何实现在所有尺寸机型上startWindowIcon都全屏显示
2、或者有没有其他方式实现全屏的启动页
视频支付宝、抖音的效果. 我希望的效果是 小图永远靠下显示(带边距), 或者大图永远全屏显示
更多关于HarmonyOS 鸿蒙Next 全屏启动页的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
目前 startWindowIcon 的大小是放多大画多大,一般推荐放一个icon小图。startWindowIcon 启动图全屏居中,按图片像素大小居中显示,没有能力根据设备屏幕或窗口大小自适应调整。
如果是设置全屏图片,可以通过:display.getDefaultDisplaySync().width、display.getDefaultDisplaySync().height 获取屏幕宽高,然后准备一张屏幕宽高尺寸的图片。(以Mate 60 Pro为例,为1260*2720像素)
另外,还可以通过设置一个自定义的启动页的方式来实现的:
1、需要新建一个启动页Page,用于显示启动图。
2、可修改module.json5中startWindowBackground背景色和启动图一致,并设置starticon为透明的空图片,即可隐藏默认的启动页。
参考案例如下-应用首次启动:
可以给组件加一个动画,根据 demo 修改参考如下:
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { preferences } from '@kit.ArkData';
import { common } from '@kit.AbilityKit';
import { router } from '@kit.ArkUI';
import Logger from '../common/utils/Logger';
import CommonConstants from '../common/constants/CommonConstants';
import CustomDialogComponent from '../view/CustomDialogComponent';
import { GlobalContext } from '../common/utils/GlobalContext';
/**
* The LauncherPage is the entry point of the application and shows how to develop the LauncherPage.
* Stay on the LauncherPage for a few seconds to jump to the AdvertisingPage.
* Developers can replace the background image.
*/
@Entry
@Component
struct LauncherPage {
private context?: common.UIAbilityContext;
private timerId: number = 0;
private isJumpToAdvertising: boolean = false;
dialogController: CustomDialogController = new CustomDialogController({
builder: CustomDialogComponent(
{
cancel: () => {
this.onCancel();
},
confirm: () => {
this.onConfirm();
}
}),
alignment: DialogAlignment.Bottom,
offset: { dx: 0, dy: CommonConstants.DIALOG_CONTROLLER_DY_OFFSET },
customStyle: true,
autoCancel: false
});
onCancel() {
// Exit the application.
this.context?.terminateSelf();
}
onConfirm() {
// Save privacy agreement status.
this.saveIsPrivacy();
this.jumpToAdvertisingPage();
}
onPageHide() {
if (this.isJumpToAdvertising) {
router.clear();
}
// globalThis.isJumpPrivacy = true;
GlobalContext.getContext().setObject('isJumpPrivacy', true);
clearTimeout(this.timerId);
}
/**
* Jump to advertising page.
*/
jumpToAdvertisingPage() {
this.timerId = setTimeout(() => {
this.isJumpToAdvertising = true;
router.pushUrl({
url: CommonConstants.ADVERTISING_PAGE_URL
}).catch((error: Error) => {
Logger.error(CommonConstants.LAUNCHER_PAGE_TAG, 'LauncherPage pushUrl error ' + JSON.stringify(error));
});
}, CommonConstants.LAUNCHER_DELAY_TIME);
}
saveIsPrivacy() {
let preferences: Promise<preferences.Preferences> = this.getDataPreferences(this);
preferences.then((result: preferences.Preferences) => {
let privacyPut = result.put(CommonConstants.PREFERENCES_KEY_PRIVACY, false);
result.flush();
privacyPut.then(() => {
Logger.info(CommonConstants.LAUNCHER_PAGE_TAG, 'Put the value of startup Successfully.');
}).catch((err: Error) => {
Logger.error(CommonConstants.LAUNCHER_PAGE_TAG, 'Put the value of startup Failed, err: ' + err);
});
}).catch((err: Error) => {
Logger.error(CommonConstants.LAUNCHER_PAGE_TAG, 'Get the preferences Failed, err: ' + err);
});
}
onPageShow() {
// this.context = getContext(this) as common.UIAbilityContext;
// // Get the operation class for saving data.
// this.getDataPreferences(this).then((preferences: preferences.Preferences) => {
// preferences.get(CommonConstants.PREFERENCES_KEY_PRIVACY, true).then((value: preferences.ValueType) => {
// Logger.info(CommonConstants.LAUNCHER_PAGE_TAG, 'onPageShow value: ' + value);
// if (value) {
// // let isJumpPrivacy: boolean = globalThis.isJumpPrivacy ?? false;
// let isJumpPrivacy: boolean = (GlobalContext.getContext().getObject('isJumpPrivacy') as boolean) ?? false;
// if (!isJumpPrivacy) {
// this.dialogController.open();
// }
// } else {
// this.jumpToAdvertisingPage();
// }
// });
// });
}
/**
* Get data preferences action.
*/
getDataPreferences(common: Object) {
return preferences.getPreferences(getContext(common), CommonConstants.PREFERENCES_FILE_NAME);
}
build() {
Stack() {
Column() {
Image($r('app.media.ic_logo'))//ic_advertising_background
.width($r('app.float.launcher_logo_size'))
.height($r('app.float.launcher_logo_size'))
.margin({ top: CommonConstants.LAUNCHER_IMAGE_MARGIN_TOP })
Text($r('app.string.healthy_life_text'))
.width($r('app.float.launcher_life_text_width'))
.height($r('app.float.launcher_life_text_height'))
.healthyLifeTextStyle(FontWeight.Bold,
CommonConstants.LAUNCHER_LIFE_TEXT_SPACING,
$r('app.float.launcher_text_title_size'),
$r('app.color.launcher_text_title_color'))
.margin({ top: CommonConstants.LAUNCHER_TEXT_TITLE_MARGIN_TOP })
Text($r('app.string.healthy_life_introduce'))
.height(CommonConstants.LAUNCHER_TEXT_INTRODUCE_HEIGHT)
.healthyLifeTextStyle(FontWeight.Normal,
CommonConstants.LAUNCHER_TEXT_INTRODUCE_SPACING,
$r('app.float.launcher_text_introduce_size'),
$r('app.color.launcher_text_introduce_color'))
.opacity($r('app.float.launcher_text_opacity'))
.margin({ top: CommonConstants.LAUNCHER_TEXT_INTRODUCE_MARGIN_TOP })
}
.transition(TransitionEffect.asymmetric(TransitionEffect.OPACITY.animation({
duration: 1000,
curve: Curve.Ease
}), TransitionEffect.OPACITY.animation({
duration: 0
})))
// .backgroundColor($r('app.color.privacy_back_text'))
.height(CommonConstants.FULL_HEIGHT)
.width(CommonConstants.FULL_WIDTH)
}
}
}
// Healthy living text common styles.
@Extend(Text) function healthyLifeTextStyle (fontWeight: number,
textAttribute: number, fontSize: Resource, fontColor: Resource) {
.fontWeight(fontWeight)
.letterSpacing(textAttribute)
.fontSize(fontSize)
.fontColor(fontColor)
}
更多关于HarmonyOS 鸿蒙Next 全屏启动页的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
HarmonyOS 鸿蒙Next 全屏启动页实现方式:
在HarmonyOS应用开发中,实现全屏启动页通常涉及修改配置文件和布局文件。以下是基本步骤:
-
修改配置文件:
- 打开
config.json
文件,确保应用具有请求全屏显示所需的权限。 - 在
module
>abilities
>[ability]
节点下,设置launchMode
为singleTop
(或根据需要设置)。 - 确保
window
配置中的fullScreen
属性设置为true
,以允许应用全屏显示。
- 打开
-
创建布局文件:
- 在
resources/base/layout
目录下创建一个新的XML布局文件,如launch_screen.xml
。 - 在该布局文件中,使用
<Image>
等组件定义全屏显示的启动页内容。
- 在
-
设置启动页Ability:
- 在
MainAbility
或其他启动Ability的代码中,确保在onCreate
方法中加载launch_screen.xml
布局。 - 可以通过设置
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
(或其他方向)来固定屏幕方向,确保启动页全屏显示时不受旋转影响。
- 在
-
编译与运行:
- 编译项目,确保无错误。
- 在支持HarmonyOS的设备或模拟器上运行应用,观察全屏启动页效果。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html