Android development basic tutorial! Perfectly explain the implementation principle of memory cache LruCache, smart people have already collected it!

Preface

This article wants to share how to prepare for the interview with Ali and what you want from the interview process. I hope it can help you.

First of all, what may disappoint you is that this article will not contain large-scale answers to interview questions. If you want to read this aspect, you can read my previous article. Thanks for attention

One, Android basics

There are many basic knowledge points of Android, see the picture.

Suggested reading:

"Android Development Art Exploration"

1. Activity

# Activity's four startup modes, and application scenarios?

ActivityThe four startup modes:

  • standard: Standard mode, each time a new Activityinstance is generated in the active stack . Usually the activities we use are standard models.
  • singleTop: Stack top reuse, if the Activityinstance already exists on the top of the stack, then no new instance will be created in the active stack. A more common scenario is to Activityset the notification jump , because you definitely don't want to click on the notification when the front desk is Activityalready Activitythe case, and then create a similar one for you Activity.
  • singleTask: Reuse within the stack. If the Activityinstance already exists in the current stack, all Activityother Activityinstances above the current instance will be removed from the stack. Commonly used to jump to the main interface.
  • singleInstance: Single instance mode, create a new task stack, this activity instance is alone in this activity stack.

# What is the difference between onStart and onResume in Activity? The difference between onPause and onStop?

First, Activitythere are three categories:

  • Foreground Activity: Active Activity, interacting with users Activity.
  • Visible but not in the foreground Activity: The Activitybackground is usually transparent at the top of the stack , and the one below it Activityis visible but not user-interactive.
  • Background Activity: The ones Activitythat have been suspended , such as onStopmethods that have been executed .

Therefore, onStartsum onStopusually refers to the perspective of whether the current activity is in the foreground, and onResumesum onPausefrom the perspective of whether it is visible.

2. Screen adaptation

# How do I usually use screen adaptation? What is the principle?

The usual screen adaptation scheme is generally adopted by the headline screen adaptation scheme. To put it simply, one side of the screen is used as the adaptation, which is usually wide.

Principle: The relationship between device pixels pxand device independent pixels dpis

px = dp * density

Assuming that the screen width of the design drawing given by UI is based on 360dp, then the pixels of the device width are known, that is, px, dp are also known, 360dp, so you can density = px / dpmodify the densityrelevant knowledge points in the system according to this later .

3. Android message mechanism

# Introduction to Android message mechanism?

Four concepts in the Android message mechanism:

  • ThreadLocal: The data stored in the current thread can only be retrieved from the current thread.
  • MessageQueue: Message queue with time priority.
  • Looper: Polling the message queue to see if there are new messages coming.
  • Handler: Where the logic is specifically handled.

process:

  1. Preparation: create Handler, if it is created in the sub-thread, also need to call Looper#prepare()in Handlerthe constructor, binds them Looperand MessageQueue.
  2. Send message: create a message, use Handlersend.
  3. Entry MessageQueue: Because it Handleris bound to the message queue, it Messageis naturally put into the message queue.
  4. LooperPolling the message queue: It Looperis an infinite loop, always observe whether there are new messages coming, then Messagetake out the binding, Handlerand finally call Handlerthe processing logic. All this happens in the Looperloop thread, which Handlercan also process tasks in the specified thread s reason.

# Looper does not cause the interface to freeze in an endless loop in the main thread?

  1. Lead to stuck is to perform time-consuming operation resulting in dropped frames interface appears, even in Ui thread ANR, Looper.loop()this operation itself will not lead to this situation.
  2. Someone might say that setting an infinite loop in the click event will cause the interface to freeze. The same is an infinite loop, isn't it all the same? Looper will block the current thread when there is no message, release CPU resources, and wake up the main thread when a message arrives.
  3. An infinite loop is required in the App process. If the loop ends, the App process is over.

# IdleHandler introduction?

Introduction: IdleHandler is a mechanism for handling idle tasks when Hanlder is idle.

Execution scenario:

  • MessageQueueWhen there is no message, the queue is empty.
  • MessageQueueIt is a delayed message, when there is no message execution.

Will an infinite loop occur: The answer is no, MessageQueuethe method of using counting guarantees that MessageQueue#nextthe IdleHandlercollection will only be used once in a call to the method .

4. View event distribution mechanism and View drawing principle

Gang's "Android Development Art Exploration" is already very comprehensive, it is recommended to read.

5. Bitmap

# How to calculate the memory of Bitmap?

In the case that the length and width of the picture are known in pixels, the factors that affect the memory size will be the location of the resource file and the size of the pixel .

Pixel size : Common pixels are:

  • ARGB_8888: 4 bytes
  • ARGB_4444, ARGB_565: 2 bytes

Resource file location : Folders corresponding to different dpi

For example, the pixel of a picture is 180*180px, dpi(device independent pixel density) is 320, if it is only stored in drawable-hdpi, then there are:

横向像素点 = 180 * 320/240 + 0.5f = 240 px
纵向像素点 = 180 * 320/240 + 0.5f = 240 px

If it is only stored in drawable-xxhdpi, there are:

横向像素点 = 180 * 320/480 + 0.5f = 120 px
纵向像素点 = 180 * 320/480 + 0.5f = 120 px

Therefore, for a 180*180pxpicture, the device dpi is 320, the resource picture only exists drawable-hdpi, the pixel size is ARGB_4444, and the final file memory size is:

横向像素点 = 180 * 320/240 + 0.5f = 240 px
纵向像素点 = 180 * 320/240 + 0.5f = 240 px
内存大小 = 240 * 240 * 2 = 115200byte 约等于 112.5kb

# Efficient loading of Bitmap?

The efficient loading of Bitmap is also used in Glide, the idea:

  1. Get the required length and width, generally get the length and width of the control.
  2. Set BitmapFactory.Optionsin inJustDecodeBoundsto true, it can not help us in a way to get loaded into memory Bitmaplength and width.
  3. Compare the required length and width with the length and width of the Bitmap to obtain the compression ratio and put BitmapFactory.Optionsin the inSampleSizeattributes.
  4. Set BitmapFactory.Optionsin inJustDecodeBoundsto false, the image is loaded into memory, and then set up to control.

Second, Android advanced

In Android Advanced, focus on inspection Android Framework, performance optimization and third-party frameworks.

1. Binder

#Binder's introduction? What are the advantages and disadvantages of other IPC methods?

Binder is a unique IPC method in Android, quoting the words in "Android Development Art Exploration" (with slight changes):

From the perspective of IPC, Binder is a cross-process communication method in Android; Binder can also be understood as a virtual physical device, and its device driver is /dev/binder; from the beginning Android Framework, Binder is Service Managerconnected to various Managerand corresponding ManagerServiceBridge. In terms of object-oriented and CS model, Clientit Servercommunicates with remote through Binder .

Based on Binder, Android also implements other IPC methods, such as AIDL, Messengerand ContentProvider.

Compared with other IPCs:

  • High efficiency: In addition to memory sharing, other IPCs need to perform two data copies, and because Binder uses memory mapping, only one data copy is required.
  • Good security: The receiver can obtain the sent process ID and user ID from the data packet, which is convenient for verifying the identity of the sender. Other IPCs who want to experiment can only actively save it, but this may be modified during the sending process. .

At last

If you see this and think the article is well written, give it a thumbs up? If you think there is something worth improving, please leave me a message. Will surely inquire carefully and correct deficiencies. Thank you.

Finally put a benefit at the end of the article: GitHub address

PS: I have a lot of high-level Android learning video materials and interview materials in GitHub~

8F%91%E4%B8%8D%E4%BC%9A%E8%BF%99%E4%BA%9B%EF%BC%9F%E5%A6%82%E4%BD%95%E9%9D%A2%E8%AF%95%E6%8B%BF%E9%AB%98%E8%96%AA%EF%BC%81.md)**

PS: I have a lot of high-level Android learning video materials and interview materials in GitHub~

Welcome everyone to communicate and discuss together~

Guess you like

Origin blog.csdn.net/chayel123/article/details/114261157