Hardware acceleration is the process to use device hardware to speed up drawing operations of Android application. In other words, android use hardware acceleration to speed up 2D rendering or speed up the image and video rendering.
It is by default enabled if your Target API level is >=14. You can also enable and disable it according to requirements. The application can deliver faster and smoother animations if you have enable it. It directly affects the rendering and scrolling behaviour of views. As all 2D drawing Operations are not supported by hardware acceleration, it might affect some of your custom views.
Levels to Control Hardware Acceleration.
Hardware acceleration can be controlled at the following levels:
- Application
- Activity
- Window
- View
Application level :
We can enable hardware acceleration at the application level by adding “android : hardwareAccelerated” attribute to the application tag.
For Example :
1 |
<application android : hardwareAccelerated="true" ...> |
Activity Level :
Similarly, we can enable or disable hardware acceleration at the Activity level by using same attribute as we using for the application level.
For Example:
1 |
<activity android:hardwareAccelerated="true" .../> |
If you are facing an issue in any activity by enabling hardware acceleration globally, you disable it only for that activity.
For Example :
1 2 3 4 5 6 |
<application android :hardwareAccelerated="true"..> // enable at application level <activity ... /> <activity android : hardwareAccelerated="false" /> //disable for particular activity <activity ... /> </application> |
Window Level :
If you want a hardware acceleration window only, then you can do this by using “FLAG_HARDWARE_ACCELERATED” flag.
For Example :
1 2 3 4 |
window.setFlags( WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED ) |
Note :
At window level you can only enable the hardware acceleration but currently you can’t disable it.
View Level:
At View Level we can only disable hardware acceleration by setting layer type to software of any view.
For Example :
1 |
splashImage.setLayerType(View.LAYER_TYPE_SOFTWARE, null) |
Note:
If you have disabled hardware acceleration in your app then “LAYER_TYPE_HARDWARE” layer type behaves same as “LAYER_TYPE_SOFTWARE”.
Hardware Acceleration Enable / Disable access on different level :
- Application: Here we can enable and disable hardware acceleration.
- Activity: Enable and Disable
- Window: Only enable
- View: Only Disable.
The problem is Due to Hardware Acceleration in some Devices.
A Common crash that you may face is if you have enabled hardware acceleration in you app and using a large image.
1 2 |
java.lang.RuntimeException: Canvas: trying to draw too large(157286400bytes) bitmap. at android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:229) |
Sometimes you may face this issue on splash screen when you are using large size of the splash screen (ex: 1280*1920 ) on some devices like Samsung Galaxy S7 or any Hawaii device.
Solution :
1. Disable hardware acceleration. You can disable Hardware acceleration at any level, but try to disable it only for image splash images (view level)
For Example :
1 2 |
Image splashImage= view.findViewbyId(R.id.splash_image) splashImage.setLayerType(View.LAYER_TYPE_SOFTWARE, null) |
2. You can also reduce/decrease the size of the image.
3. Don’t put your splash image directly into the draw-able folder. Put your image into a folder according to resolution like.. drawable-xxdpi.
Determine view is hardware accelerated or not :
1. View.isHardwareAccelerated() returns true if the View is attached to a hardware accelerated window.
2. Canvas.isHardwareAccelerated() returns true if the Canvas is hardware accelerated
To get more info please visit developer official site : https://developer.android.com/guide/topics/graphics/hardware-accel
Conclusion
In this blog we learn importance of hardware acceleration in android and how it will effects our apps.