gridview with dynamic textview&可绘图像

时间:2013-03-28 12:58:41

标签: android gridview

开发一款Android应用程序,显示图像的网格视图和关于那个文本视图。现在我用gridable显示了来自drawable的图像。我在arraylist变量中得到了json的文本值。

代码我以前只用于显示图像:

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;
    }



}
 }

我从包含15个值的异步任务中获取textview值。当没有来自mThumbIds的图像(即textvalues>图像)时,它应该将我的默认图像R.drawable.no_image.Is有任何方法来实现这一点然后plz建议我。谢谢。

1 个答案:

答案 0 :(得分:1)

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

可以在本地或从服务器加载图像。

它基于Lazy List(基于相同的原理)。但它有很多其他配置。我更喜欢使用Universal Image Loader,它为您提供了更多配置选项。如果下载失败,您可以显示错误图像。可以显示带圆角的图像。可以缓存在光盘或内存上。可压缩图像。

在自定义适配器构造函数

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