2013-03-28 50 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