Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中,如何提高方向改变时内容视图加载的性能?_Android_Performance_Optimization_Android Webview_Android Orientation - Fatal编程技术网

在Android中,如何提高方向改变时内容视图加载的性能?

在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);

我有一个教程应用程序,可以加载教程页面列表[这些页面是网页]。这些页面占据了整个屏幕,菜单栏的显示速度约为100dp。虽然不能共享源代码,但下面是教程活动的伪代码

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:

  • 我必须在WebView上添加几个按钮,因此我们使用RelativeLayout作为页面包装器

  • 我不能选择适配器[PagerAdapter for ViewPager],因为它会导致webview在加载时突然闪烁其内容,这是我们无法承受的。因此,CustomHorizontalRollView对我们来说是必须的

  • 我将在scrollview中添加大约150多页

  • 我有不同的网页分别为landsape和肖像模式。因此,我需要在方向改变时重新创建。此外,每个屏幕方向的视图大小也不同

  • 我没有覆盖OnConfiguration更改(..)。i、 在方向变化上,活动生命周期从新开始

  • 问题:

    在方向更改时,加载页面内容需要2-3秒。而且,这个应用程序似乎很晚才检测到方向变化


    请建议如何提高方向更改时快速加载的性能。

    您可以做一件事,创建一个包含方向横向和纵向设计的布局文件。并覆盖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线程中初始化。所以,寻找其他的选择。。。