在Android中,如何提高方向改变时内容视图加载的性能?
我有一个教程应用程序,可以加载教程页面列表[这些页面是网页]。这些页面占据了整个屏幕,菜单栏的显示速度约为100dp。虽然不能共享源代码,但下面是教程活动的伪代码在Android中,如何提高方向改变时内容视图加载的性能?,android,performance,optimization,android-webview,android-orientation,Android,Performance,Optimization,Android Webview,Android Orientation,我有一个教程应用程序,可以加载教程页面列表[这些页面是网页]。这些页面占据了整个屏幕,菜单栏的显示速度约为100dp。虽然不能共享源代码,但下面是教程活动的伪代码 HashMap<Integer, customWebView > mapPage; onCreate(){ updateScreen(); } updateScreen(){ Instantiate CustomScrollView(Customized HorizontalScrollView);
HashMap<Integer, customWebView > mapPage;
onCreate(){
updateScreen();
}
updateScreen(){
Instantiate CustomScrollView(Customized HorizontalScrollView);
Instantiate scrollViewChildHolder (LinearLayout with horizontal orientation);
Instantiate mapPage;
for index : total page size{
Instantiate pageHolder(RelativeLayout);
Instantiate customWebView (Custom WebView);//Takes orientation as one of argument
Set html page url;//customWebView has a method loadWebPage() which loads this url.
Add customWebView to pageHoler;
mapPage.put(index, customWebView);
Add pageHolder to scrollViewChildHolder;
}
//pageNo which will be safely fetched from saved state. So it will be 1 on first start
//and on orientation change retains current reading page.
onPageChange(pageNo);
}
//Custom callback on page swipe by user
@Override
onPageChange(int pageNo){
//Custom method which loads html page from url which was set in updateScreen()
mapPage.get(pageNo).loadWebPage();
//unloadNeighbouring pages
//Will pick pageNo+2 & pageNo-2 pages and clears their content.
unloadNeighbouringPagesTo(pageNo);
}
HashMap映射页面;
onCreate(){
updateScreen();
}
updateScreen(){
实例化CustomScrollView(自定义水平滚动视图);
实例化scrollViewChildHolder(具有水平方向的线性布局);
实例化mapPage;
对于索引:总页面大小{
实例化pageHolder(RelativeLayout);
实例化customWebView(customWebView);//将方向作为参数之一
设置html页面url;//customWebView有一个加载此url的方法loadWebPage()。
将customWebView添加到pageHoler;
mapPage.put(索引,customWebView);
将pageHolder添加到scrollViewChildHolder;
}
//将从保存状态安全获取的页码。因此,第一次启动时它将为1
//并且在方向改变时保留当前的阅读页面。
onPageChange(pageNo);
}
//用户在页面滑动时的自定义回调
@凌驾
onPageChange(int pageNo){
//从updateScreen()中设置的url加载html页面的自定义方法
获取(pageNo.loadWebPage();
//卸载相邻页面
//将选择pageNo+2和pageNo-2页面并清除其内容。
将相邻页面卸载到(页面编号);
}
PS:
请建议如何提高方向更改时快速加载的性能。您可以做一件事,创建一个包含方向横向和纵向设计的布局文件。并覆盖OnConfiguration Change() mnifest.xml
<activity
android:name="YourActivity"
android:configChanges="keyboardHidden|orientation|screenSize" // this will not refresh your activity
/>
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
//visible landscape layout
//hide portrait layout
}else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
//visible portrait layout
//hide landscape layout
}
}
@凌驾
公共无效OnConfiguration已更改(配置newConfig){
super.onConfigurationChanged(newConfig);
if(newConfig.orientation==Configuration.orientation\u横向){
//可见景观布局
//隐藏人像布局
}else if(newConfig.orientation==Configuration.orientation\u纵向){
//可视纵向布局
//隐藏景观布局
}
}
Biraj,感谢您的输入!我照你说的做了。结果的性能几乎与它本身相同。在尝试您的方法之后,我可以得出一个结论:WebView创建需要更长的时间[~=1700到2000毫秒]。不幸的是,WebView无法在非ui线程中初始化。所以,寻找其他的选择。。。