Android Widgets: Animations on RemoteViews ? [dupl

2019-03-14 10:50发布

This question already has an answer here:

I'm not having too much success applying animations to views inside a RemoteViews.

For the sake of simplicity, let's say I have a widget with an ImageView and a Button. When clicking the button, I want to add a simple animation to the ImageView (a rotation for example).

Since I can't get a reference using findViewById() like in an Activity and RemoteViews doesn't have a setter for an animation, I'm not sure what should I do.

I'm thinking of replacing the RemoteViews for the widget with a new layout, similar to the original but this one has the animation already loaded. Can I do this? Is it possible to embed an animation in a layout?

Code would be something like this for the 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);

Then in RotateService:

ComponentName myWidget = new ComponentName(this, MyWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
RemoteViews newViews = buildNewRemoteViewsWithAnimation();
manager.updateAppWidget(myWidget, newViews);

Any other suggestion?

2条回答
Explosion°爆炸
2楼-- · 2019-03-14 11:20
  1. Create custom animation.
  2. Create ProgressBar and set in android:indeterminateDrawable your animation.
  3. Add ProgressBar to your widget layout and make it visible(invisible)
查看更多
你好瞎i
3楼-- · 2019-03-14 11:39

Have you tried using a LayoutAnimation defined in xml. This is the only way I've found to add any sort of animation effect to my appwidget. The downside is that you can't control the animation, so everytime the widget layout is reloaded, i.e an OnUpdate, the animation defined in XML will fire.

This is ok in some circumstances but not all.

As the name suggests LayoutAnimations are applied to Layouts so it's easy to specify which Views you want animated by nesting them in their own Layout. I used LinearLayout in my case and it worked fine.

See Picz on the market for a demonstration of this.

查看更多
登录 后发表回答