2017-05-16 58 views
0

我正在開發面向android Wear的Watch Face。我想讓Canvas硬件加速。如何在CanvasWatchFaceService的onDraw()中獲取硬件加速Canvas?

在清單中,我宣佈它像波紋管

 <application 
     android:hardwareAccelerated="true" 
     ..> 
      <service 
      android:name=".WatchFaceMain" 
      android:hardwareAccelerated="true" 
      ..> 
    </application> 

但是,當我從我的onDraw內檢查的話(帆布油畫,矩形邊界)方法canvas.isHardwareAccelerated()返回false。這顯然意味着我的畫布不是硬件加速的。

@Override 
public void onDraw(Canvas canvas, Rect bounds) { 
     ... 
     boolean isAccelarated = canvas.isHardwareAccelerated(); 
     ....... 
} 

如何使此畫布硬件加速?

回答

2

終於找到了解決這個問題的辦法。雖然這不是一個簡單或簡單的解決方案。

首先我需要從Librar中複製整個android.support.wearable.watchface.CanvasWatchFaceService類,這是我的WatchFaceMain類擴展的。

然後,我必須編輯引擎類內的draw()方法等波紋管

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.

希望這個答案可以幫助別人了。