Android development skills 2

Source: Internet
Author: User
Tags image processing library

Previous: http://www.bkjia.com/kf/201202/118959.html


7. Android ImageButton Problems

Many users have questions about ImageButton provided by Android. When a Drawable image is displayed, text is no longer displayed. There are two solutions, the first method is to write text in the image, but this solution increases the size of the program, and the hard encoding method affects the publishing of multiple languages. The second solution is very simple. Through analysis, we can see the layout of ImageButton. We can directly inherit it and add a TextView. The alignment mode is the right side to enable ImageButton to support text display on the right side.

8. Android code optimization technology

1. Java memory control

For String operations, we recommend using StringBuilder if you need to add strings. After debugging, it is not difficult to find that if your strings are added repeatedly, the memory sales required by using String will be much larger than that required by StringBuilder, the general running memory of the Android mobile phone is about mb. You need to consider how to run multiple tasks, android Development Network prompts that Because Java GC does not need to be manually released, you must be extremely careful when assigning the GC. Frequent GC operations still affect the performance, during debugging, we can use logcat to view the memory release status.

2. Recycling

When accessing an attribute, the efficiency is far lower than that of a fixed variable. If the number of cycles is usually greater than 5, suppose xxx. the value of the GetLength () method is generally greater than 5. It is recommended to write this statement, for example

For (int I = 0; I <xxx. GetLength (); I ++)

Xxx. GetLength must be called in every loop, which will inevitably affect the program efficiency and become more obvious in game development. The improved method should be

Int j = xxx. GetLength ()

For (int I = 0; I <j; I ++)

3. Image Optimization

BitmapFactory, a two-dimensional image processing library on the Android platform, is more intelligent. To reduce file size and efficiency, many small images are often placed in one image instead of many resource files, this is done by slicing. In j2s, we solve the problem of MIDP devices by reducing the number of file headers. In Android, although the hardware configurations of models are relatively high, for Android G1 hardware configuration, refer to G1 mobile phone parameters and evaluation. However, when there are many resources, the operation efficiency is satisfactory. At least Dalvik is not optimized enough.

9. NIO non-blocking package for Android development (1)

To improve the network communication performance of Android, we can use the high-performance NIO (New I/O) technology on Java for processing. NIO was introduced from JDK 1.4, we can understand the N of NIO as Noblocking, which means non-blocking. It is blocking relative to traditional I/O methods, such as Socket accpet () and read.

NIO mainly uses Channel and Selector for implementation. Java Selector is similar to Winsock's Select mode and is event-driven. The whole processing method uses the rotation state machine, if you have developed a Symbian application in the past, this method is a bit like an active object. The advantage is that a single thread saves system overhead and NIO can handle concurrency well, it is critical for Android online game development. using NIO for Multi-Point Socket connections can greatly reduce thread usage and reduce the probability of thread deadlock. After all, mobile games have UI threads and music threads, network threads and management difficulties can be imagined. At the same time, low-speed devices such as I/O will affect the gaming experience.

NIO, as a medium-and high-load I/O model, has greatly improved compared with the traditional BIO (Blocking I/O), so there is no need for too many threads to process concurrency, saves the creation and destruction time. If too many threads are scheduled, and many threads may be idle, the CPU time is greatly wasted. At the same time, too many threads may be greatly reduced in performance, in general solutions, thread pools may be used to manage scheduling, but this method is not a permanent cure. Using NIO can greatly improve the concurrency efficiency. Of course, there are still some differences between NIO and AIO in JDK 7. AIO is updated. Of course, this is for Java. If you have developed a Winsock server, the I/O completion port such as IOCP can solve more advanced loads. Of course, today Android123 mainly explains why NIO is used in Android.

NIO is described in several types. As a feature of Java, we need to understand some new concepts, such as ByteBuffer class, Channel, SocketChannel, ServerSocketChannel, Selector, and SelectionKey. For specific usage, the Android development network will be explained in detail tomorrow. Users can see java. nio and java. nio. channels in the Android SDK documentation.
Http://www.bkjia.com/kf/201202/119038.html

Take a look at this technology and see if you can use it in the project you are about to do.

 

From LuoXianXiong, your partner

Related Article

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.