2013-05-14 28 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,並且從後續操作中,你可以設置其圖像源