Android Graphics and hardware acceleration

Source: Internet
Author: User

Today, when studying Android-pulltorefresh , I suddenly find that there is a android:hardwareaccelerated= "true" in the manifest file. Not seen before, decisive Baidu, found that the original is related to hardware acceleration, see Google Developer documentation

Since Android3.0, Android's 2D pipeline has been designed to support hardware acceleration. Hard acceleration uses the GPU to take on all the drawing operations performed on the view's canvas. At the same time, such applications will consume more memory.

The simplest way to enable hard acceleration is to enable hardware speed for the entire application. If your app uses only standard view and drawable, global enable hard acceleration will have no negative impact. However, because hard acceleration is not supported by all 2D drawing, enabling it can have an impact on your custom drawing. The problems that arise are often not visible, may be anomalies, or incorrectly display pixels. To avoid these problems, Android provides the ability to enable or disable hard acceleration at the following levels:

Application | | | |     Activity | | | |     Window | | | | View

Application Level

In the app's Android manifest file, add the following attributes to the <application> element to turn on hardware acceleration for the entire application.

< applicationandroid:hardwareaccelerated = "true"  ... >

activity Level

If the application does not correctly use the global hardware acceleration that is turned on, the activity can also be controlled separately. Use the android:hardwareaccelerated attribute in the <activity> element to enable or disable hardware acceleration at the activity level. The following example enables global hardware acceleration, but disables hardware acceleration for an activity:

< applicationandroid:hardwareaccelerated = "true" > < ... /> < activityandroid:hardwareaccelerated = "false" /> </ Application >

window Level

If finer-grained control is required, you can use the following code to enable hardware acceleration for a given window:

Note: Hardware acceleration cannot be disabled at the window level at this moment.

GetWindow (). SetFlags (Windowmanager.layoutparams.flag_hardware_accelerated,windowmanager.layoutparams.flag_ hardware_accelerated);

View level

You can use the following code to suppress hardware acceleration at run time against a separate view object:

Note: Hardware acceleration cannot be turned on at the view level at this moment. View Layer In addition to prohibit hardware acceleration, there are other features, more information please see the "View layer" of this article.

Myview.setlayertype (View.layer_type_software,null);

Determine if a view can be hard-accelerated

Sometimes an app is useful to know if hardware speed is enabled, especially for things like custom view. This is even more important when your application does some custom drawing that is not supported by the latest pipeline.

There are two ways to check whether an app is hard-accelerated:

View.ishardwareaccelerated (); // returns True if the view is attached to a hard-accelerated window.  canvas.ishardwareaccelerated (); // returns True if the canvas is hard-accelerated. 

If you have to do this in your drawing code, you should use canvas.ishardwareaccelerated () instead of view.ishardwareaccelerated (). When a view is attached to a hard acceleration window, it can still draw using a non-hardware-speed canvas. For example, when a view is drawn to a bitmap in order to cache it.

Android Graphics and hardware acceleration

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.