Android development skills 1

Source: Internet
Author: User

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

Some netizens recently asked how to convert pixel pixels into independent dip device pixels. Due to the large number of Android device resolutions, wvga is currently the mainstream, while many old devices use 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?

For example, if you want to develop a network application, 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 ConnectivityManagercwjManager = (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-permission 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 (ConfigurationnewConfig) method is reloaded in the Java file of the Activity, so that the onCreate and other methods are 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
}
}

 

From LuoXianXiong, your partner

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.