什么是实现一个Android窗口小部件动态绘制内容的正确方法? [英] What's the proper way to implement an Android widget with dynamically drawn content?

查看:162
本文介绍了什么是实现一个Android窗口小部件动态绘制内容的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关我的第一个也是最awesomest Android项目,我想创建一个主屏幕控件,它显示这些的弧时钟的,所有的孩子们热烈讨论的这些日子。

For my first and most awesomest Android project, I want to create a home screen widget which displays one of those Arc Clocks that all the kids are raving about these days.

,在我看来,要获得适当的方式它实际上在屏幕上绘制是使用的ImageView 在实际的布局,然后当它是更新,创建一个<一个href="http://developer.android.com/reference/android/graphics/drawable/ShapeDrawable.html">ShapeDrawable两个和吸引他们到一个新创建的位图。然后,设置位图作为ImageView的源

Based on the limitations of RemoteViews, it looks to me that the appropriate way to get it actually drawn on the screen is to use an ImageView in the actual layout, then when it is updating, create a ShapeDrawable or two and draw them onto a newly created Bitmap. Then, set that Bitmap as the source of the ImageView.

也有一些是关于我只是没有grokking,但这一进程。以下是code我试图用更新部件。我什么也没有在屏幕上绘制的。 ImageView的刚刚通过的设置一个占位符图像的src 属性。当我不跑了code将其更新到我的画,占位符图像仍留在原位。所以,我知道这code是干什么的的东西的,刚才明明没有的右键的事情。

There is Something about that process that I'm just not grokking, though. The following is the code I tried to use to update the widget. I get nothing drawn on the screen at all. The ImageView has a placeholder image just set by its src property. When I don't run the code to update it to my drawing, the placeholder image stays in place. So, I know this code is doing something, just obviously not the right thing.

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
Bitmap bitmap = Bitmap.createBitmap(100, 100, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);

ShapeDrawable drawable = new ShapeDrawable(new ArcShape(0, 360));
drawable.getPaint().setColor(0xffff0000);
drawable.getPaint().setStrokeWidth(5.0f);
drawable.setIntrinsicWidth(100);
drawable.setIntrinsicHeight(100);
drawable.draw(canvas);

views.setImageViewBitmap(R.id.ImageView01, bitmap);     
appWidgetManager.updateAppWidget(appWidgetId, views);

我知道这不可能是那么容易。所以,我会连朝着正确的方向?

I knew it couldn't be that easy. So, am I even heading in the right direction?

更新

好吧,也许这是太多的信息。这个问题我真的想问的是:

Ok, maybe that was too much information. The question I'm really trying to ask is this:

如果我想绘制形状上的小部件,我应该使用ImageView的和我的ShapeDrawable创建位图或是否有更合适的方式吸引到它是由RemoteViews约束小部件?

If I want to draw shapes on a widget, should I use an ImageView and create a bitmap from my ShapeDrawable or is there more appropriate way to draw into a widget where it is constrained by RemoteViews?

推荐答案

其实是有一个部件叫<一href="http://developer.android.com/reference/android/widget/AnalogClock.html"><$c$c>AnalogClock在SDK,这是非常相似,你正在做什么。

There is actually a widget called AnalogClock in the SDK that is very similar to what you are trying to do.

它是作为一个扩展查看,并注有 @RemoteView A类。它注册的接收器上的 Intent.ACTION_TIME_TICK Intent.ACTION_TIME_CHANGED ,并通过覆盖的OnDraw绘制自身方法。退房的<一个href="http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob_plain;f=core/java/android/widget/AnalogClock.java;hb=HEAD">source.该标准<一href="http://android.git.kernel.org/?p=platform/packages/apps/AlarmClock.git;a=summary">AlarmClock应用<一href="http://android.git.kernel.org/?p=platform/packages/apps/AlarmClock.git;a=blob_plain;f=AndroidManifest.xml;hb=HEAD">exposes <一href="http://android.git.kernel.org/?p=platform/packages/apps/AlarmClock.git;a=blob_plain;f=res/xml/analog_appwidget.xml;hb=HEAD">this <一href="http://android.git.kernel.org/?p=platform/packages/apps/AlarmClock.git;a=blob_plain;f=res/layout/analog_appwidget.xml;hb=HEAD">view作为一个小窗口可添加到主屏幕。

It is implemented as a class that extends View and is annotated with @RemoteView. It registers receivers on Intent.ACTION_TIME_TICK and Intent.ACTION_TIME_CHANGED and draws itself by overriding the onDraw method. Check out the source. The standard AlarmClock application exposes this view as a widget that can be added to the home screen.

我认为这是一个很好的(如果不是最好的)的方式来实现你所描述,显然有什么效果很好。

I think that is a good (if not the best) way to implement what you described and obviously works well.

这篇关于什么是实现一个Android窗口小部件动态绘制内容的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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