Android Graphic System details 4: Control Hard acceleration

Source: Internet
Author: User

Previous: http://www.bkjia.com/kf/201204/126277.html

Since Android3.0 (API level11), the Android 2D display pipeline has been designed to support Hard acceleration. Hard acceleration uses GPU to perform all the painting operations on the View canvas.

 


The easiest way to enable Hard acceleration is to enable hardware speed for the entire application. if your application only uses standard view and Drawable, enabling Hard acceleration globally will not have any negative impact. however, since Hard acceleration is not supported by all 2D plotting, enabling it may affect your custom plotting. the problems are often invisible, abnormal, or incorrectly displayed in pixels. to avoid these problems, Android provides the ability to enable or disable Hard acceleration at the following levels:

Application

Activity

Window

View


If your application uses custom rendering, You need to enable Hard acceleration on the real device for testing to find out the problem. the "unsupported painting operations" section describes known painting operations that cannot be hard accelerated and how to avoid them.

 

 

Control Hard acceleration

You can control Hard acceleration at the following levels:

Application

Activity

Window

View

 

 

Application-level
In your manifest file, add the following attributes to the <application> label to enable Hard acceleration for your entire application:

<Applicationandroid: hardwareAccelerated = "true"...>

Activity-level
If your application performs incorrectly when Hard acceleration is enabled globally, you can enable Hard acceleration for individual activities. to enable or disable Hard acceleration at the actvity level, you can use the android: hardwareAccelerated attribute for the <activity> element. in the following example, Hard acceleration is enabled for the entire application but hard acceleration is disabled for an activity:

<Applicationandroid: hardwareAccelerated = "true">

<Activity.../>

<Activityandroid: hardwareAccelerated = "false"/>

</Application>

Window Level
If you need more granular control, you can use the following code to enable Hard acceleration for a window:

GetWindow (). setFlags (

WindowManager. LayoutParams. FLAG_HARDWARE_ACCELERATED,

WindowManager. LayoutParams. FLAG_HARDWARE_ACCELERATED );

 

 

Note: Hard acceleration cannot be disabled at the window level.

 


Viewlevel

You can use the following code to disable Hard acceleration for individual views during running:

MyView. setLayerType (View. LAYER_TYPE_SOFTWARE, null );

 


Note: currently, you cannot enable Hard acceleration at the View level. The View layer has other features except hard acceleration.

 

 

Determine whether a View can be hard accelerated

Sometimes it is useful for an application to know whether hardware speed is enabled. It is especially important for things like custom views. this is even more important when your application does some custom rendering that is not supported by the latest pipelines.

 


There are two ways to check whether an application 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-accelerated window, it can still be drawn using a Canvas without any hardware speed. for example, a view is drawn to a bitmap for high-speed cache.

 

From the column of nkmnkm

Related Article

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.