2013-04-03 66 views
0
private Context mContext; 
private int[] colors = new int[] { Color.WHITE, 0x30aaaaaa }; 
private int[] dotColors = new int[7]; 
private List<Integer> sta; 
private TasksDataSource datasource; 
private int con=1000; 
public Integer[] mThumbIds = { 
     R.drawable.green, R.drawable.green, 
     R.drawable.green, R.drawable.green, 
     R.drawable.green, R.drawable.green, 
     R.drawable.green, R.drawable.green, 
     R.drawable.green, R.drawable.green, 
     R.drawable.green, R.drawable.green, 
     R.drawable.green, R.drawable.green, 
     R.drawable.green 
    }; 
public ImageAdapter(Context context, List<Integer> content){ 
sta = content; 
datasource = new TasksDataSource(context); //here 
datasource.open(); 
mContext=context; 
} 
@Override 
public int getCount() { 
    return mThumbIds.length; 
} 
@Override 
public Object getItem(int position) { 
    return mThumbIds[position]; 
} 
@Override 
public long getItemId(int position) { 
    return 0; 
} 
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    int[] st = new int[sta.size()]; 
    ImageView imageView = new ImageView(mContext); 
    imageView.setImageResource(mThumbIds[position]); 
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
    imageView.setLayoutParams(new GridView.LayoutParams(20, 20)); 
    return imageView; 
} 

我有動態intialisation一個環,其具有在前面的代碼中的一些改變動態地顯示多個圖像..我怎樣才能在網格視圖

public Integer[] mThumbIds = new Integer[con]; 
for(int i = 0;i < st.length;i++) 
     { 
     st[i] = sta.get(i); 
     Log.i("st::" + st[i]," "); 

     } 
     int ii=0,jj=0; 
     while(ii<con) 
     { 
      if(st[jj]==0) 
      { 
       mThumbIds[ii]=R.drawable.red; 
      } 
      else if(st[jj]==1) 
      { 
       mThumbIds[ii]=R.drawable.green; 
      } 
      else 
      { 
       mThumbIds[ii]=R.drawable.grey; 
      } 
     } 

上述加成din't工作和運行無限地停下來。 我想要的是,當mThumbIds [ii]是1時,顯示紅色img wen mThumbIds [ii]是0,綠色,當mThumbIds [ii]是2時是綠色,我怎麼能實現這個?

回答

0

使用通用圖像加載器。

https://github.com/nostra13/Android-Universal-Image-Loader

它基於Lazy List(基於相同原理)。可以在本地加載圖片或組成服務器。 但它有很多其他配置。我寧願使用通用圖像加載程序因爲它給你更多的配置選項。如果downlaod失敗,您可以顯示錯誤圖像。可以顯示圓角的圖像。可以緩存在光盤或內存上。可以壓縮圖像。

在您的自定義適配器構造

public Integer[] mThumbIds = { 
    R.drawable.red, R.drawable.green, 
    R.drawable.grey, ..... 
    }; 
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()

按照postition顯示圖像。

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

您可以使用其他選項進行配置以滿足您的需求。

隨着通用圖像加載器,您可以查看持有人的平滑滾動和性能。 http://developer.android.com/training/improving-layouts/smooth-scrolling.html

+0

當mThumbIds是一個整型並要求輸入字符串時,如何使用displayImage(mThumbIds [position])。嘗試時出現錯誤。 –

+0

是的,它需要int值 – Raghunandan

+0

@紅色檢查https://github.com/nostra13/Android-Universal-Image-Loader – Raghunandan