Android 如何使用glide将图像下载到绘图设备中?
我有学习图像滑块使用ViewPager。为此,我希望图像是动态的。滑翔很容易,但我认为我没有正确地使用它。 这就是我要做的Android 如何使用glide将图像下载到绘图设备中?,android,android-glide,Android,Android Glide,我有学习图像滑块使用ViewPager。为此,我希望图像是动态的。滑翔很容易,但我认为我没有正确地使用它。 这就是我要做的 //Initialize by default Images private static final Integer[] IMAGES = {R.drawable.image1,R.drawable.image2}; // Load images into Drawables using Glide Glide.with(this)
//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
)没有定义,我知道这一点。但是我想要一些能解决我问题的东西。我有滑动视图。在这方面,我需要将图像作为绘图或类似的东西传递。图像加载将由适配器进行。据我所知,我不能直接加载。有没有办法在drawable
中加载滑动图像
另外,在本地捕获或存储图像的最佳方式是什么,以便在没有internet连接的情况下,滑块仍能顺利工作
这是我的护照
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;
}
}
public类MainPageSlideImageAdapter扩展了PagerAdapter{
私有数组列表图像;
私人充气机;
私人语境;
公共MainPageSlideImageAdapter(上下文上下文、ArrayList图像){
this.context=上下文;
这个。图像=图像;
充气器=充气器。从(上下文);
}
@凌驾
公共项(视图组容器、int位置、对象){
container.removeView((视图)对象);
}
@凌驾
public int getCount(){
返回图像。size();
}
@凌驾
公共对象实例化项(视图组视图,int位置){
查看图像布局=充气机。充气(R.layout.main\u页面\u滑动图像,查看,错误);
断言imageLayout!=null;
最终图像视图图像视图=(图像视图)图像布局
.findviewbyd(R.id.image);
setImageResource(IMAGES.get(position));
view.addView(imageLayout,0);
返回图像布局;
}
@凌驾
公共布尔值isViewFromObject(视图,对象){
返回视图.equals(对象);
}
@凌驾
公共无效恢复状态(可包裹状态,类装入器){
}
@凌驾
公共包裹存储状态(){
返回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>
您必须在
.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>
与其将图像存储在内存中,不如存储地址
private static final String[] IMAGES = {"https://google.com/someImage1.jpg", "https://google.com/someImage2.jpg"};
然后,当您实例化Item
@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还提供内存和磁盘缓存。这意味着即使没有互联网,它也应该能够显示图像。与其将图像存储在内存中,不如存储地址
private static final String[] IMAGES = {"https://google.com/someImage1.jpg", "https://google.com/someImage2.jpg"};
然后,当您实例化Item
@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
只需用滑动调用替换即可
或者对要下载的URL使用字符串数组适配器。我认为您无法从Glide“覆盖”可绘制的图像视图。setImageResource只需替换为Glide调用即可
或者对要下载的URL使用字符串数组适配器。我不认为您可以“覆盖”Glide中的可绘图文件当您遇到未定义方法的错误时,请务必查看文档。
R.drawable
引用是整数。文档清楚地显示了ImageView已加载到数据库中。我确实理解并阅读了文档。请阅读我添加的详细信息。当您遇到未定义方法的错误时,请始终检查文档。R.drawable
引用是整数。文档清楚地显示了ImageView已加载到数据库中。我确实理解并阅读了文档。请阅读我添加的详细信息。我理解。但我有滑动视图。在这方面,我需要将图像作为绘图或类似的东西传递。图像加载将由适配器进行。据我所知,我无法直接加载。@ArewegoodQ根据您的上述代码更新了我的答案。我相信您正在将图像数组发送到适配器,因此我正在从数组中获取图像,并使用glide将其加载到图像视图中。请检查它是否适用于您。我明白这一点。但我有滑动视图。在这方面,我需要将图像作为绘图或类似的东西传递。图像加载将由适配器进行。据我所知,我无法直接加载。@ArewegoodQ根据您的上述代码更新了我的答案。我相信您正在将图像数组发送到适配器,因此我正在从数组中获取图像,并使用glide将其加载到图像视图中。请检查它是否适用于您。我明白这一点。但我有滑动视图。在这方面,我需要将图像作为绘图或类似的东西传递。图像加载将由适配器进行。据我所知,我不能直接加载。当然。请看adapater的构造函数,我正在处理图像的整数。@ArewegoodQ我已经更新了我的答案。而且,Drawable
在编译后无法更改。是的。我明白了。你的答案和我想要的很接近。当然,我想要的是不可能的:因此我接受了它。我理解这一点。但我有滑动视图。在这方面,我需要将图像作为绘图或类似的东西传递。图像加载将由适配器进行。据我所知,我不能直接加载。当然。请看adapater的构造函数,我正在处理图像的整数。@ArewegoodQ我已经更新了我的答案。而且,Drawable
在编译后无法更改。是的。我明白了。你的答案和我想要的很接近。当然,我想要的是不可能的:因此我接受了它。