如何从Android穿戴式并发症提供者的表盘上绘制图标? [英] How do I draw icons on a watch face from an Android wear complication provider?

查看:108
本文介绍了如何从Android穿戴式并发症提供者的表盘上绘制图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Complications API,有一种图标类型,但是当涉及到绘制图标时,我不确定该怎么做。



绘制时实际的表盘表面似乎没有drawIcon方法。例如。类似于canvas.drawIcon(icon)。我只能看到如何绘制位图。



在我的drawComplications方法中,我有以下内容:

 }否则,如果(complicationData.getType()== ComplicationData.TYPE_ICON){
Icon icon = complicationData.getIcon();

然后如何在画布上绘制图标?



此处的代码实验室不会告诉我们如何绘制图标: https://codelabs.developers.google.com/codelabs/complications/index.html?index=..%2F..%2Findex#3



我可以将所有可绘制对象复制到本地可穿戴模块,并通过传入字符串来绕过图标类型,然后从那里绘制适当的位图。但是必须有一种方法可以从图标中提取图片,否则为什么呢?



我也无法从Google搜索中找到如何将图标转换为位图的方法,但是这似乎很愚蠢,因为我必须首先将位图转换为图标,因为API需要一个图标。



任何帮助表示赞赏。



谢谢。

解决方案

好,所以我不知道我怎么想念这个,但是

 简单可绘制drawable = icon.loadDrawable(getApplicationContext()); 

然后只需执行您可能已经知道的操作即可:

  if(BitmapDrawable的可绘制实例){
位图位图;
位图=(((BitmapDrawable)drawable).getBitmap();
canvas.drawBitmap(bitmap,并发症X,mTopComplicationY,null);
}

我在这里查看



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



,然后看到它并想知道为什么我没有尝试过。



编辑以回应评论:如果要使用VectorDrawable,请根据需要进行调整。 / p>

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.

解决方案

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.

这篇关于如何从Android穿戴式并发症提供者的表盘上绘制图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆