4

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.

TofferJ
  • 4,678
  • 1
  • 37
  • 49
Michael Vescovo
  • 3,741
  • 4
  • 32
  • 45

1 Answers1

5

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.

Michael Vescovo
  • 3,741
  • 4
  • 32
  • 45
  • Hi. This seems to fail in most cases, as it is often a vectordrawable attached to the icon. I used the following instead: http://stackoverflow.com/a/10600736 – Thomas Thomas Mar 24 '17 at 22:46
  • The first line is the answer. Use whatever drawable you want. In my case I was using a bitmap. The answer you've referenced doesn't even mention icon.loadDrawable so it would fail completely as an answer to my question. – Michael Vescovo Mar 25 '17 at 02:07
  • I agree with your solution, using icon.loadDrawable. But it seems that I can not always cast the returned Drawable as a BitmapDrawable. Sometimes the returned Drawable is a VectorDrawable, and it will crash. That is why I mentionned this link, to transform the Drawable to a Bitmap. – Thomas Thomas Mar 26 '17 at 10:14
  • Like the default icon for Battery: it seems that it is a VectorDrawable, and cast in BitmapDrawable did crash for me. But maybe did I missed something? It seems that you find a way to use any type of drawable, I am very interested as I am currently working on it. – Thomas Thomas Mar 26 '17 at 10:18
  • In your example you are testing the instanceof BitmapDrawable, but you will miss a lot of icons if you only display the BitmapDrawable. – Thomas Thomas Mar 26 '17 at 10:19
  • how can it crash? If it's not a BitmapDrawable it will not run the code (in the if statement). – Michael Vescovo Mar 26 '17 at 10:19
  • Like I said, use whatever code you want to handle the drawable. That wasn't the answer, I provided it only as an example - I thought it would be more helpful than not. There's nothing stopping you from using a VectorDrawable. What you're talking about is a totally different question. I wanted to know how to get the drawable out of the icon. That was my question. – Michael Vescovo Mar 26 '17 at 10:24
  • If you are the complication data provider developer, you are sure of the type of Icon you use. But in the other cases, you can not just handle BimapDrawable. I thought that your question was general: "how to draw icon on a watch face". That is why I mentionned that link. If you only want to display BitmapDrawable, it is ok. – Thomas Thomas Mar 26 '17 at 10:31
  • please give answer here: http://stackoverflow.com/questions/43538447/how-set-complication-on-watch-face-of-icon-ranged-value-small-image-on-android-w – Sachin Suthar Apr 21 '17 at 09:29