安卓小部件:如何在RemoteViews上添加动画?

22

在一个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);

还有其他建议吗?

1个回答

7
  1. 创建自定义动画。
  2. 创建进度条并在android:indeterminateDrawable中设置您的动画。
  3. 将进度条添加到您的小部件布局中,并使其可见(不可见)。

你确定它适用于AppWidgets吗? - android developer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接