我正在为Android佩戴开发Watch Face。我想让Canvas硬件加速。
在清单中我声明它像吼叫
<application
android:hardwareAccelerated="true"
..>
<service
android:name=".WatchFaceMain"
android:hardwareAccelerated="true"
..>
</application>
但是当我从My onDraw(Canvas canvas,Rect bounds)内部检查它时,canvas.isHardwareAccelerated()返回false。这显然意味着我的画布不是硬件加速的。
@Override
public void onDraw(Canvas canvas, Rect bounds) {
...
boolean isAccelarated = canvas.isHardwareAccelerated();
.......
}
如何使此画布硬件加速?
答案 0 :(得分:2)
终于找到了解决这个问题的方法。虽然这不是一个直接或简单的解决方案。
首先,我从librar复制整个android.support.wearable.watchface.CanvasWatchFaceService类,我的WatchFaceMain类扩展了它。
然后我必须编辑Engine类中的draw()方法,如bellow
public abstract class CanvasWatchFaceService extends WatchFaceService {
.....
public class Engine extends android.support.wearable.watchface.WatchFaceService.Engine {
...
private void draw(SurfaceHolder holder) {
this.mDrawRequested = false;
Canvas canvas = holder.getSurface().lockHardwareCanvas();
if(canvas != null) {
try {
this.onDraw(canvas, holder.getSurfaceFrame());
} finally {
holder.getSurface().unlockCanvasAndPost(canvas);
}
}
}
}
}
我在onDraw中编辑了两行,如下所述。
from
Canvas canvas = holder.lockCanvas();
To
Canvas canvas = holder.getSurface().lockHardwareCanvas();
And From
holder.unlockCanvasAndPost(canvas);
To
holder.getSurface().unlockCanvasAndPost(canvas);
我仍然想知道为什么CanvasWatchFaceService没有提供任何更简单的方法来请求硬件加速画布!
More detail solution is available here in this Article.
希望这个答案对其他人也有帮助。