一个小控件转换图片
private static int layoutId;
// onReceive should be something like this
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
int newLayoutId = bundle.getInt("layout_id");
layoutId = newLayoutId;
initViews(context);
} else {
initViews(context);
}
}
private void initViews(Context context) {
RemoteViews views = null;
// Set the initial layout
if (layoutId == 0) {
Intent layoutIntent = new Intent("android.appwidget.action.APPWIDGET_UPDATE");
Bundle layoutBundle = new Bundle();
layoutBundle.putInt("layout_id", 1);
PendingIntent lPendingIntent = PendingIntent.getBroadcast(context, 0,
layoutIntent, PendingIntent.FLAG_ONE_SHOT);
views = new RemoteViews(context.getPackageName(), R.layout.layout_zero);
views.setOnClickPendingIntent(R.id.btnNext, lPendingIntent);
} // Else if there's some trigger to change the layout...
else if (layoutId == 1) {
Intent layoutIntent = new Intent("android.appwidget.action.APPWIDGET_UPDATE");
Bundle layoutBundle = new Bundle();
// Since I only have two layouts, I put here the id of the previous layout
layoutBundle.putInt("layout_id", 0);
PendingIntent lPendingIntent = PendingIntent.getBroadcast(context, 0,
layoutIntent, PendingIntent.FLAG_ONE_SHOT);
views = new RemoteViews(context.getPackageName(), R.layout.layout_one);
views.setOnClickPendingIntent(R.id.btnPrev, lPendingIntent);
}
}
2 其实简单的实践 widget_flame_with_border_off.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/main"
android:gravity="center"
android:background="@drawable/widget_frame_portrait1x1_black"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="74dip"
android:layout_width="74dip">
<LinearLayout
android:id="@+id/btn_led"
android:layout_width="52dip"
android:layout_height="52dip"
android:background="@drawable/appwidget_button_center"
android:clickable="true"
android:focusable="true"
android:gravity="center">
<ImageView
android:id="@+id/img_led"
android:layout_height="40dip"
android:layout_gravity="center"
android:layout_width="40dip"
android:src="@drawable/mototorch_led_off"
android:scaleType="fitXY" />
</LinearLayout>
</LinearLayout>
widget_flame_with_border_on.xml
<pre><?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/main"
android:gravity="center"
android:background="@drawable/widget_frame_portrait1x1_black"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="74dip"
android:layout_width="74dip">
<LinearLayout
android:id="@+id/btn_led"
android:layout_width="52dip"
android:layout_height="52dip"
android:background="@drawable/appwidget_button_center"
android:clickable="true"
android:focusable="true"
android:gravity="center">
<ImageView
android:id="@+id/img_led"
android:layout_height="40dip"
android:layout_gravity="center"
android:layout_width="40dip"
android:src="@drawable/mototorch_led_on"
android:scaleType="fitXY" />
</LinearLayout>
</LinearLayout>
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
RemoteViews views = null;
int layoutID = 0;
if (ledIsOn){
layoutID = R.layout.widget_flame_with_border_on;
}
else {
layoutID = R.layout.widget_flame_with_border_off;
}
// the layoutID that is passed in the constructor of the
// RemoteViews object is the layout that will be loaded
// when the widget is updated.
views = new RemoteViews(context.getPackageName(), layoutID);
for (int i = 0; i < appWidgetIds.length; i++) {
appWidgetManager.updateAppWidget(appWidgetIds[i], views);
}
}