Wheat Academy Android developed Android App Development View optimization steps

Source: Internet
Author: User

1) ViewOptimizedI. Reduce unnecessary view and the nesting level of the view. For example, to implement a ListView commonly used in layout, you can use relativelayout to reduce nesting, to know that each view of the object will consume 1~2k memory, too many nesting levels will cause frequent GC, resulting in ANR. II. View the structure of the view using Hierarchyviewer to view the layout structure by Hierarchyviewer: ~/tools/hierarchyviewer, it is clear to see the flat structure below the relativelayout, This will speed up the rendering of the DOM. III. Optimize layouts with layoutopt through the layoutopt command under the Tools directory in the Android SDK to see if your layout needs to be optimized.

2)
multithreading to solve complex computationsMore CPU-intensive data operations are carried out as far as possible in a single thread, and the results of execution are presented to the UI thread through handler and other means. Especially for network access, database queries, and complex algorithms. Currently Android provides a combination of asynctask,hanlder, message, and thread. For multi-threaded processing, if there are many concurrent threads and frequent creation and deallocation, you can resolve the thread-created efficiency bottlenecks through the thread pool of the concurrent class. It is also worth noting that in the application development of the custom view, the interactive part, do not write threads constantly refresh the interface display, but based on the Touchlistener event active trigger interface update.
3)layout withJavaComplete thanXMLFastIn general, the layout of the Android program is often written using XML files, which can improve the development efficiency, but in view of the security of the Code and execution efficiency, can be created through Java code, although the Android compiled XML is binary, But the efficiency of loading an XML parser is much more efficient for resource use, and Java processing is a lot faster than XML, but for a complex interface to write, it may require some nesting considerations, and if you're flexible, it's a better way to use Java code to lay out your Android application.

4)
zoom in on a large picturePicture reading is an oom (out of Memory) regulars, when on the Android phone directly read 4M pictures, death will come, so often the photos taken by their mobile phone can not be directly read. We often use the Bitmapfactory class when scaling images for large images, and the stack size of the images in the virtual machine is only 8M when the bitmap is read in the Android system bitmap. This error is sometimes encountered when decoding a picture with bitmapfactory. This is often caused by a large picture. At this point we need to allocate less memory space for storage. BitmapFactory.Options.inSampleSize Setting the appropriate insamplesize allows the bitmapfactory to allocate less space to eliminate the error. Android provides a dynamic calculation, as follows:
Look at the size of the picture before reading it:
bitmapfactory.options opts = new Bitmapfactory.options ();  opts.injustdecodebounds = true; Bitmap Bitmap = Bitmapfactory.decodefile (ImageFile, opts);

Use the resulting image to calculate the original width and height to fit your smaplesize
bitmapfactory.options opts = new Bitmapfactory.options (); opts.insamplesize = 4;//4  represents the capacity into the previous capacity of the Bitmap Bitmap = Bitmapfactory.decodefile (ImageFile, opts);

Be sure to recycle the obsolete bitmap object in a timely manner, and assign the object a value of NULL.
bitmap.recycle (); bitmap = null;


5)
Reasonable UseviewstubViewstub is a hidden, non-memory-intensive view object that can delay loading a layout resource file at run time. This layout resource file is loaded when viewstub is visible, or when the inflate () function is called. The viewstub replaces itself in the parent container when the view is loaded. Therefore, viewstub is always present in the view until setvisibility (int) or inflate () is called. The layout parameters of the viewstub are added to the Viewstub parent container along with the number of views loaded. Similarly, you can also define or rename the ID value of the View object to be loaded by using the Inflatedid property. So we can use the Viewstub delay to load some more complex layout, dynamically load the view, use viewstub to avoid some infrequently view long-term hold the reference.

6)
forListViewThe performance optimizationI. Multiplexing Convertview. II. In Getitemview, determine whether the Convertview is empty, if not empty, can be reused. If the view in Couvertview needs to add Listerner, the code must be outside the if (Convertview==null) {}. III. Asynchronously loading a picture, if it contains a Web image, it is best to load asynchronously. Iv. do not display the picture when fast sliding the list (scroll_state_fling), the picture in item or the view that needs to consume resources, can not be displayed, but in the other two states (Scroll_state_idle and Scroll_ State_touch_scroll), the view is displayed. V. Item minimizes the level of controls and layouts used, the background color is set to the same color as the Cachecolorhint, and the layout of item in the ListView is critical, and you must minimize the use of controls and layouts. Relativelayout is an absolute tool through which you can reduce the level of layout. Reuse the control as much as possible, which reduces the memory usage of the ListView and reduces the number of GC times when sliding. The background color of the ListView is set to the same color as the cachecolorhint, which improves rendering performance when sliding. Vi. GetView Optimization in the ListView GetView is the key to performance, and here to optimize as possible. In the GetView method, it is not possible to do complicated logical computation in the View;getview method, especially the database and network access operation, otherwise it will seriously affect the performance of sliding.
7)otherI. Resolution adaptation-LDPI,-MDPI,-HDPI configuration of different precision resources, the system will be based on the device adaptive, including drawable, Layout,style and other different resources. II. Use DP (density independent pixel) as much as possible, without PX (pixel).   III. Multi-use wrap_content, Fill_parentiv.   Abandon Absolutelayoutv.   Using 9patch (Launch application via ~/tools/draw9patch.bat), PNG format VI. Use <merge> to optimize the number of layout layers, using <include > to share layouts. VII. Set the background of the window in acitivity to empty. GetWindow (). setbackgrounddrawable (null); The default background for Android is not empty. The cache properties are set in Viii. view. Setdrawingcache is true.


Read more about Android development TechnologiesWheat Academy(www.maiziedu.com)

The Android Application Development View Optimization step of the Wheat Academy Android development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.