Android supports the technical principle of the technical principle of the ASYNC Layout Inflator in the Java library
Android supports the technical principle of the technical principle of the ASYNC Layout Inflator in the Java library
introduction:
The layout in Android is an XML file for defining the user interface, but when the layout file is more complex or the amount of data is large, the layout filling (Layoutinflator) will occupy a lot of time and memory resources.To solve this problem, the Android support library provides an asynchronous layout of the Async Layout Inflator, which is carried out in the background thread to improve the loading speed and performance of the user interface.
The principle of asynchronous layout Fill framework:
1. Caller requests asynchronous loading layout: When the application needs to fill in a complex layout, it can request the laid layout by calling the inflate method of the Asynclayoutinflator class by calling the Inflate method of the Asynclayoutinflator class.
Example code:
AsyncLayoutInflater inflater = new AsyncLayoutInflater(context);
inflater.inflate(R.layout.my_layout, parent, new AsyncLayoutInflater.OnInflateFinishedListener() {
@Override
public void onInflateFinished(@NonNull View view, int resid, @Nullable ViewGroup parent) {
// The recovery method after filling is completed
// Here you can operate the filling layout
}
});
2. Layout filling in the background thread: When the INFLATE method is called, the asynchronous layout filler will put the layout task that needs to be filled in a thread pool and perform the analysis and filling process of the layout in the background thread.
Example code:
private static class InflateRequest {
public int resid;
public ViewGroup parent;
public View view;
public OnInflateFinishedListener callback;
}
private static class BasicInflater extends LayoutInflater {
// Other methods of rewriting layoutinflater to realize the logic of layout filling
...
}
private static class InflateThread extends Thread {
...
// The layout queue to be filled
private final Queue<InflateRequest> mQueue;
// The recovery method for the completion of the asynchronous layout is executed in the main thread
private final Handler mHandler;
...
@Override
public void run() {
while (true) {
InflateRequest request;
synchronized (mQueue) {
while (mQueue.isEmpty()) {
try {
mQueue.wait();
} catch (InterruptedException e) {
// Interrupt abnormal treatment
return;
}
}
request = mQueue.poll();
}
// Create a layoutinflator instance
LayoutInflater inflater = new BasicInflater(context);
inflater.setFactory(factory);
View view = inflater.inflate(request.resid, request.parent, false);
// The main thread reckle asynchronous layout filling method
Message.obtain(mHandler, REQUEST_DONE, new InflateResult(request, view)).sendToTarget();
}
}
...
}
3. Layout in the main thread: When the layout is filled, the OninflatingISHEDLISTENER interface will be adjusted in the main thread. The application can further process and operate the filling layout here.
Example code:
AsyncLayoutInflater inflater = new AsyncLayoutInflater(context);
inflater.inflate(R.layout.my_layout, parent, new AsyncLayoutInflater.OnInflateFinishedListener() {
@Override
public void onInflateFinished(@NonNull View view, int resid, @Nullable ViewGroup parent) {
// The recovery method after filling is completed
// Here you can operate the filling layout
}
});
Summarize:
Android supports library wearing layout filling framework to effectively improve the loading speed and performance of the user interface by putting the layout of filling in the background thread.By calling the INFLATE method provided by the asynchronous layout, and the layout of the filling method in the main thread, developers can better optimize the user experience of Android applications.
Note: There may be some differences or omittinders in the specific implementation of the example code. It is only used to explain the technical principles of the asynchronous layout of the filling device framework. In actual use, it is necessary to adapt and adjust according to the actual situation.