java中的另一个示例。
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
String url = "https://firebasestorage.googleapis.com/v0/b/telling-299f3.appspot.com/o/POSTS%2Fcropped1099087212.jpg?alt=media&token=6a3bca19-6483-484d-a7e3-64c25a8a77a8";
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.app_widget);
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
AppWidgetTarget awt = new AppWidgetTarget(context, R.id.widget_post_image, views, appWidgetId) {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
super.onResourceReady(resource, transition);
}
};
RequestOptions options = new RequestOptions().
override(300, 300).placeholder(R.drawable.post_placeholder).error(R.drawable.post_placeholder);
Glide.with(context.getApplicationContext())
.asBitmap()
.load(url)
.apply(options)
.into(awt);
//views.setImageViewBitmap(R.id.widget_post_image, bitmap);
views.setOnClickPendingIntent(R.id.widget_post_image, pendingIntent);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
我的app\u小部件。xml文件包含
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#09C"
android:padding="@dimen/widget_margin">
<ImageView
android:id="@+id/widget_post_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/placeholder_image_one" />
</LinearLayout>