问题
I'm trying to use the hardware acceleration for Android with my canvas. I used to have a SurfaceView
which I did lockCanvas()
on to get a canvas which I later draw on, but I changed to TextureView
since I couldn't get SurfaceView
to use hardware acceleration. I'm currently trying to get this canvas to use hardware acceleration.
Canvas canvas = this.lockCanvas();
System.out.println(this.isHardwareAccelerated() + ", " + canvas.isHardwareAccelerated());
Gives me the output: true, false
(this
is a TextureView
)
Does anyone know why the canvas
is not hardware accelerated, and how to make it so?
Edit: As far as I have found, it seems that I have to use OpenGL. However, I would still like to know if there are any announced plans to make hardware acceleration possible for such a canvas.
回答1:
TextureView
works only when the application is hardware accelerated, but the Canvas
it returns from lockCanvas()
is currently never hardware accelerated. This means that you will draw inside the TextureView
's Canvas
in software, but TextureView
itself will be drawn using the GPU. Currently, the only way to get a hardware accelerated Canvas is to use the onDraw(Canvas)
method of a View.
回答2:
Check the Hardware Acceleration article.
Basically if you want your TextureView
to be hardware accelerated you must make sure that hardware acceleration is enabled at some level in the context of your TextureView, i.e one of the following:
- At application level:
<application android:hardwareAccelerated="true" ...>
- At activity level:
<activity android:hardwareAccelerated="true" />
- At window level:
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Note that hardware acceleration is actually mandatory to use a TextureView:
TextureView can only be used in a hardware accelerated window. When rendered in software, TextureView will draw nothing.
来源:https://stackoverflow.com/questions/9966228/android-textureview-hardware-acceleration-with-lockcanvas