对于笔者这个openharmony高校开发者来说,开源的乐趣在于折腾。在此记录下折腾OpenHarmony标准系统开机动画的过程,ohos的开机动画从3.1beta之后经历过一次变更。下面展示一下变更前和变更后的开机动画。
目录
OpenHarmony3.1beta版本使用的旧开机动画
- 这个开机动画和HarmonyOS的有异曲同工之妙。

从OpenHarmony3.1release版本开始使用的新开机动画
带有开机声效的视频如下:https://ost.51cto.com/show/17830

新版Logo设计者是刘石老师,刘石老师在《新发布的 OpenHarmony Logo 竟有这么多的故事!》一文中分享了新版logo背后的故事

两个版本开机动画实现方式不一样
3.2beta3开机动画模块源码分析
- graphic图形子系统包含了开机动画模块,开机动画模块在ohos3.2beta3源码下foundation/graphic/graphic_2d/frameworks/bootanimation,开机动画模块bootanimation源码结构
-
bootpic.zip打开后内容如下

开机动画源文件bootpic.zip和bootsound.wav等作为配置文件打包至开发板/system/etc/init目录下
阅读bootanimation目录下BUILD.gn可以知道bootpic.zip和bootsound.wav等作为配置文件打包至开放板/system/etc/init目录下。
bootpic.zip的解压缩依赖三方库zlib
阅读bootanimation目录下BUILD.gn可以知道bootpic.zip的解压缩依赖三方库zlib

config.json文件在util.h中被声明为C ++ 的std::string类型

- 阅读bootanimation目录下BUILD.gn可以知道config.json的解析依赖三方库cJSON

开机动画服务启动配置graphic.cfg分别启动了bootanimation进程
开机动画服务启动配置graphic.cfg在ohosbeta3源码./foundation/graphic/graphic_2d/graphic.cfg目录,分别启动了bootanimation和render_service进程。
.cfg只是一个为开发及使用方便而"发明"的一个后缀名。所以,这种文件没有固定的格式,其实也并不能算作是一种文件类型。

- 可以用hdc_std工具将pc端与开发板连接进入shell界面运行
ps -ef
命令,查看进程信息。
-
查看beta3源码foundation/graphic/graphic_2d/BUILD.gn,可以知道graphic.cfg会被打包到开发板/system/etc/init/目录下

-
用hdc_std工具连接开发板后,服务启动配置graphic.cfg在开发板system\etc\init目录也能找到

boot_animation.cpp中将开发板目录/system/etc/init下的bootpic.zip和bootsound.wav声明为C ++ 的std::string类型
- const 就是只读的意思,只在声明中使用;
- static 一般有2个作用,规定作用域和存储方式。
- 对于局部变量, static规定其为静态存储方式, 每次调用的初始值为上一次调用的值,调用结束后存储空间不释放
- 对于全局变量, 如果以文件划分作用域的话,此变量只在当前文件可见; 对于static函数也是在当前模块内函数可见。
using namespace OHOS;
表示使用using指令使用OHOS空间,其在boot_animation.h中定义boot_animation.CPP中要用到boot_animation.h中OHOS空间中的函数和变量。
3.1Beta开机动画模块源码分析
开机动画文件bootanimation.raw作为配置文件打包至开发板/system/etc目录下
RAW是未经处理、也未经压缩的格式,可以把RAW概念化为“原始图像编码数据”或更形象的称为“数字底片”。
3.2beta3开机动画制作
- 1.开机动画在3.2beta版本的源文件为bootpic.zip和bootpic.wav,需要利用视频剪辑软件制作好一段自己的开机动画。
- 2.然后利用特殊软件工具将制作好的视频变成150张图片,然后将图片编号再排好序。
- 3.最后打包为bootpic.zip压缩包。利用hdc_std工具将自制的bootpic.zip导入开发板/system/etc/init目录下。
- 4.重启开发板就可以了。
笔者使用的剪映软件(对笔者来说剪映够用)制作开机动画。然后在
https://www.img2go.com/zh/convert-to-image网址将视频转换成图片。

最后打包成bootpic.zip(需要和原生的bootpic.zip目录机构一样),hdc_std工具使用命令如下:
最后重启开发板,效果如下:https://ost.51cto.com/show/17796
3.1beta版本开机动画制作
准备好一段开机动画的mp4格式动画,利用以下raw_maker.py脚本,内容如下:
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development 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 struct
import zlib
import os
import argparse
import re
import pip
def lost_module(module_name):
print("""
need %s module, try install first:
pip install %s""" % (module_name, module_name))
exit()
try:
import cv2
except ImportError:
pip.main(["install", "opencv-python", "-i", "https://pypi.tuna.tsinghua.edu.cn/simple"])
try:
import cv2
except ImportError:
cv2 = None
lost_module("opencv-python")
try:
from PIL import Image
except ImportError:
pip.main(["install", "pillow"])
try:
from PIL import Image
except ImportError:
Image = None
lost_module("pillow")
try:
import numpy as np
except ImportError:
pip.main(["install", "numpy"])
try:
import numpy as np
except ImportError:
np = None
lost_module("numpy")
class RawMaker:
"""
Make a boot video by a MP4 file or some .img files:
"""
def __init__(self, args):
self._mp4 = args.mp4
self._image = args.image
self._out = args.out
self._display = [int(i) for i in re.split(r'[xX* ]+', args.display.strip())]
self._rotate = args.rotate
self._flip = args.flip
self._fnp = 0
self._vdo = None
self._image_files = []
def _iter_img(self):
if self._mp4:
success, frame = self._vdo.read()
if success:
image = Image.fromarray(frame)
return success, image
else:
return False, None
else:
if self._fnp >= len(self._image_files):
return False, None
image = Image.open(os.path.join(self._image, self._image_files[self._fnp]))
self._fnp += 1
return True, image
def make(self):
frame_count, width, height = 0, 0, 0
if self._mp4:
if not os.path.exists(self._mp4):
print("mp4 file %s is not exist" % self._mp4)
exit()
self._vdo = cv2.VideoCapture(self._mp4)
fps = int(self._vdo.get(cv2.CAP_PROP_FPS))
w = int(self._vdo.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(self._vdo.get(cv2.CAP_PROP_FRAME_HEIGHT))
frame_count = int(self._vdo.get(cv2.CAP_PROP_FRAME_COUNT))
if fps != 30:
print("video fps :", fps, ", width :", w, ", height :", h, ", frame count :", frame_count)
if frame_count <= 0:
exit()
elif self._image:
for fn in os.listdir(self._image):
self._image_files.append(fn)
frame_count = len(self._image_files)
if frame_count <= 0:
exit()
self._image_files.sort()
else:
exit()
output_bytes = bytearray(b"RAW.diff")
offset = 8
screen_old_bytes = None
num = 0
while True:
ret, img = self._iter_img()
if not ret:
break
num += 1
img = img.convert("RGBA")
if self._flip:
img = img.transpose(Image.FLIP_LEFT_RIGHT)
if self._rotate == 90:
img = img.transpose(Image.ROTATE_90)
elif self._rotate == 180:
img = img.transpose(Image.ROTATE_180)
elif self._rotate == 270:
img = img.transpose(Image.ROTATE_270)
if self._display[0] != 0:
img = img.resize((self._display[0], self._display[1]))
img = np.array(img)
height, width = img.shape[0], img.shape[1]
img[img < 20] = 0
img = img.reshape(-1)
screen_now_bytes = img.tobytes()
if screen_old_bytes is None:
screen_old_bytes = screen_now_bytes
start_pos = 0
end_pos = width * height * 4
else:
start_pos, end_pos = 3, 6
for i in range(width * height * 4):
if screen_now_bytes[i] != screen_old_bytes[i]:
start_pos = i
break
for i in range(width * height * 4 - 1, start_pos, -1):
if screen_now_bytes[i] != screen_old_bytes[i]:
end_pos = i + 1
break
screen_old_bytes = screen_now_bytes
print("\r|%s%s|" % ("=" * int(num / frame_count * 30), " " * (30 - int(num / frame_count * 30))),
"%.2f%%" % (num / frame_count * 100), end="", flush=True)
if start_pos == 3 or end_pos == 6:
output_bytes[offset:offset + 16] = struct.pack("IIII", 0, 0, 0, 0)
offset += 16
continue
compressed_bytes = zlib.compress(screen_old_bytes[start_pos:end_pos])
raw_len = end_pos - start_pos
new_len = len(compressed_bytes)
output_bytes[offset:offset + 16] = struct.pack("IIII", 2, start_pos, raw_len, new_len)
offset += 16
output_bytes[offset:offset + new_len] = compressed_bytes
offset += new_len
while new_len % 4 != 0:
new_len += 1
output_bytes[offset:offset + 1] = b'\0'
offset += 1
if not os.path.exists(self._out):
os.makedirs(self._out)
with open(os.path.join(self._out, "bootanimation-%dx%d.raw" % (width, height)), "wb") as fp:
fp.write(output_bytes)
print("\nGenerate successfully!")
def parse_option():
parser = argparse.ArgumentParser(description="Make a boot video by a MP4 file or some .img files",
usage="python raw_maker.py (-m <*.mp4> | -i <directory>) [-o <directory>] "
"[-d <size>] [-r <angle>] [-f]\n"
" eg.: python raw_maker.py -i ./source/png -o ./out -d 640x480\n"
" python raw_maker.py -m ./animation.mp4 -o ./out -d 640x480")
exclusive_group = parser.add_mutually_exclusive_group(required=True)
exclusive_group.add_argument("-m", "--mp4", metavar="<*.mp4>", help="The input <*.mp4> file")
exclusive_group.add_argument("-i", "--image", metavar="<directory>",
help="The <directory> where image files are stored")
parser.add_argument("-o", "--out", metavar="<directory>", default=".",
help="Place generated .raw files into the <directory>")
parser.add_argument("-d", "--display", metavar="<size>", default="0x0",
help="Set the boot video display <size> and zoom the image, e.g.:640x480")
parser.add_argument("-r", "--rotate", metavar="<angle>", type=int, help="Rotate video <angle>, e.g.:90 180 270")
parser.add_argument("-f", "--flip", action="store_true", help="Flip the video", )
return parser.parse_args()
if __name__ == "__main__":
raw_maker = RawMaker(parse_option())
raw_maker.make()
- 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.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
- 204.
- 205.
- 206.
- 207.
在当前文件夹打开cmd窗口执行以下命令

此时会生成cat.raw文件,分辨率为720x1280
播放cat.raw准备好raw_player.py脚本,内容如下:
在当前文件夹打开cmd窗口,执行以下命令;
效果如下:

然后重启开发板,效果如下:https://ost.51cto.com/show/17795
贴一下前两天作者上传的开机视频:
https://ost.51cto.com/show/17796
https://ost.51cto.com/show/17795
比较喜欢旧的开机动画
进步的关键就是爱折腾
666
+1
看了下附件,图片数量挺多的,难怪会如此流畅
猫和妹子都不可辜负😀
浩哥细节了,当时和女朋友去猫咖排的猫片。😁😁😁
不错不错,很详细了
请问如何设置开机动画在屏幕中的显示宽度、高度以及帧速?
以及如何去掉开机动画的音效?