将 URL 中的 ImageView 加载到主屏幕小部件的 RemoteView 中
Load ImageView from URL into RemoteView of a Home Screen Widget
我正在根据 Google StackWidget
示例为我的 Android 应用程序开发一个简单的小部件:https://android.googlesource.com/platform/development/+/master/samples/StackWidget/src/com/example/android/stackwidget/StackWidgetService.java
我正在使用 Glide 图像库并尝试在扩展 RemoteViewsService
的 StackWidgetService
class 的 getViewAt
方法上填充 ImageView
。我的代码类似于:
Handler uiHandler = new Handler(Looper.getMainLooper());
uiHandler.post(() ->
Glide.with(context)
.asBitmap()
.load(widgetItems.get(position).image_url)
.into(new SimpleTarget<Bitmap>(512, 512) {
@Override
public void onResourceReady(Bitmap bitmap, Transition transition) {
rv.setImageViewBitmap(R.id.widget_item_image, bitmap);
}
})
);
从 URL 加载图像以从 Android Widget
填充 RemoteView
的最佳方式是什么?
只需要同步完成即可。这似乎工作正常:
try {
Bitmap bitmap = Glide.with(context)
.asBitmap()
.load(widgetItems.get(position).image_url)
.submit(512, 512)
.get();
rv.setImageViewBitmap(R.id.widget_item_image, bitmap);
} catch (Exception e) {
e.printStackTrace();
}
Glide 有一个用于执行此操作的构造,称为:AppWidgetTarget:
在科特林中:
val awt: AppWidgetTarget = object : AppWidgetTarget(context.applicationContext, R.id.img, remoteViews, *appWidgetIds) {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
super.onResourceReady(resource, transition)
}
}
var options = RequestOptions().
override(300, 300).placeholder(R.drawable.placeholder_img).error(R.drawable.error_img)
Glide.with(context.applicationContext).asBitmap().load(imageUrl).apply(options).into(awt)
在Java中:
AppWidgetTarget awt = new AppWidgetTarget(context, R.id.img_dog, remoteViews, appWidgetIds) {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
super.onResourceReady(resource, transition);
}
};
RequestOptions options = new RequestOptions().
override(300, 300).placeholder(R.drawable.placeholder_img).error(R.drawable.error_img)
Glide.with(context.getApplicationContext())
.asBitmap()
.load(imageUrl)
.apply(options)
.into(awt);
- 文档:https://futurestud.io/tutorials/glide-loading-images-into-notifications-and-appwidgets
- 在当前版本的 Glide 4.7.1 上测试
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_widget.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>
我正在根据 Google StackWidget
示例为我的 Android 应用程序开发一个简单的小部件:https://android.googlesource.com/platform/development/+/master/samples/StackWidget/src/com/example/android/stackwidget/StackWidgetService.java
我正在使用 Glide 图像库并尝试在扩展 RemoteViewsService
的 StackWidgetService
class 的 getViewAt
方法上填充 ImageView
。我的代码类似于:
Handler uiHandler = new Handler(Looper.getMainLooper());
uiHandler.post(() ->
Glide.with(context)
.asBitmap()
.load(widgetItems.get(position).image_url)
.into(new SimpleTarget<Bitmap>(512, 512) {
@Override
public void onResourceReady(Bitmap bitmap, Transition transition) {
rv.setImageViewBitmap(R.id.widget_item_image, bitmap);
}
})
);
从 URL 加载图像以从 Android Widget
填充 RemoteView
的最佳方式是什么?
只需要同步完成即可。这似乎工作正常:
try {
Bitmap bitmap = Glide.with(context)
.asBitmap()
.load(widgetItems.get(position).image_url)
.submit(512, 512)
.get();
rv.setImageViewBitmap(R.id.widget_item_image, bitmap);
} catch (Exception e) {
e.printStackTrace();
}
Glide 有一个用于执行此操作的构造,称为:AppWidgetTarget:
在科特林中:
val awt: AppWidgetTarget = object : AppWidgetTarget(context.applicationContext, R.id.img, remoteViews, *appWidgetIds) {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
super.onResourceReady(resource, transition)
}
}
var options = RequestOptions().
override(300, 300).placeholder(R.drawable.placeholder_img).error(R.drawable.error_img)
Glide.with(context.applicationContext).asBitmap().load(imageUrl).apply(options).into(awt)
在Java中:
AppWidgetTarget awt = new AppWidgetTarget(context, R.id.img_dog, remoteViews, appWidgetIds) {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
super.onResourceReady(resource, transition);
}
};
RequestOptions options = new RequestOptions().
override(300, 300).placeholder(R.drawable.placeholder_img).error(R.drawable.error_img)
Glide.with(context.getApplicationContext())
.asBitmap()
.load(imageUrl)
.apply(options)
.into(awt);
- 文档:https://futurestud.io/tutorials/glide-loading-images-into-notifications-and-appwidgets
- 在当前版本的 Glide 4.7.1 上测试
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_widget.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>