Summary of Android hardware acceleration related issues
Since Android 3.0, hardware acceleration has been supported, making full use of the characteristics of GPU to make the drawing of View smoother. For example, using hardware acceleration in ListView (GridView) or WebView will make the page smoother.
hardware acceleration switch
It is turned off by default and can be turned on in the four levels of Application, Activity, Window, and View.
Application
Specify android:hardwareAccelerated="true" in the application tag in the Manifest
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:hardwareAccelerated="true"
android:label="@string/app_name"
android:theme="@style/AppTheme">
Activity
Specify android:hardwareAccelerated="true" in the activity tag in the Manifest
<activity
android:name=".presentation.ui.activity.MainActivity"
android:label="@string/app_name"
android:hardwareAccelerated="true"
android:screenOrientation="portrait" />
You can also turn on hardware acceleration in the Application and turn it off in the activity, just specify android:hardwareAccelerated="false" in the activity tag.
Window
getWindow().setFlags( WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
View
mView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Related questions
In actual projects, we often use hardware acceleration at the Application level or at the Activity level, hoping to get a better operating experience. In fact, hardware acceleration can indeed make our programs run more smoothly. However, Android's support for hardware acceleration is not perfect, and some drawing operations cannot work properly when hardware acceleration is turned on.
Flashes when the page is drawn
UI controls display abnormally
WebView loading exception
When encountering the above problems, you can consider whether hardware acceleration is enabled in the program, which is caused.
Set properties on the specified page
android:hardwareAccelerated="false"
Specify the hardware acceleration properties of a View
mView.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
WebView uses hardware to greatly improve the loading efficiency, so it is recommended
onPageStart()
to usemView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
it in theonPageFinish()
callback and use it in the callbackmWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
.