1、如何减小编译产物so大小?
如果是在cmake命令中构建,在CMakeLists.txt中添加CMake编译参数和C++编译器参数
1、设置构建类型为Release,这将关闭debug调试信息
set(CMAKE_BUILD_TYPE Release)
2、-s
剥离符号表信息
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s")
如果是DevEco Studio IDE中集成,则在 externalnativeoptions 配置编译参数和编译器参数
模块级build-profile.json5中 externalNativeOptions 参数是NDK工程C/C++文件编译配置的入口,可以通过path指定CMake脚本路径、arguments配置CMake参数、cppFlags配置C++编译器参数、abiFilters配置编译架构等。
"externalNativeOptions": {
"path": "./src/main/cpp/CMakeLists.txt",
"arguments": "-DCMAKE_BUILD_TYPE=Release",
"cppFlags": "-s",
}
2、如何判断应用处于前台还是后台?
有没有判断前后台的API 在切换到前后台时有回调吗?
可以使用ApplicationContext.on注册对当前应用前后台变化的监听:
或者appManager.getRunningProcessInformation获取当前运行进程的有关信息:
3、如何调整 IDE 内存大小?
AndroidStudio 中 gradle.properties 中 org.gradle.jvmargs
可以设置IDE占用内存大小,DevEco有没有类似配置呢?
在hvigor-config.json5中修改maxOldSpaceSize字段,根据工程的大小,适当将其增大(如设置为8192)
"nodeOptions": {
"maxOldSpaceSize": 8192
}
4、如何处理tabs嵌套web滑动场景?
tabs组件嵌套web组件滚动场景会出现下面问题:
- web页上下滑动的时候会误触发tab页翻页手势
- tab页切换时web组件还可以上下滚动
- 问题1:可以通过给web组件设置嵌套滚动nestedScroll属性解决
- 问题2:可以通过给web组件设置网页是否允许滚动setScrollable属性解决
参考代码:
import web_webview from '@ohos.web.webview';
import business_error from '@ohos.base';
@Component
@Entry
struct TabWebPage2 {
@State message: string = 'Hello World';
private tabsController = new TabsController();
private currentIndex: number = 0;
private webviewController: web_webview.WebviewController = new web_webview.WebviewController();
private panOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Vertical })
// 控制滑动页面进行页面切换
@State flag: boolean = true
build() {
Tabs({ barPosition: BarPosition.End, controller: this.tabsController }) {
TabContent() {
Web({ src: 'https://m.xxx.com/', controller: this.webviewController })// 设置嵌套滚动
.nestedScroll({
scrollForward: NestedScrollMode.PARENT_FIRST,
scrollBackward: NestedScrollMode.SELF_FIRST
})
}.tabBar(this.tabBuilder('首页', 0))
TabContent() {
Column() {
Text("发现")
}.width('100%').height('100%')
}.tabBar(this.tabBuilder('发现', 1))
TabContent() {
Column() {
Text("推荐")
}.width('100%').height('100%')
}.tabBar(this.tabBuilder('推荐', 2))
TabContent() {
Column() {
Text("我的")
}.width('100%').height('100%')
}
.tabBar(this.tabBuilder('我的', 3))
}
.onChange((index: number) => {
this.currentIndex = index;
})
.scrollable(this.flag)
.onAnimationStart((index: number, targetIndex: number, event: TabsAnimationEvent) => {
})
.onAnimationEnd((index: number, event: TabsAnimationEvent) => {
// 切换动画结束时触发该回调,设置web组件可以滑动
this.webviewController.setScrollable(true)
})
.onGestureSwipe((index: number, event: TabsAnimationEvent) => {
// 在页面跟手滑动过程中,逐帧触发该回调,设置tab页切换的时web页无法上下滑动
this.webviewController.setScrollable(false)
})
}
@Builder
tabBuilder(title: string, targetIndex: number) {
Column() {
Text(title)
.fontColor(this.currentIndex === targetIndex ? '#1698CE' : '#6B6B6B')
}
.width('100%')
.height(50)
.justifyContent(FlexAlign.Center)
}
}
5、Marquee 组件的文字滚动,第一次滚动出现大量空白,如何避免空白出现?
Marquee 组件在文本滚动时,文本滚动到控件的开头,会造成大量空白,如何实现让文本末尾滚动到控件末尾时停止,避免空白出现呢?
Marquee 目前不支持在文本末尾停止,可以使用 scroll 代替跑马灯组件实现文字滚动。
示例代码:
@Observed
class NewsTitle {
public title: string
constructor(title: string) {
this.title = title;
}
}
@Entry
@Component
struct Index {
@State textList: string[] = [
'this is a test string1 this is a test string1 this is a test string1',
'this is a test string2 this is a test string2',
'this is a test string3 this is a test string3 this is a test string3 this is a test string3',
]
@State count: number = 1;
build() {
Row() {
Column() {
myMarqueeCard({
textList: $textList,
updateList: () => {
this.textList = [
`这是测试数据${this.count++}这是测试数据${this.count++}这是测试数据${this.count++}这是测试数据${this.count++}`,
`这是测试数据${this.count++}这是测试数据${this.count++}这是测试数据${this.count++}`,
`这是测试数据${this.count++}这是测试数据${this.count++}这是测试数据${this.count++}这是测试数据${this.count++}这是测试数据${this.count++}`,
]
}
})
}
.width('100%')
.margin(20)
}
.height('100%')
}
}
class Tmp {
text: string = ''
scroller: Scroller = new Scroller()
}
@Component
struct myMarqueeCard {
@Link @Watch('handleNewList') textList: string[]
@State list: string[] = [];
scroller1: Scroller = new Scroller()
scroller2: Scroller = new Scroller()
scroller3: Scroller = new Scroller()
updateList?: () => void
handleNewList() {
console.log(JSON.stringify(this.textList))
}
build() {
Column() {
this.SingleText({ text: this.textList[0], scroller: this.scroller1 })
this.SingleText({ text: this.textList[1], scroller: this.scroller2 })
this.SingleText({ text: this.textList[2], scroller: this.scroller3 })
}
}
@Builder
SingleText($$: Tmp) {
Scroll($$.scroller) {
Row() {
Text($$.text).fontSize(30).onAppear(() => {
this.handleScroll($$.scroller)
})
}
}
.width(300)
.scrollable(ScrollDirection.Horizontal)
.enableScrollInteraction(false)
.scrollBar(BarState.Off)
}
handleScroll(scroller: Scroller) {
let timer: number = setInterval(() => {
const curOffset: OffsetResult = scroller.currentOffset()
scroller.scrollTo({
xOffset: curOffset.xOffset + 50, yOffset: curOffset.yOffset, animation: {
duration: 1000,
curve: Curve.Linear
}
})
if (scroller.isAtEnd()) {
// clearInterval(timer);
if (this.scroller1.isAtEnd() && this.scroller2.isAtEnd() && this.scroller3.isAtEnd()) {
// 其他操作
if (this.updateList) {
this.scroller1.scrollTo({ xOffset: 0, yOffset: 0, animation: { duration: 0 } })
this.scroller2.scrollTo({ xOffset: 0, yOffset: 0, animation: { duration: 0 } })
this.scroller3.scrollTo({ xOffset: 0, yOffset: 0, animation: { duration: 0 } })
this.updateList()
}
}
}
}, 500)
}
}