androidwear-oswatch-face-api

Android Wear Watch Face: onDraw() alternative in WatchFaceService?


as explained here (https://developer.android.com/training/wearables/watch-faces/service.html) to draw a watch face I have to use the method OnDraw, is that right? there are no alternatives?

is this a joke? no layout from xml management? no dpi management? no screen format management? etc etc?

really?

Please tell me I'm wrong!

PS this page (http://www.binpress.com/tutorial/how-to-create-a-custom-android-wear-watch-face/120) make a watch face using a normal activity, it's correct or not?


Solution

  • In onCreateEngine() of your WatchFaceService, get the inflater:

    mInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    

    then in onCreate() of your Engine, inflate your layout:

    mFrameLayout = (FrameLayout) mInflater.inflate(R.layout.main, null);
    

    then in onDraw(), measure, layout and draw the view:

            //Measure the view at the exact dimensions (otherwise the text won't center correctly)
            int widthSpec = View.MeasureSpec.makeMeasureSpec(bounds.width(), View.MeasureSpec.EXACTLY);
            int heightSpec = View.MeasureSpec.makeMeasureSpec(bounds.height(), View.MeasureSpec.EXACTLY);
            mFrameLayout.measure(widthSpec, heightSpec);
    
            //Lay the view out at the rect width and height
            mFrameLayout.layout(0, 0, bounds.width(), bounds.height());
    
            mFrameLayout.draw(canvas);
    

    Just don't expect to do this with a TextClock. It doesn't exist on Android Wear. Apparently, Google didn't anticipate developers would want to display a clock on the watchface!