How do I draw icons on a watch face from an Android wear complication provider?

混江龙づ霸主 提交于 2019-12-12 12:34:08

问题


Using the Complications API there's an icon type, but when it comes to drawing the icon I'm not sure how to do it.

When I'm drawing the actual watch face there doesn't seem to be a drawIcon method. E.g. something like canvas.drawIcon(icon). I can only see how to draw a bitmap.

In my drawComplications method I have this:

} else if (complicationData.getType() == ComplicationData.TYPE_ICON) {
                Icon icon = complicationData.getIcon();

How do I then draw the icon to the canvas?

The code lab here doesn't tell us how to draw icons: https://codelabs.developers.google.com/codelabs/complications/index.html?index=..%2F..%2Findex#3

I could instead just copy all the drawables to the local wearable module, and bypass the icon type by passing in a string, and from there just draw the appropriate bitmap. But there must be a way to draw from the icon otherwise why have it?

I also couldn't find out from Googling how to convert the icon to a bitmap, but that also seems silly as I had to convert the bitmap to an icon in the first place as the API wants an icon.

Any help appreciated.

Thanks.


回答1:


Ok so I don't know how I missed this but it's as simple as

Drawable drawable = icon.loadDrawable(getApplicationContext());

Then just do this which you probably already knew:

if (drawable instanceof BitmapDrawable) {
    Bitmap bitmap;
    bitmap = ((BitmapDrawable) drawable).getBitmap();
    canvas.drawBitmap(bitmap, complicationsX, mTopComplicationY, null);
}

I was looking here

https://developer.android.com/reference/android/graphics/drawable/Icon.html#loadDrawable(android.content.Context)

and then saw it and wondered why I hadn't tried that.

Edit in reponse to comment: If you want to use a VectorDrawable then adapt as necessary.



来源:https://stackoverflow.com/questions/41579550/how-do-i-draw-icons-on-a-watch-face-from-an-android-wear-complication-provider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!