WebView Introduction (Accelerated Loading)

Source: Internet
Author: User

Starting with Android 3.0, Android's 2D rendering pipeline provides better support for hardware acceleration. Hardware acceleration uses the GPU for drawing operations on the view.

Hardware acceleration can be turned on or off at four levels:

    • Application
    • Activity
    • Window
    • View
Application level

You can turn on hardware acceleration for the entire application by adding the following attributes to your application Androidmanifest.xml file for the application tag:

<application android:hardwareaccelerated= "true" ...>
Activity level

You can also control whether each activity turns on hardware acceleration, just add the android:hardwareaccelerated attribute to the activity element. For example, turn on hardware acceleration at the application level, but turn off hardware acceleration on an activity.

<application android:hardwareaccelerated= "true" > <activity .../> <activity android:hardwareaccelerat Ed= "false"/></application>
Window level

If you need more granular control, you can use the following code to turn on hardware acceleration for a window:

GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_ hardware_accelerated);

Note: Hardware acceleration cannot be turned off at the window level at this time.

View level

You can turn off hardware acceleration for a single view at run time with the following code:

Myview.setlayertype (view.layer_type_software, NULL);

Note: You cannot turn on hardware acceleration at the view level

Why do you need so many levels of control?

Obviously, hardware acceleration can lead to performance gains, why does Android need to make so many levels of control, rather than the default is all hardware acceleration? The reason is that not all 2D drawing operations support hardware acceleration, and programs may not work properly if you use custom views or draw calls in your program. If you only use the standard view and drawable in your program, be sure to turn on hardware acceleration with confidence! Specifically, which drawing operations do not support hardware acceleration? The following is a drawing operation that is known to not support hardware acceleration:

  • Canvas
    • Clippath ()
    • Clipregion ()
    • Drawpicture ()
    • Drawpostext ()
    • Drawtextonpath ()
    • DrawVertices ()
  • Paint
    • Setlineartext ()
    • Setmaskfilter ()
    • Setrasterizer ()

    There are also some drawing operations that turn on and do not turn on hardware acceleration, with different effects:

  • Canvas
    • ClipRect (): XOR ,Difference和ReverseDifference裁剪模式被忽略,3D变换将不会应用在裁剪的矩形上。
    • Drawbitmapmesh (): Colors array ignored
    • DrawLines (): Anti-aliasing not supported
    • Setdrawfilter (): Can be set, but no effect
  • Paint
    • Setdither (): Ignore
    • Setfilterbitmap (): Filter always on
    • Setshadowlayer (): Can only be used on text
  • Composeshader
    • Composeshader can only contain different types of shader (such as a bitmapshader and a lineargradient, but not two Bitmapshader instances)
    • Composeshader cannot contain composeshader

    If the application is affected by these effects, you can call in the affected sectionsetLayerType(View.LAYER_TYPE_SOFTWARE, null),这样在其它地方仍然可以享受硬件加速带来的好处

The drawing model of Android

When hardware acceleration is turned on, the Android framework takes on a new drawing model. The software-based drawing model and the hardware-based drawing model are different?

Software-based drawing model

Under the software drawing model, the view is drawn in the following two steps:

1. Invalidate The hierarchy (note: Hierarchy how to translate?) )

2. Draw the Hierarchy

The application calls invalidate () to update a portion of the UI, and the defunct (invalidation) message is passed through the entire view layer, calculating each area (that is, the dirty area) that needs to be redrawn. The Android system will then redraw the view with the intersection of all the dirty areas. Obviously, there are drawbacks to this drawing pattern:

1. Unnecessary code is executed in each drawing operation. For example, if the application calls invalidate () to redraw the button, and the button is on another view, it will redraw even if the view does not change.

2. Bugs that may obscure some applications. Because the Android system redraws the view that intersects the dirty area, the contents of the view may be redrawn without calling invalidate (). This may cause a view to be dependent on other view failures to get the correct behavior.

A hardware-based drawing model

The Android system still uses invalidate () and draw () to draw the view, but it differs in processing the drawing. The Android system records the draw command to the display list instead of immediately executing the draw command. Another optimization is that the Android system simply records and updates the view labeled Dirty (via Invalidate ()). The new drawing model consists of three steps:

1. Invalidate the Hierarchy

2. Record and update the display list

3. Drawing the Display list

Layer_type_software
There will be a bitmap (software LAYER) regardless of whether hardware acceleration is turned on, and soft rendering of webview on top.
Benefits:
to animate, use software to draw a view tree only once, which is very provincial.
When not to use:
The view tree is not used when it is updated frequently. Especially when hardware acceleration is turned on, each update consumes more time. Because after rendering this bitmap, it is necessary to render this bitmap to the hardware layer.


layer_type_hardware
When hardware acceleration is off, it works with software.
Hardware acceleration is rendered on FBO (Framebuffer Object) when it is turned on, and the view tree only needs to be drawn once when animating.


Differences :
1, one is rendered to bitmap, and one is rendered to FB.
2, hardware may have some operations not supported.
The same:
all open a buffer and draw the view to this buffer.


layer_type_none
This is easier, and does not create a separate LAYER for this view tree


PS: The Glsurfaceview and webview default Layertype are none.


Glsurfaceview:
Set Glsurfaceview to software or hardware and find nothing to draw. Conclusion: The Glsurfaceview layer type can only be none


WebView:
Previously encountered an issue with WebView, if animation is used on WebView, The painting area of WebView is not moving. The solution at the time was to take a screenshot of the WebView before animating it (Drawingcache). According to the above reason to try, set a hardware or software layer is OK.

Now another problem, after turning on hardware acceleration, on some machines (mine is 3.2) WebView Sometimes there is an area white screen problem. The default layer type is none, and hardware is not possible, and setting it to software is resolved. Of course, hardware acceleration is turned off, but then the overall program is slower. So the final solution is the overall hardware acceleration, the problem of the WebView setup software

Added on 2012.4.21:

Add this sentence to make the 3D draw faster: Getholder (). SetType (Surfaceholder.surface_type_hardware);

Added on 2012.4.22

First, the question:
When hardware acceleration is turned on, Glsurfaceview is removed from the view tree, causing the entire window background to darken, even if the layer type is set to software.
After two days of troubleshooting, found the reason, my program is in the C layer by drawframe (belonging to the glthread thread) to drive the painting, when Glsurfaceview was taken off, Glsurfaceview destroy method is called, I called the end method of the Glthread thread directly in the Destroy method (which belongs to the UI thread). While Glsurfaceview.creat,sizechanged,destroyed is on the UI thread, Render.create,sizechanged,drawframe is on the glthread thread. Therefore, there is a problem with the way that the UI thread calls the Glthread thread directly. Finally, the problem is resolved by sending runnable through glsurfaceview.queueevent to the glthread thread.
It seems that the soft rendering of the fault-tolerant ability, a hardware acceleration, the bottom is more fragile.
Conclusion: Be sure to figure out which is the UI thread and which is the glthread thread.

Fill in several points of knowledge found in the process of finding a problem:
Hardware acclerator is the acceleration of the entire window, View.ishardwareacclerator returns True when hardware acceleration is turned on. However, each view may be rendered to a different Canvas, such as the view may be set by Setlayer layer, then canvas.ishardwareaccelerator return False
Android provides three types of hardware acceleration whether the control level is turned on, respectively, Application,activity,window,view. This can be referred to the dev Guide

When we use WebView, if the load of netizens is larger, this loading speed will be very slow.

This paper summarizes several methods of accelerating webview loading 1, improving the priority of rendering webview.getsettings (). setrenderpriority (Renderpriority.high); 2, Use Webview.getsettings (). Setblocknetworkimage to load the image and load the rendering at the end. Referring to example 1.3, using hardware acceleration, this feature was added to Android 3.0 (API level 11). Specific reference: http://developer.android.com/guide/topics/graphics/hardware-accel.html Example 1:

WebView Introduction (Accelerated Loading)

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.