Android Glide 在 Fragment 中不显示图片
Glide does not display picture in a Fragment in Android
我刚开始使用 Glide 将图像加载到我的 Android 应用程序中。我在 Fragment 中使用它,我想将工具栏的背景设置为 URL 中的某个图形。不幸的是,glide 不显示图片,而只是在工具栏中显示白色背景。工具栏本身不是问题,因为我可以用一种颜色改变它的背景,这很有效。还可以显示 App 文件夹中保存的图像(我将其存储在 drawable 文件夹中)。
这是我在片段的 onCreateView 方法中执行的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
binding = FragmentGenericBinding.inflate(inflater, container, false);
ImageView image = new ImageView(getContext());
image.setDrawingCacheEnabled(true);
Glide.with(this).load("https://images.unsplash.com/photo-1614907301762-405163ba662f?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80")
.into(image);
Bitmap bmap = image.getDrawingCache();
binding.toolbarGenericMenu.setBackground(new BitmapDrawable(getResources(), bmap));
//This code works at creates a green background
//ColorDrawable colorDrawable = new ColorDrawable(ContextCompat.getColor(getContext(), R.color.colorGreen));
//binding.toolbarGenericDrinkMenu.setBackground(colorDrawable);
//This code also works and display an image from R.drawable on the toolbar
// binding.toolbarGenericMenu.setBackgroundResource(R.drawable.test_foto);
return binding.getRoot();
}
知道为什么用 Glide 加载图像不起作用吗?我会很感激每一条评论。
如果你在 Kotlin
的布局中使用 androidx.appcompat.widget.Toolbar
,你可以试试这个
val toolbar = findViewById<Toolbar>(R.id.mToolbar)
Glide.with(this)
.asBitmap()
.load("https://images.unsplash.com/photo-1614907301762-405163ba662f?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80")
.into(object : CustomTarget<Bitmap>() {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
toolbar.background = BitmapDrawable(resources, resource)
}
override fun onLoadCleared(placeholder: Drawable?) {}
})
Java
Toolbar toolbar = findViewById(R.id.mToolbar);
Glide.with(this)
.asBitmap()
.load("https://images.unsplash.com/photo-1614907301762-405163ba662f?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80")
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
toolbar.setBackground(new BitmapDrawable(getResources(), resource));
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
我刚开始使用 Glide 将图像加载到我的 Android 应用程序中。我在 Fragment 中使用它,我想将工具栏的背景设置为 URL 中的某个图形。不幸的是,glide 不显示图片,而只是在工具栏中显示白色背景。工具栏本身不是问题,因为我可以用一种颜色改变它的背景,这很有效。还可以显示 App 文件夹中保存的图像(我将其存储在 drawable 文件夹中)。
这是我在片段的 onCreateView 方法中执行的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
binding = FragmentGenericBinding.inflate(inflater, container, false);
ImageView image = new ImageView(getContext());
image.setDrawingCacheEnabled(true);
Glide.with(this).load("https://images.unsplash.com/photo-1614907301762-405163ba662f?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80")
.into(image);
Bitmap bmap = image.getDrawingCache();
binding.toolbarGenericMenu.setBackground(new BitmapDrawable(getResources(), bmap));
//This code works at creates a green background
//ColorDrawable colorDrawable = new ColorDrawable(ContextCompat.getColor(getContext(), R.color.colorGreen));
//binding.toolbarGenericDrinkMenu.setBackground(colorDrawable);
//This code also works and display an image from R.drawable on the toolbar
// binding.toolbarGenericMenu.setBackgroundResource(R.drawable.test_foto);
return binding.getRoot();
}
知道为什么用 Glide 加载图像不起作用吗?我会很感激每一条评论。
如果你在 Kotlin
的布局中使用androidx.appcompat.widget.Toolbar
,你可以试试这个
val toolbar = findViewById<Toolbar>(R.id.mToolbar)
Glide.with(this)
.asBitmap()
.load("https://images.unsplash.com/photo-1614907301762-405163ba662f?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80")
.into(object : CustomTarget<Bitmap>() {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
toolbar.background = BitmapDrawable(resources, resource)
}
override fun onLoadCleared(placeholder: Drawable?) {}
})
Java
Toolbar toolbar = findViewById(R.id.mToolbar);
Glide.with(this)
.asBitmap()
.load("https://images.unsplash.com/photo-1614907301762-405163ba662f?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80")
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
toolbar.setBackground(new BitmapDrawable(getResources(), resource));
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});