0

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?

MonoThreaded
  • 11,429
  • 12
  • 71
  • 102
Alessandro Scarozza
  • 4,273
  • 6
  • 31
  • 39

2 Answers2

6

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!

user1210205
  • 116
  • 3
1

You're right, you have to use a Canvas or OpenGL. The link you gave explained an old workaround working for pre 5.0 version. Now, you have to use the new API.

Nicolas POMEPUY
  • 1,123
  • 9
  • 11
  • it's crazy! when in a few years there will be devices with different resolutions? – Alessandro Scarozza Jan 01 '15 at 12:44
  • I can't see any problem. The API gives you all the information needed to display your watchface correctly. – Nicolas POMEPUY Jan 01 '15 at 12:46
  • yes, but i need to do all programmatically! Welcome in the 90. – Alessandro Scarozza Jan 01 '15 at 12:50
  • 1
    @Xan what's the big deal in that you have to draw your watch face with Canvas API? CanvasWatchFaceService.Engine is like a WallpaperService.Engine and in wallpaper API you don't have any "managements" you mentioned in your original question – pskink Jan 01 '15 at 14:16
  • never done a WallpaperService. I am very surprised by this, the strength of Android is an SDK that lets you easily create interfaces that fit on all screens (Density and Size). I do not understand the reason for this choice. save battery? – Alessandro Scarozza Jan 01 '15 at 14:31
  • you can still use xml layouts: `v = LayoutInflater.from(context).inflate(R.layout.xxxx, null)` then you can call `v.measure()` and `v.layout()` and finally `v.draw()` in `onDraw()` method – pskink Jan 01 '15 at 14:57