Hardware acceleration is available with Android 3.0 (API level 11).
From Android 4.0(API level 14) it is enabled by default.
Hardware acceleration can be controlled at the following levels:
Application
In the Androidmanifest.xml, indicating true
or false
to the attribute android:hardwareAccelerated
, in section <application/>
:
<application android:hardwareAccelerated="true" ...>
Activity
In the Androidmanifest.xml, indicating true
or false
to the attribute android:hardwareAccelerated
, in section <activity/>
:
<application android:hardwareAccelerated="true">
<activity ... />
<activity android:hardwareAccelerated="false" />
</application>
Window
Using the method setFlags()
Window to enable hardware acceleration:
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Hardware acceleration cannot be disabled at this level.
View
Using the method setLayerType()
of View to disable hardware acceleration:
myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Hardware acceleration cannot be enabled at this level.
There are two ways to know if an application is hardware-accelerated:
Please note that before using the method you must be assured that the View is associated with a Window, what is not the case for example in onCreate().
A place where you have this guarantee is the onWindowFocusChanged() of Activity.
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus){
boolean isAccelerated = view.isHardwareAccelerated();
}
}
For more detailed information see the topic Hardware Acceleration in the documentation.
Not,
getWindow()
is an Activity method, it returns the Window of that Activity.– ramaral
You say "Both ..." but then do not materialize, I suppose you meant "both ..., even though I have... ...., return false". I don’t know if these methods are correctly emulated. You may have to test on physical devices. Note that it is something I never used, the answer was based on documentation.
– ramaral
Please note that before using the method the view has to be associated with a Window. See the edit of the answer.
– ramaral
I tested the example I posted, using the
onWindowFocusChanged()
, and it worked.– ramaral
I don’t know where or how you intend to use the methods but I do know that the
Canvas.isHardwareAccelerated()
within the methoddraw()
of a View also works.– ramaral
Ramaral I’m beginning to believe that the problem is not acceleration, I get this error
GLES2DecoderImpl::ResizeOffscreenFrameBuffer failed to allocate storage for offscreen target depth buffer
, this when the acceleration is configured in the manifest like this<activity android:hardwareAccelerated="false" />
and in case only occurs if I use awebView.loadUrl
. I will mark this answer as correct, as it seems that both problems are unrelated. Please see this other question http://answall.com/q/163926/3635– Guilherme Nascimento
Just a correction I wrote this
<activity android:hardwareAccelerated="false" />
, but I actually wanted to say this<activity android:hardwareAccelerated="true" />
, would not have felt thefalse
in most of the tests I did. Thanks again. A note, the question about webView I changed.– Guilherme Nascimento