Picasso
和GridView
存在问题。有一些网址,我希望应用程序获取它们并在GridView
中显示它们。使用INTERNET permission
和New Activity
即可显示清单。
这是activity_gallery.xml
:
<GridView xmlns:android='http://schemas.android.com/apk/res/android'
android:id="@+id/gridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:layout_gravity="center"
/>
这是我的GalleryActivity
:
public class GalleryActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
GridView gridview = (GridView) findViewById(R.id.gridView);
gridview.setAdapter(new ImageAdapter(this));
}
}
而Adapter
就是ImageAdapter
:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
} else {
imageView = (ImageView) convertView;
}
Log.i("milad", "Before");
Picasso.with(mContext).load(mThumbIds[position]).centerCrop().into(imageView);
Log.i("milad", "After");
return imageView;
}
String[] mThumbIds =
{"http://30shahid.ir/Upload/pictures/gallery/2015/08/11/IMG_4995_e.jpg",
"http://30shahid.ir/Upload/pictures/gallery/2015/08/11/IMG_502kkk7.jpg",
"http://30shahid.ir/Upload/pictures/gallery/2015/08/11/IMG_49o74.jpg",
"http://30shahid.ir/Upload/pictures/gallery/2015/08/11/13940515184958618_PhotoL.jpg",
};
}
正如我的代码所提到的,我得到两个日志:
Picasso.with(mContext).load(mThumbIds[0]).centerCrop().into(imageView);
我得到Before
登录LOGCAT
,但没有After
日志。这意味着Picasso Line
存在问题
请帮助,谢谢。
答案 0 :(得分:0)
我猜你最近在GridView中提交图像时遇到了问题我最近使用了通用图像加载器库
private static class ImageAdapter extends BaseAdapter {
private final String[] IMAGE_URLS ;
private LayoutInflater inflater;
private DisplayImageOptions options;
ImageAdapter(Context context,String source[]) {
inflater = LayoutInflater.from(context);
IMAGE_URLS=source;
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.ico_place_holder)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
}
@Override
public int getCount() {
return IMAGE_URLS.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.item_grid_image, parent, false);
holder = new ViewHolder();
assert view != null;
holder.imageView = (ImageView) view.findViewById(R.id.image);
holder.progressBar = (ProgressBar) view.findViewById(R.id.progress);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
ImageLoader.getInstance()
.displayImage(IMAGE_URLS[position], holder.imageView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
holder.progressBar.setProgress(0);
holder.progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
holder.progressBar.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
holder.progressBar.setVisibility(View.GONE);
}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {
holder.progressBar.setProgress(Math.round(100.0f * current / total));
}
});
return view;
}
}
static class ViewHolder {
ImageView imageView;
ProgressBar progressBar;
}
愿这对你有所帮助。 享受编码。