简体   繁体   中英

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?

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM