2013-03-28 111 views
1

Iam開發Android應用程序,顯示圖像&與gridview顯示的圖像&現在我做了gridview圖像顯示drawable.I從json中獲取文本值arraylist變量。gridview與動態文本視圖和可繪製圖像

代碼我用於顯示僅適用於圖像:從含有15 values.when沒有從mThumbIds(即,textvalues>圖像)圖像,那麼它應該把我的默認一個異步任務

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_front_page); 


    GridView grid=(GridView)findViewById(R.id.maincatgrid); 

    grid.setAdapter(new ImageAdapter(this)); 
    grid.setColumnWidth(170); 
    grid.setVerticalSpacing(20); 
    grid.setStretchMode(GridView.STRETCH_COLUMN_WIDTH); 
    grid.setNumColumns(GridView.AUTO_FIT); 


} 

     public class ImageAdapter extends BaseAdapter { 

    private Context mContext; 


    public ImageAdapter(Context c){ 
     mContext = c; 
    } 

     public Integer[] mThumbIds = { 
       R.drawable.image1, 
       R.drawable.image2, 
       R.drawable.image3, 
       R.drawable.image4, 
       R.drawable.image5, 
       R.drawable.image6, 
       R.drawable.image7, 
       R.drawable.image8, 


     }; 

    public int getCount() { 
     // TODO Auto-generated method stub 
     return mThumbIds.length; 
    } 

    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return mThumbIds[position]; 
    } 

    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 


    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View MyView = convertView; 
     // TODO Auto-generated method stub 

     ImageView imageView; 
     if (convertView == null) { 

      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(170, 150)); 
      imageView.setAdjustViewBounds(false); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 



     } else { 
      imageView = (ImageView) convertView; 
     } 

     imageView.setImageResource(mThumbIds[position]); 

     return imageView; 
    } 



} 
} 

蔭得到的TextView值圖像R.drawable.no_image.Is有任何方法來實現這個然後PLZ建議我。提前感謝。

+0

使用通用圖像加載程序。 – Raghunandan

+0

Thankyou.can你提供給我的鏈接? – Neeha

回答

1

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

可以從本地或從服務器加載圖像。

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

在您的自定義適配器構造

File cacheDir = StorageUtils.getOwnCacheDirectory(activity context, "your folder");//for caching 

// 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 untik image is loaded 
.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