在小部件中旋转 ImageView
我正在创建一个需要旋转 ImageView 的小部件。 ImageView 驻留在一个布局内,该布局又在 RemoteView 中进行描述。这可能吗?在我的常规应用程序中,活动使用 findViewById() 获得对 ImageView 的引用,然后调用 setRotate(),但由于 RemoteView 不是活动,因此 findViewById() 不可用。
我可以看到这个应用程序之前已经完成了: https://market. android.com/details?id=com.lanteanstudio.compass
I'm creating a widget where I need to rotate an ImageView. The ImageView resides insides a layout which in turn is described in a RemoteView. Is this possible? In my regular application Activity gained a reference to the ImageView using findViewById() and then calling setRotate(), but as RemoteView isn't an activity findViewById() isn't available.
I can see that it has been done before in this app: https://market.android.com/details?id=com.lanteanstudio.compass
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是 RemoteViews,那么您无法直接更改可见视图本身,因为无法获取对它的引用——RemoteViews 对象通常运行在不同的进程(对于主屏幕上的小部件来说,这是正确的 - 小部件在主屏幕进程中运行,而不是您自己的进程)。
相反,您需要创建一个旋转图像的新 RemoteView - 您需要在将位图设置为
ImageView
之前旋转位图。然后获取
AppWidgetManager
的实例,并调用manager.updateAppWidget(appWidgetId,remoteView)
。If you are using
RemoteViews
, then you cannot directly change the visible View itself, as there is no way of getting a reference to it -- aRemoteViews
object generally runs in a different process (in the case of the widgets on the Homescreen this is true - the widgets run in the Homescreen process, not your own).Instead you need to create a new RemoteViews with the image rotated - you need to rotate the bitmap before setting it to the
ImageView
.Then get an instance of
AppWidgetManager
, and callmanager.updateAppWidget(appWidgetId, remoteView)
.在花了一天时间之后...我发现以下代码适用于我的天气应用程序小部件来指示风向。
R.drawable.arrow 是我的可绘制文件夹中的 png
windDeg 是从 openweathermap.org 获取的 int
R.id.wind_direction 是 ImageView
After spending a day on this... I found the following code to work for my weather app widget to indicate wind direction.
R.drawable.arrow is png in my drawable folder
windDeg is an int obtained from openweathermap.org
R.id.wind_direction is an ImageView