在画布上绘制到主屏幕小部件 - Android

发布于 2025-01-08 18:41:44 字数 328 浏览 0 评论 0原文

我正在尝试创建一个主屏幕小部件,它是显示时间的自定义方式(想象一个弧形时钟但有所不同)。所以,我需要能够使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掩饰不了的爱 2025-01-15 18:41:44

在您的 widget_layout.xml 中有一个 ImageView,它将包含您的绘图区域

<ImageView
    android:id="@+id/widget_image"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

在您的 AppWidgetProvider.onUpdate 方法中,创建一个位图并创建一个 Canvas 可以在上面绘图。位图越大,质量越好,但会降低性能。

Bitmap bitmap = Bitmap.createBitmap(400, 180, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);

在画布上完成绘画后,使用 setImageViewBitmap 更新小部件

final RemoteViews remoteViews = new RemoteViews(
            context.getPackageName(), R.layout.widget_layout);
remoteViews.setImageViewBitmap(R.id.widget_image, bitmap);

In your widget_layout.xml have an ImageView that will contain your drawing area

<ImageView
    android:id="@+id/widget_image"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

In your AppWidgetProvider.onUpdate method, create a bitmap and make a Canvas from it that you can draw on. The larger the bitmap, the better the quality at the expense of performance.

Bitmap bitmap = Bitmap.createBitmap(400, 180, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);

When you are done painting on your canvas, update the widget using setImageViewBitmap

final RemoteViews remoteViews = new RemoteViews(
            context.getPackageName(), R.layout.widget_layout);
remoteViews.setImageViewBitmap(R.id.widget_image, bitmap);
老娘不死你永远是小三 2025-01-15 18:41:44

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文