Some important knowledge points of Android development online [1]

Source: Internet
Author: User
Tags image processing library

1. How to run a single Android instance

We all know that the Android platform does not have a task manager, and the internal app maintainer has an activity history stack to display and destroy windows, startactivity may use the flag_activity_new_task flag to open a new window, for example, launcher. Therefore, it is easy to implement a single task, first, android123 can correct the following error. android: launchmode = "singleinstance" is added to the XML application node. In fact, this does not play any role. The history stack maintained in apps acts on the activity, android: launchmode = "singleinstance" must be added to the activity node to ensure a single instance. Of course, it is generally added to the activity in the main program startup window.

2. How to convert pixel pixels into independent pixels of DIP Devices

Recently, some netizens asked how to convert PX pixels into independent dip device pixels. Due to the large number of Android device resolutions, WVGA is currently the mainstream, and many old devices are hvga or even low-end qvga, for compatibility, dip is nothing more than convenient. It is recommended because it is irrelevant to the resolution and the screen density. Px = (INT) (Dip * density + 0.5f) // here, the android Development Network prompts many users to obtain the density (density) method, what we get from resources is static defined. Generally, 1.0 is correct for hvga, and WVGA should be obtained from windowsmanager. WVGA is 1.5.

Here, we can add some dip and SIP knowledge.

3. dynamically changing the imageview size in Android

Many netizens may find that in layout. after the absolute size of imageview is defined in the XML file, the subsequent size display cannot be dynamically modified. In fact, this problem is taken into account when the Android platform designs the UI control, to adapt to different drawable, you can add the Android: scaletype = "fitxy" line to the XML-related imageview, but the current UI may be deformed due to scaling. The premise is to restrict the layer where the imageview is located. You can use an embedded method to restrict display.

4. How can I determine whether Android phones are connected to the Internet?

If you want to develop a network application program, first consider whether to connect to the network. On Android phones, you can determine whether to connect to the network through the isavailable () method of the connectivitymanager class, first, obtain the connectivitymanager cwjmanager = (connectivitymanager) getsystemservice (context. connectivity_service);, use cwjmanager. getactivenetworkinfo (). isavailable (); to return whether it is valid. If it is true, it indicates that the current Android phone is connected to the Internet, which may be WiFi, GPRS, HSDPA, etc. Specifically, you can use getactivenetworkinfo () of the connectivitymanager class () method to Determine the detailed access method. Note that the <uses-permissio N Android: Name = "android. permission. access_network_state "> </uses-Permission> permission. The Android Development Network reminds everyone that the market and browser programs on the real machine use this method to determine whether to continue, when some network times out, you can also check whether the network connection exists to avoid wasting the power resources on the mobile phone.

5. Relationship between drawable, bitmap, canvas, and paint

Many users have just begun to learn about the Android platform. They are not very clear about the concepts of drawable, bitmap, canvas, and painting. In fact, they have already appeared in Sun's j2s apart from drawable, however, bitmap and canvas related changes in the Android platform.

First, let's understand that the display class in the Android platform is view, but it also provides the underlying graphics class android. Graphics. What we call today is the underlying graphic interface of graphics.

Bitmap is called a bitmap. Generally, the format of a bitmap file is suffixed with BMP. Of course, there are many encoders such as rgb565 and rgb888. As a pixel-by-pixel Display object, the execution efficiency is high, but the disadvantage is also obvious low storage efficiency. It is better to understand it as a storage object.

Drawable-as a common image object in Android, it can load images in common formats, such as GIF, PNG, and jpg. Of course, it also supports BMP. Of course, it also provides some advanced visualization objects, such as gradient and graphics.

Canvas-the name of the canvas can be seen as a processing process. You can use various methods to manage bitmap, GL, or path paths, at the same time, it can work with the matrix class to perform image rotation, scaling, and other operations, while the canvas class also provides operations such as cropping and selecting.

Paint-we can regard it as a drawing tool, such as a paint brush or paint brush. He manages the fonts, colors, and styles of each drawing tool.

If android game development and display effects are involved, you can use these underlying graphics classes to efficiently implement your own applications.

6. Duplicate oncreate execution due to activity Switching

Some netizens will find that the activity is switched to the background or the layout is switched from landscape screen landscape to portrait. Switching the activity again will trigger an oncreate method. We can go to androidmanifest. add the activit element in XML to this attribute Android: configchanges = "orientation | keyboardhidden", for example

<Activity Android: Name = ". android123"Android: configchanges = "orientation | keyboardhidden"Android: Label = "@ string/app_name">

At the same time, the onconfigurationchanged (configuration newconfig) method is reloaded in the Java file of the activity, so that the oncreate method is not reloaded during layout switching or window switching. The Code is as follows:

@ Override
Public void onconfigurationchanged (configuration newconfig)
{
Super. onconfigurationchanged (newconfig );
If (this. getresources (). getconfiguration (). Orientation = configuration. orientation_landscape)
{
// Land
}
Else if (this. getresources (). getconfiguration (). Orientation = configuration. orientation_portrait)
{
// Port
}
}

7. Android imagebutton Problems

Many users have a question about imagebutton provided by Android. When a drawable image is displayed, no text is displayed. There are two solutions. The first solution is to write text into the image, however, this solution increases the size of the program, and the hard encoding method affects the release of multi-language systems. 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 concatenate strings. After debugging, it is not difficult to find that if your strings are added repeatedly, the memory overhead required by using strings will be much larger than that of 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 entire 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.android123.com.cn/androidkaifa/695.html

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

10. Android theme and styles internal definition Parsing

We talked about androidmanifest yesterday. the theme method of the activity is defined in XML to implement the Untitled method. As mentioned in the article about how to use XML to make your activity untitled, many netizens do not understand why this is done, in fact, the styles style definition method has been mentioned many times in previous articles of android123. Today, Android Development Network review some netizens to learn about the internal definition of Android styles. In a project's res/values/theme. in XML, we can easily define our own style themes. For example, in the cwjtheme below, we use the background theme based on the white tone inside android. light: If windowsnotitle is set to true, no title is displayed. The background color is transparent defined in Android, and the style of the listview control is cwjlistview. The XML style code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Style name = "cwjtheme" parent = "Android: theme. Light">
<Item name = "Android: windownotitle"> true </item>
<Item name = "Android: windowbackground"> @ Android: color/transparent </item>
<Item name = "Android: listviewstyle"> @ style/cwjlistview </item>
</Style>

The custom style of the listview control is to modify the delimiter style of each row of the system listview control. Here we place an image named list_selector In the Res/drawable folder of the project, in this way, the code of our cwjlistview can be written in this way.

<Style name = "cwjlistview" parent = "@ Android: style/widget. listview">
<Item name = "Android: listselector"> @ drawable/list_selector </item>
</Style>
</Resources>

You can set more styles. For example, you can add the font color of cwjlistview to the textappearance attribute, for example, <item name = "textappearance"> @ Android: style/textappearance </item>.

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.