如何使用 glide 将图像下载到可绘制对象中?
How does one use glide to download an image into a drawable?
我正在学习使用 ViewPager 的图像滑块。为此,我希望图像是动态的。 Glide 很简单,但我认为我没有正确使用它。
这是我正在尝试做的事情。
//Initialize by default Images
private static final Integer[] IMAGES = {R.drawable.image1,R.drawable.image2};
// Load images into Drawables using Glide
Glide.with(this)
.load("https://google.com/someImage1.jpg")
.error(R.drawable.image1)
.into(R.drawable.image1);
Glide.with(this)
.load("https://google.com/someImage2.jpg")
.error(R.drawable.image2)
.into(R.drawable.image2);
// Set Again
IMAGES[0]= R.drawable.image1;
IMAGES[1]= R.drawable.image2;
但是 .into(java.lang.Integer
) 没有定义,我知道。但我想要一些可以解决我的问题的东西。我有滑块视图。因为我需要将图像作为可绘制对象或类似的东西传递。图像加载将由 Adapter 进行。据我所知,我无法直接加载。有什么方法可以在 drawable
中加载滑行图像吗?
另外,在本地捕获或存储图像的最佳方式是什么,这样即使没有互联网连接,滑块也能正常工作。
这是我的适配器
public class MainPageSlideImageAdapter extends PagerAdapter {
private ArrayList<Integer> IMAGES;
private LayoutInflater inflater;
private Context context;
public MainPageSlideImageAdapter(Context context,ArrayList<Integer> IMAGES) {
this.context = context;
this.IMAGES=IMAGES;
inflater = LayoutInflater.from(context);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return IMAGES.size();
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.main_page_slidingimages, view, false);
assert imageLayout != null;
final ImageView imageView = (ImageView) imageLayout
.findViewById(R.id.image);
imageView.setImageResource(IMAGES.get(position));
view.addView(imageLayout, 0);
return imageLayout;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
@Override
public Parcelable saveState() {
return null;
}
}
您必须提供.into()
中的视图请查看示例
样本布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/myImage"
android:layout_width="100dp"
android:layout_height="100dp"/>
</LinearLayout>
更新
@Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.main_page_slidingimages, view, false);
assert imageLayout != null;
final ImageView imageView = (ImageView) imageLayout
.findViewById(R.id.image);
Glide.with(this)
.load(IMAGES.get(position))
.error(R.drawable.image2)
.into(imageView);
//imageView.setImageResource();
view.addView(imageLayout, 0);
return imageLayout;
}
不是将图像存储在内存中,而是存储地址。
private static final String[] IMAGES = {"https://google.com/someImage1.jpg", "https://google.com/someImage2.jpg"};
然后在 instantiateItem
时加载图像
@Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.main_page_slidingimages, view, false);
assert imageLayout != null;
final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
Glide.with(context)
.load(IMAGES.get(position))
.error(R.drawable.image1)
.into(imageView);
view.addView(imageLayout, 0);
return imageLayout;
}
此外,glide 还提供内存和磁盘缓存。这意味着即使没有互联网,它也应该能够显示图像。
imageView.setImageResource
只需要替换为 Glide 调用。
或者对要下载的 Urls 使用字符串的 ArrayAdapter。我不认为你可以 "overwrite" 来自 Glide
的可绘制对象
我正在学习使用 ViewPager 的图像滑块。为此,我希望图像是动态的。 Glide 很简单,但我认为我没有正确使用它。 这是我正在尝试做的事情。
//Initialize by default Images
private static final Integer[] IMAGES = {R.drawable.image1,R.drawable.image2};
// Load images into Drawables using Glide
Glide.with(this)
.load("https://google.com/someImage1.jpg")
.error(R.drawable.image1)
.into(R.drawable.image1);
Glide.with(this)
.load("https://google.com/someImage2.jpg")
.error(R.drawable.image2)
.into(R.drawable.image2);
// Set Again
IMAGES[0]= R.drawable.image1;
IMAGES[1]= R.drawable.image2;
但是 .into(java.lang.Integer
) 没有定义,我知道。但我想要一些可以解决我的问题的东西。我有滑块视图。因为我需要将图像作为可绘制对象或类似的东西传递。图像加载将由 Adapter 进行。据我所知,我无法直接加载。有什么方法可以在 drawable
中加载滑行图像吗?
另外,在本地捕获或存储图像的最佳方式是什么,这样即使没有互联网连接,滑块也能正常工作。
这是我的适配器
public class MainPageSlideImageAdapter extends PagerAdapter {
private ArrayList<Integer> IMAGES;
private LayoutInflater inflater;
private Context context;
public MainPageSlideImageAdapter(Context context,ArrayList<Integer> IMAGES) {
this.context = context;
this.IMAGES=IMAGES;
inflater = LayoutInflater.from(context);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return IMAGES.size();
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.main_page_slidingimages, view, false);
assert imageLayout != null;
final ImageView imageView = (ImageView) imageLayout
.findViewById(R.id.image);
imageView.setImageResource(IMAGES.get(position));
view.addView(imageLayout, 0);
return imageLayout;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
@Override
public Parcelable saveState() {
return null;
}
}
您必须提供.into()
中的视图请查看示例
样本布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/myImage"
android:layout_width="100dp"
android:layout_height="100dp"/>
</LinearLayout>
更新
@Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.main_page_slidingimages, view, false);
assert imageLayout != null;
final ImageView imageView = (ImageView) imageLayout
.findViewById(R.id.image);
Glide.with(this)
.load(IMAGES.get(position))
.error(R.drawable.image2)
.into(imageView);
//imageView.setImageResource();
view.addView(imageLayout, 0);
return imageLayout;
}
不是将图像存储在内存中,而是存储地址。
private static final String[] IMAGES = {"https://google.com/someImage1.jpg", "https://google.com/someImage2.jpg"};
然后在 instantiateItem
@Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.main_page_slidingimages, view, false);
assert imageLayout != null;
final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
Glide.with(context)
.load(IMAGES.get(position))
.error(R.drawable.image1)
.into(imageView);
view.addView(imageLayout, 0);
return imageLayout;
}
此外,glide 还提供内存和磁盘缓存。这意味着即使没有互联网,它也应该能够显示图像。
imageView.setImageResource
只需要替换为 Glide 调用。
或者对要下载的 Urls 使用字符串的 ArrayAdapter。我不认为你可以 "overwrite" 来自 Glide
的可绘制对象