2013-05-14 35 views

回答

0

你需要解析你的json响应获取图像的URL。然后使用grdiview和自定义适配器。将url传递给自定义适配器构造函数。

为了解析JSON使用GSON

https://code.google.com/p/google-gson/

教程同样

http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html

您可以使用懒列表或通用ImageLoader的。

图像可以缓存到本地SD卡或手机mmeory。网址被认为是关键。如果密钥存在于sdcard中,则显示来自SD卡的图像,否则通过从服务器下载显示图像并将其缓存到您选择的位置。缓存限制可以设置。您也可以选择自己的位置来缓存图像。缓存也可以被清除。

而不是用户等待下载大图像,然后显示懒惰列表按需加载图像。由于缓存的图像区域可以离线显示图像。

https://github.com/thest1/LazyList。懒列表

在你getview

imageLoader.DisplayImage(imageurl, imageview); 
ImageLoader Display method 

public void DisplayImage(String url, ImageView imageView) //url and imageview as parameters 
{ 
imageViews.put(imageView, url); 
Bitmap bitmap=memoryCache.get(url); //get image from cache using url as key 
if(bitmap!=null)   //if image exists 
    imageView.setImageBitmap(bitmap); //dispaly iamge 
else //downlaod image and dispaly. add to cache. 
{ 
    queuePhoto(url, imageView); 
    imageView.setImageResource(stub_id); 
} 

} 懒列表的替代方案是通用图像装载机

https://github.com/nostra13/Android-Universal-Image-Loader。它基于Lazy List(基于相同原理)。但它有很多其他配置。我宁愿使用通用图像加载程序因为它给你更多的配置选项。如果downlaod失败,您可以显示错误图像。可以显示圆角的图像。可以缓存在光盘或内存上。可以压缩图像。

在您的自定义适配器构造

File cacheDir = StorageUtils.getOwnCacheDirectory(a, "your folder"); 

// Get singletone instance of ImageLoader 
imageLoader = ImageLoader.getInstance(); 
// Create configuration for ImageLoader (all options are optional) 
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a) 
     // You can pass your own memory cache implementation 
    .discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation 
    .discCacheFileNameGenerator(new HashCodeFileNameGenerator()) 
    .enableLogging() 
    .build(); 
// Initialize ImageLoader with created configuration. Do it once. 
imageLoader.init(config); 
options = new DisplayImageOptions.Builder() 
.showStubImage(R.drawable.stub_id)//display stub image 
.cacheInMemory() 
.cacheOnDisc() 
.displayer(new RoundedBitmapDisplayer(20)) 
.build(); 

在你getView()

ImageView image=(ImageView)vi.findViewById(R.id.imageview); 
imageLoader.displayImage(imageurl, image,options);//provide imageurl, imageview and options 

你可以用其他选项配置,以满足您的需求。

随着延迟加载/通用图像加载器,您可以查看持有人的平滑滚动和性能。 http://developer.android.com/training/improving-layouts/smooth-scrolling.html

+0

您好我使用lazylist概念并在gridview中预览图像。 – Wishy 2013-05-31 07:14:37

+0

好的,上面有帮助吗? DId它解决你的问题? – Raghunandan 2013-05-31 07:15:29

+0

我想问一个更多的查询,我想选择onClick Gridview的图像,并在下一个活动预览它。 – Wishy 2013-05-31 07:15:37

0

您可以从JSON获取图像URL并使用ASyncTask下载图像。如果你愿意,你可以将视图传递给asynctask,并且从后续操作中,你可以设置其图像源