在画布上绘制到主屏幕小部件 - Android
我正在尝试创建一个主屏幕小部件,它是显示时间的自定义方式(想象一个弧形时钟但有所不同)。所以,我需要能够使用 android 图形库来绘制这个小部件。实际上我只需要每 20 或 30 分钟更新一次。
最好的方法是什么?小部件的视图类型似乎有很多限制。我可以只包含一个画布,我用信息更新它就会重新绘制吗?或者我是否需要它成为一个 ImageView,并让广播接收器每 20 分钟重绘一个图像,然后替换该图像?
TL;DR 我可以在主屏幕小部件中包含一个每 20 分钟重绘一次的画布对象吗?
我很难弄清楚如何表达问题来寻找答案,因此任何正确方向的帮助将不胜感激。
I'm trying to create a home screen widget that's a custom way to display time (Imagine an arc clock but different). So, I need to be able to draw using the android graphics library to this widget. Realistically I'd only need to update every 20 or 30 minutes.
Whats the best way to do this? It seems like there's a lot of limitations on types of views for widgets. Can I just include a canvas which I update with info and it will redraw? Or would I need perhaps it to be an ImageView, and have a BroadCast Receiver redraw a single image every 20 minutes and then replace the image?
TL;DR Can I include a canvas object in a home screen widget which I redraw every 20ish minutes?
I'm having a hard time trying to figure out how to phrase the question to search for an answer, so any help in the right direction would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的
widget_layout.xml
中有一个ImageView
,它将包含您的绘图区域在您的
AppWidgetProvider.onUpdate
方法中,创建一个位图并创建一个Canvas
可以在上面绘图。位图越大,质量越好,但会降低性能。在画布上完成绘画后,使用
setImageViewBitmap
更新小部件In your
widget_layout.xml
have anImageView
that will contain your drawing areaIn your
AppWidgetProvider.onUpdate
method, create a bitmap and make aCanvas
from it that you can draw on. The larger the bitmap, the better the quality at the expense of performance.When you are done painting on your canvas, update the widget using
setImageViewBitmap
Android 的主屏幕小部件使用 RemoteViews 系统。
这意味着您的应用程序提供其布局的 id,以及对此布局的一些基本(有限)修改集,例如设置文本、颜色、可见性等。所有这些修改都写入“Parcel" 并由系统发送到实际的其他进程管理主屏幕项目。此过程将加载布局、解包修改数据并应用于布局。
所以你看,你无法直接访问 Canvas。
编辑:绘制任何内容的一种可能性是使用 RemoteViews.setBitmap,这样您就可以在小部件中拥有任何位图。但要注意,Android 人员说它很慢,因为位图必须是 存储到“parcel” 这意味着转换为字符串之类的东西,然后在其他部分转换回位图。如果图像很小,它可能会起作用。
Android's home screen widget using system of RemoteViews.
This means your app provides id of its layout, and some basic (limited) set of modifications to this layout, such as setting text, color, visibility, etc. All these modifications are written into "Parcel" and sent by system to other process which actually manages home screen items. This process will load the layout, unparcels modification data and aplies to layout.
So you see, you can't have any direct access to Canvas.
EDIT: one possibility to draw anything is to use RemoteViews.setBitmap, so you'd have any bitmap in the widget. But beware, Android guys say it is SLOW, as bitmap has to be stored to "parcel" which means converted to something like string, then convertet back to Bitmap on other part. If it's small image, it may work.