我正在使用Picasso库在GridView中加载图像。这是我的代码。首先我有一个包含String数组链接的类。为了实验目的,我使用相同的图像。
public class Data {
static final String[] URLS = {
"http://www.panoramio.com/photo/116726502",
"http://www.panoramio.com/photo/116726502",
"http://www.panoramio.com/photo/116726502",
"http://www.panoramio.com/photo/116726502",
"http://www.panoramio.com/photo/116726502",
"http://www.panoramio.com/photo/116726502",
"http://www.panoramio.com/photo/116726502",
};
private Data() {
// No instances.
}
}
接下来,我有MainActivity类,它可以执行常规操作,即初始化GridView等。
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gv = (GridView) findViewById(R.id.grid_view);
gv.setAdapter(new SampleGridViewAdapter(this));
gv.setOnScrollListener(new SampleScrollListener(this));
}
}
最后我有我创建的GridViewAdapter。
final class SampleGridViewAdapter extends BaseAdapter {
private final Context context;
private final List<String> urls = new ArrayList<String>();
public SampleGridViewAdapter(Context context) {
this.context = context;
// Ensure we get a different ordering of images on each run.
Collections.addAll(urls, Data.URLS);
Collections.shuffle(urls);
// Triple up the list.
ArrayList<String> copy = new ArrayList<String>(urls);
urls.addAll(copy);
urls.addAll(copy);
}
@Override public View getView(int position, View convertView, ViewGroup
parent) {
SquaredImageView view = (SquaredImageView) convertView;
if (view == null) {
view = new SquaredImageView(context);
view.setScaleType(CENTER_CROP);
}
// Get the image URL for the current position.
String url = getItem(position);
// Trigger the download of the URL asynchronously into the image view.
Picasso.with(context) //
.load(url) //
.placeholder(R.drawable.placeholder) //
.error(R.drawable.error) //
.fit() //
.tag(context) //
.into(view);
return view;
}
@Override public int getCount() {
return urls.size();
}
@Override public String getItem(int position) {
return urls.get(position);
}
@Override public long getItemId(int position) {
return position;
}
}
答案 0 :(得分:1)
您传递的网址不是实际的图片网址,而是网页。
我检查并提取了其中一个网址的图片网址。使用它会起作用。 http://static.panoramio.com/photos/large/116726502.jpg
确保在应用中加载图片时有实际的图片网址
答案 1 :(得分:0)
您可以尝试将网址更改为真实的照片网址
e.g。
http://static.panoramio.com/photos/large/116726502.jpg
答案 2 :(得分:0)
您的网址数组不正确..请检查并制作有效的网址数组。那个数组可能就像..
static final String[] URLS = {
"http://www.panoramio.com/photo/116726502.jpg",
"http://www.panoramio.com/photo/116726502.jpg",
"http://www.panoramio.com/photo/116726502.jpg",
"http://www.panoramio.com/photo/116726502.jpg",
"http://www.panoramio.com/photo/116726502.jpg",
"http://www.panoramio.com/photo/116726502.jpg",
"http://www.panoramio.com/photo/116726502.jpg",
};
private Data() {
// No instances.
}
}
答案 3 :(得分:0)
确定。我找到了一些时间来解决这个问题。
public class MainActivity extends Activity {
private GridView gridView;
private GridviewAdapter gridAdapter;
String[] items = {
"http://static.panoramio.com/photos/large/116726502.jpg",
"http://static.panoramio.com/photos/large/116726502.jpg",
"http://static.panoramio.com/photos/large/116726502.jpg",
"http://static.panoramio.com/photos/large/116726502.jpg",
"http://static.panoramio.com/photos/large/116726502.jpg",
"http://static.panoramio.com/photos/large/116726502.jpg",
"http://static.panoramio.com/photos/large/116726502.jpg",
"http://static.panoramio.com/photos/large/116726502.jpg",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView);
gridAdapter = new GridviewAdapter(MainActivity.this, items);
gridView.setAdapter(gridAdapter);
}
}
还有一个适用于通常工作的适配器。
public class GridviewAdapter extends BaseAdapter{
private Context context;
private String[] items;
public GridviewAdapter(Context context, String[] items){
super();
this.context = context;
this.items = items;
}
@Override
public int getCount() {
return items.length;
}
@Override
public Object getItem(int position) {
return items[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView img = null;
if (convertView == null) {
img = new ImageView(context);
convertView = img;
img.setPadding(5, 5, 5, 5);
} else {
img = (ImageView) convertView;
}
Picasso.with(context)
.load(items[position])
.placeholder(R.drawable.picture)
.resize(200, 200)
.into(img);
return convertView;
}
}
希望这对某人有用......