Through such a long box development and experience in the previous mobile phone project, the overall feeling of the two different devices still have a lot of different places, the first difference is that the mobile phone project and TV project default start page loading speed has important differences
For mobile phone: Mobile phone load network data, because the screen is small, if the home page has a network picture, the basic is to display the default picture, which is also due to the speed limit, more importantly, the phone is basically a picture mixed, the user did not see the image may focus on the text.
For TV: If the application first page load using the default diagram, will feel particularly ugly, because the screen is large, important information is the picture, if there is no picture, the user sees is blank, the user's focus is not, only wait and complain.
Therefore, it is important to load the TV homepage.
2.0 for the above situation, is to add a black view cover the bottom of the view, waiting for the first page carousel map data loading complete Let it disappear, but the black screen time is too long. Here are the recommended optimizations for black screen time
Through the previous experience of mobile Project list optimization, Viewpager optimization and other project optimization, we have made some optimizations to 2.0, which mainly have the following points:
1, if there is no network image of the hard disk cache, the picture should be downloaded in the application startup process has started, such as Blu-ray, is in the launcher after the start of the broadcast to Blu-ray, Blu-ray started preloaded. (for 2.0 did not do so, can do technical research is feasible, just a suggestion)
2, if there is a local network picture cache, first the picture from the hard disk cache to the memory cache, the reason for the TV, the millisecond must contend ah, relative to the picture object from file to bitmap/drawable decode process is quite slow, small picture hundreds of MS, large figure to 1s to ns, so, This is a very important step.
If it is not the first time to start launcher, the home image is already loaded in the local, Universalimageloader this frame loading picture process is first from memory to get the picture, not to get decode local image, local no longer from the network, and our basic thinking consistent, Decode process is very slow, so in application first put the picture asynchronously loaded into memory, of course, not necessarily can be loaded, if not finished loading, wait until the execution to universalimageloader loading picture, it decode local image, The number of pictures in decode will be reduced, at least for a fraction of the time.
Before the optimization, look at the last one is the first screen large map load completion time, large map loading time:
The following is the time when the load is completed after optimization:
Total time taken from 13840ms down to 3459ms
3, through the second step of optimization, thought to reduce such a long time, total has been reduced by at least 10 seconds, the speed will be much faster, but performance and not much good, continue to analyze
First look for images before and after the download operation
The most primitive discovery was that Matrixpager.loaddata (false) was called in the OnCreate of homeactivity, and this function called Strategy.loadpagedata (isrefleshdata ); method
Loadpagedata () is a template method, strategy is a policy mode, the corresponding class has Fivepageviewstrategy and Fourpageviewstrategy two classes implemented Loadpagedata (), My box is five pages, so is the use of fivepageviewstrategy this strategy,debug found that the actual film download is the application of the image data, which is actually unreasonable, should first download the first to see the page, that is, the recommended page data
Loadpagedata uses a thread pool, and does not specify the order of the threads to execute, I changed it to Asynctask, although it is also a thread pool, but inside the thread through the deque implementation of the queue download, FIFO, let the recommended page (default startup page) First load the data, The black screen disappears only if he finishes loading the data.
4, the above step to do a good job, the homeactivity onCreate analysis, log found, onCreate from the beginning to the implementation to loaddata, the period of time spent more than 5s, see onCreatebefore LoadData, This is the log before LoadData execution, from OnCreate to execution LoadData, blocked for 5 seconds , this time is still very long.
Through the tracking code, the discovery is actually loading the layout file in the Setcontentview time-consuming, gradual follow-up, first found the Guidepageview this class, 2.0 projects in the first installation will have a guide page, The Guide page is actually just Viewpager put in homeactivity (Launcher Activity), these pictures of the Guide page is also decode from the local picture, the decode process is very slow, there is no use of the last way, Instead, the picture decode process is placed in a sub-thread, and after Decode is finished, the Viewpager is set to the picture.
such as: Optimization before log:loadimage guidepageview::init time-consuming 2751, optimized as time-consuming from 2751ms to 28ms:
OnCreate before LoadData time-consuming also reduced a lot.
This side actually hides an important point of knowledge, is not to view the two signature of the construction method to do too many things, because the layout file in the inflate process will call this construction method, the longer the logical blocking time, the longer the inflate time
5, in the Initview, there is a step to send statistical data operation, this step operation called my encapsulated device class method, such as Getsn,getmac method
These are read local files, but also time-consuming, this paragraph to get the sub-thread, log is not attached, a total of about 1s saving time
6, in the Initview, there is a step operation is time-consuming, but I can do nothing, because the 2.0 code to understand not much, participate in less, so can only give advice, the inside of the thing or to say a bit.
The following actions are available in the Initview
if (Versioncode = = 1) {
Fourpageviewstrategy forstrategy = new Fourpageviewstrategy (Matrixpager);
Matrixpager.inittabviewbyindex (2, forstrategy);
} else if (Versioncode = = 2) {
Fivepageviewstrategy fivstrategy = new Fivepageviewstrategy (Matrixpager);
Matrixpager.inittabviewbyindex (2, fivstrategy);
}
This is to determine whether the home page is five pages or four pages of the policy selection method
The following code is in the constructor:
View3dpager.addtabpageview (New Matrixappview (View3dpager.getcontext (), New Appfocusdirectioner ()), 0);
View3dpager.addtabpageview (New Matrixmovieview (View3dpager.getcontext (), New Moviefocusdirectioner ()), 1);
View3dpager.addtabpageview (New Matrixrecommendview (View3dpager.getcontext (), New Recommendfocusdirectioner ()), 2);
View3dpager.addtabpageview (New Matrixtvview (View3dpager.getcontext (), New Tvfocusdirectioner ()), 3);
View3dpager.addtabpageview (New Matrixgameview (View3dpager.getcontext (), New Gamefocusdirectioner ()), 4);
The important is not here, is the Addtabpageview in the five new operations, see one of them
Public Matrixappview (context context, Recommendviewfocusdirectionlistener listener) {
Super (context, listener);
init ();
}
protected void init () {
Code here ....
View view = Inflate (GetContext (), r.layout.matrix_app_view, NULL);
This.addview (view, params);
Code here ...
}
A cursory look at this inflate layout, no custom view, but time-consuming each two hundred or three hundred, three hundred or four hundred milliseconds, five add up, can reach two or three seconds,inflate process is very long, If these view can be instantiated before loading homeactivity or instantiated a few, similar to the image added to the memory process, may shorten the time
Summary below:
1, in addition to the operation for the view, can get the child thread to get the child thread to do the operation;
2, loading order is very important, combined with the queue to line constructor thread execution order, to ensure that the first user interface before the data load, the fastest display;
3, picture decode process is very long, if it is the first screen, it is necessary to load the picture in application first into memory, because application is the first class to load the entire application;
4, do not in the custom view construction method to do too much time-consuming operation, this will prolong the inflate process;
5, inflate process is very long, if the custom view inflate the other layout, it is necessary to put on the sub-thread inflate operation (first to ensure that there is no other view in the layout of the inflate in the construction method of the operation of the view, otherwise it will be an error).
The above can be used for reference in future projects, if there are other suggestions or questions, welcome to discuss together
If you have a different opinion or a wrong understanding, please correct it.
Android loading speed optimization, through the optimization process analysis of the project