问题
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