在一个RemoteViews
中为视图应用动画的效果并不容易。
为了简单起见,假设我有一个小部件,其中包含一个ImageView
和一个Button
。当点击按钮时,我想为ImageView
添加一个简单的动画(例如旋转)。
由于不能像在Activity
中那样使用findViewById()
获取引用,并且RemoteViews
没有设置动画的方法,所以我不确定该怎么办。
我考虑用一个新的布局替换小部件中的RemoteViews
,它与原始布局类似,但是已经加载了动画。我能这样做吗?可以将动画嵌入到布局中吗?
对于WidgetProvider,代码应该类似于:
Intent intent = new Intent(context, RotateService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.a_widget);
views.setOnClickPendingIntent(R.id.a_button, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
然后在 RotateService 中:
ComponentName myWidget = new ComponentName(this, MyWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
RemoteViews newViews = buildNewRemoteViewsWithAnimation();
manager.updateAppWidget(myWidget, newViews);
还有其他建议吗?