Android 将图像从 Url 加载到 GridView
Android Load image from Url to GridView
在CustomAdapter.class中:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(4, 4, 4, 4);
} else
{
imageView = (ImageView) convertView;
}
String[] mThumbIds =
{ "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg",
"https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s1024/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg",
"https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s1024/Another%252520Rockaway%252520Sunset.jpg",
};
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
I get this error: *The method setImageResource(int) in the type ImageView is not applicable for the arguments (String)*
我这样称呼CustomAdapter.class:
public static class ImageFrg extends Fragment
{
GridView gridview;
DisplayImageOptions options;
public ImageFrg () {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.imageFrg, container, false);
gridview=(GridView) rootView.findViewById(R.id.gridview);
CustomAdapter i = new CustomAdapter(getActivity());
gridview.setAdapter(i);
return rootView;
}
}
我想将 url 中的一些图像上传到 GridView 中。我如何修改 setImagesResource,以便能够用图像填充 Gridview?
您可以使用 Picasso 来做到这一点。
来自他们页面的示例:
Picasso.with(context).load("http://<url to your image>").into(imageView);
最简单的方法是使用 Picasso.
Picasso.with(getContext()).load(mThumbId[position]).centerCrop().into(imageView);
编辑
String[] mThumbIds =
{ "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg",
"https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s1024/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg",
"https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s1024/Another%252520Rockaway%252520Sunset.jpg",
};
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(4, 4, 4, 4);
}
else {
imageView = (ImageView) convertView;
}
Picasso.with(mContext).load(mThumbs[position]).centerCrop().into(imageView);
return imageView;
}
@Override
public int getCount() {
return mThumbId.size();
}
从 URL 下载图像的最简单方法是,使用 AQuery 库下载 AQuery.jar 文件并将其放入您的 Llib 文件夹
//load an image to an ImageView from network
AQuery aq=new AQuery(this);
aq.id(R.id.imageview).image("http://www.vikispot.com/z/images/vikispot/android-w.png");
参考这个以获取更多信息
https://code.google.com/p/android-query/wiki/ImageLoading
在 CustomAdapter.class 上:
String[] mThumbIds =
{ "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg",
"https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s1024/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg",
"https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s1024/Another%252520Rockaway%252520Sunset.jpg",
};
Picasso.with(mContext).load(mThumbIds[0]).centerCrop().into(imageView);
return imageView;
.
private Integer[] mThumbIds =
{
R.drawable.ic_stub , R.drawable.ic_stub,
R.drawable.ic_stub , R.drawable.ic_stub,
R.drawable.ic_stub , R.drawable.ic_stub ,
R.drawable.ic_stub , R.drawable.ic_stub,
R.drawable.ic_stub , R.drawable.ic_stub,
R.drawable.ic_stub , R.drawable.ic_stub,
};
现在,第一张图片打印了 6 次!
在CustomAdapter.class中:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(4, 4, 4, 4);
} else
{
imageView = (ImageView) convertView;
}
String[] mThumbIds =
{ "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg",
"https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s1024/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg",
"https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s1024/Another%252520Rockaway%252520Sunset.jpg",
};
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
I get this error: *The method setImageResource(int) in the type ImageView is not applicable for the arguments (String)*
我这样称呼CustomAdapter.class:
public static class ImageFrg extends Fragment
{
GridView gridview;
DisplayImageOptions options;
public ImageFrg () {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.imageFrg, container, false);
gridview=(GridView) rootView.findViewById(R.id.gridview);
CustomAdapter i = new CustomAdapter(getActivity());
gridview.setAdapter(i);
return rootView;
}
}
我想将 url 中的一些图像上传到 GridView 中。我如何修改 setImagesResource,以便能够用图像填充 Gridview?
您可以使用 Picasso 来做到这一点。
来自他们页面的示例:
Picasso.with(context).load("http://<url to your image>").into(imageView);
最简单的方法是使用 Picasso.
Picasso.with(getContext()).load(mThumbId[position]).centerCrop().into(imageView);
编辑
String[] mThumbIds =
{ "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg",
"https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s1024/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg",
"https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s1024/Another%252520Rockaway%252520Sunset.jpg",
};
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(4, 4, 4, 4);
}
else {
imageView = (ImageView) convertView;
}
Picasso.with(mContext).load(mThumbs[position]).centerCrop().into(imageView);
return imageView;
}
@Override
public int getCount() {
return mThumbId.size();
}
从 URL 下载图像的最简单方法是,使用 AQuery 库下载 AQuery.jar 文件并将其放入您的 Llib 文件夹
//load an image to an ImageView from network
AQuery aq=new AQuery(this);
aq.id(R.id.imageview).image("http://www.vikispot.com/z/images/vikispot/android-w.png");
参考这个以获取更多信息 https://code.google.com/p/android-query/wiki/ImageLoading
在 CustomAdapter.class 上:
String[] mThumbIds =
{ "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg",
"https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s1024/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg",
"https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s1024/Another%252520Rockaway%252520Sunset.jpg",
};
Picasso.with(mContext).load(mThumbIds[0]).centerCrop().into(imageView);
return imageView;
.
private Integer[] mThumbIds =
{
R.drawable.ic_stub , R.drawable.ic_stub,
R.drawable.ic_stub , R.drawable.ic_stub,
R.drawable.ic_stub , R.drawable.ic_stub ,
R.drawable.ic_stub , R.drawable.ic_stub,
R.drawable.ic_stub , R.drawable.ic_stub,
R.drawable.ic_stub , R.drawable.ic_stub,
};
现在,第一张图片打印了 6 次!