开发者

Change Image in Widget

开发者 https://www.devze.com 2023-02-04 20:17 出处:网络
How do I have one image in my Widget, then when its clicked change to another image? Its a widget with only an ImageButton, but I\'m not sure where to go after this, I know all about pending intents a

How do I have one image in my Widget, then when its clicked change to another image? Its a widget with only an ImageButton, but I'm not sure where to go after this, I know all about pending intents and such, but that doesn't help here, unless I'm missing something... here is my onUpdate

public v开发者_Python百科oid onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds){
   RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
   PendingIntent onPendingIntent = PendingIntent.getService(context, 0, new Intent(context, widgetService.class), 0);
   remoteViews.setOnClickPendingIntent(R.id.ImageButton01, onPendingIntent);
   appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
 }


I think this is what your looking for

 /* get the handle on your widget*/
 RemoteViews views = new RemoteViews(getPackageName(), R.layout.your_widget_layout);
 /* replace the image */
 views.setBitmap(R.id.id_of_your_imageview, "setImageBitmap", yourBitmap);

 /* update your widget */
 ComponentName thisWidget = new ComponentName(this, YourWidget.class);
 AppWidgetManager manager = AppWidgetManager.getInstance(this);
 manager.updateAppWidget(thisWidget, updateViews);


Try this out:

public void onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds){
   RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
   PendingIntent onPendingIntent = PendingIntent.getService(context, 0, new Intent(context, widgetService.class), 0);

   remoteViews.setOnClickPendingIntent(R.layout.widget2, onPendingIntent);
                                      **add widgetON.xml*

Have a separate .xml layout for the "widgetON" image. (Widget2) Then, you'd have to mess with remoteViews to distinguish ON/OFF views

I think. :D

0

精彩评论

暂无评论...
验证码 换一张
取 消