Android笔记:GridView与ImageView
2008-03-27 13:30
585 查看
很久没有进行我的Android学习之旅了,今天抽空继续。
简单一点吧,就瞧瞧那个Grid的效果,Android提供了一个GridView,不过从APIDemo中看来,它似乎与PC上的GRID差别还是挺大的,更像那个IconView的感觉。不知道Android中如何实现表格界面?虽然在移动终端上,表格一般不会有谁使用,大家似乎更倾向于使用ListView,而Android对于ListView则有更简单的实现ListActivity。
废话不说,还是自己写几句代码来实验一下。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<GridView id="@+id/grid"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
android:layout_width="fill_parent"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
android:layout_height="fill_parent"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
android:padding="10dip"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
android:verticalSpacing="10"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
android:horizontalSpacing="10"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
android:numColumns="auto_fit"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
android:columnWidth="60"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
android:stretchMode="columnWidth"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
android:gravity="center"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
/>
从描述文件中的这些属性来看,与表格非常类似,除了padding和spacing以外,它还多了那个gravity,这里是center表示单元格中的内容居中放,在类GridView中也提供了方法setGravity(int)来实现这个效果。
接着,我们沿用以前那个fillMaps方法来构造SimpleAdapter,以前将这个adapter赋给ListActivity,现在同样的Adapter,却是赋给了GridView,效果又会是怎样呢?
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
List<HashMap<String, String>> items = fillMaps();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
GridView grd=(GridView)this.findViewById(R.id.grid);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
SimpleAdapter adapter=new SimpleAdapter(this,items,R.layout.list_row,new String[]...{"name"},new int[]...{R.id.item});
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
grd.setAdapter(adapter);
我觉得GridView并不象表格,倒更象IconView,下面试试用图像作为GridView的内容。现在,不能用简单Adapter了,得自己弄一个ImageAdapter,就让它衍生于BaseAdapter类吧。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class ImageAdapter extends BaseAdapter ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//这是资源ID的数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
private Integer[] mThumbIds = ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
R.drawable.a,R.drawable.b,R.drawable.c,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
R.drawable.d,R.drawable.e,R.drawable.f,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
R.drawable.g,R.drawable.h,R.drawable.i
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public ImageAdapter(Context c) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
mContext = c;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public int getCount() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return mThumbIds.length;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Object getItem(int position) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return position;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public long getItemId(int position) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return position;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public View getView(int position, View convertView, ViewGroup parent) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ImageView i = new ImageView(mContext);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//设置图像源于资源ID。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
i.setImageResource(mThumbIds[position]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
i.setAdjustViewBounds(true);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
i.setBackground(android.R.drawable.picture_frame);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Context mContext;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
很简单,只要重载几个方法就可以了,关键是那个getView方法,它负责构建出每个单元格中的对象实例。这里我们构造的是一个ImageView实例。
然后就是同样的将这个Adapter赋给GridView即可,大家可以看看效果,注意在做这个例子前,先放几个小图片到res/drawable目录下,buildproject一下就可以得到那个R.drawable.a了(这里的a是图像文件名,如a.png)。
在getView方法中我们使用了ImageView类,这又是一个widget。除了上面用到的几个方法以外,还有以下几个方法值得注意:
与图像来源有关的方法,我们只用了资源文件的方式。
//不同的图像来源
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public void setImageBitmap(Bitmap bm)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public void setImageDrawable(Drawable drawable)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public void setImageResource(int resid)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public void setImageURI(ContentURI uri)
图像效果的操作。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//颜色过滤
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public void setColorFilter(int color, Mode mode)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//矩阵变换
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public void setImageMatrix(Matrix matrix)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//透明度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public void setAlpha(int alpha)
具体的使用可以参考API,动手试一下就差不多了。
简单一点吧,就瞧瞧那个Grid的效果,Android提供了一个GridView,不过从APIDemo中看来,它似乎与PC上的GRID差别还是挺大的,更像那个IconView的感觉。不知道Android中如何实现表格界面?虽然在移动终端上,表格一般不会有谁使用,大家似乎更倾向于使用ListView,而Android对于ListView则有更简单的实现ListActivity。
废话不说,还是自己写几句代码来实验一下。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<GridView id="@+id/grid"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
android:layout_width="fill_parent"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
android:layout_height="fill_parent"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
android:padding="10dip"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
android:verticalSpacing="10"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
android:horizontalSpacing="10"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
android:numColumns="auto_fit"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
android:columnWidth="60"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
android:stretchMode="columnWidth"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
android:gravity="center"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
/>
从描述文件中的这些属性来看,与表格非常类似,除了padding和spacing以外,它还多了那个gravity,这里是center表示单元格中的内容居中放,在类GridView中也提供了方法setGravity(int)来实现这个效果。
接着,我们沿用以前那个fillMaps方法来构造SimpleAdapter,以前将这个adapter赋给ListActivity,现在同样的Adapter,却是赋给了GridView,效果又会是怎样呢?
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
List<HashMap<String, String>> items = fillMaps();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
GridView grd=(GridView)this.findViewById(R.id.grid);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
SimpleAdapter adapter=new SimpleAdapter(this,items,R.layout.list_row,new String[]...{"name"},new int[]...{R.id.item});
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
grd.setAdapter(adapter);
我觉得GridView并不象表格,倒更象IconView,下面试试用图像作为GridView的内容。现在,不能用简单Adapter了,得自己弄一个ImageAdapter,就让它衍生于BaseAdapter类吧。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class ImageAdapter extends BaseAdapter ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//这是资源ID的数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
private Integer[] mThumbIds = ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
R.drawable.a,R.drawable.b,R.drawable.c,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
R.drawable.d,R.drawable.e,R.drawable.f,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
R.drawable.g,R.drawable.h,R.drawable.i
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public ImageAdapter(Context c) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
mContext = c;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public int getCount() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return mThumbIds.length;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Object getItem(int position) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return position;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public long getItemId(int position) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return position;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public View getView(int position, View convertView, ViewGroup parent) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ImageView i = new ImageView(mContext);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//设置图像源于资源ID。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
i.setImageResource(mThumbIds[position]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
i.setAdjustViewBounds(true);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
i.setBackground(android.R.drawable.picture_frame);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Context mContext;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
很简单,只要重载几个方法就可以了,关键是那个getView方法,它负责构建出每个单元格中的对象实例。这里我们构造的是一个ImageView实例。
然后就是同样的将这个Adapter赋给GridView即可,大家可以看看效果,注意在做这个例子前,先放几个小图片到res/drawable目录下,buildproject一下就可以得到那个R.drawable.a了(这里的a是图像文件名,如a.png)。
在getView方法中我们使用了ImageView类,这又是一个widget。除了上面用到的几个方法以外,还有以下几个方法值得注意:
与图像来源有关的方法,我们只用了资源文件的方式。
//不同的图像来源
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public void setImageBitmap(Bitmap bm)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public void setImageDrawable(Drawable drawable)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public void setImageResource(int resid)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public void setImageURI(ContentURI uri)
图像效果的操作。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//颜色过滤
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public void setColorFilter(int color, Mode mode)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//矩阵变换
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public void setImageMatrix(Matrix matrix)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//透明度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public void setAlpha(int alpha)
具体的使用可以参考API,动手试一下就差不多了。
相关文章推荐
- Android开发笔记----禁止GridView进行上下滑动
- Android GridView学习笔记。
- Android开发总结笔记 GridView 网格视图 1-3-4
- Android 应用开发笔记 - 网格视图(GridView)
- Android工作学习笔记之图片自适应imageview属性android:scaleType
- Android 学习笔记(十三):Activity-GridView
- Android入门笔记 - 界面开发 - GridView, BaseAdapter
- Android 学习笔记(十三):Activity-GridView
- Android Widget——GridView 学习笔记
- Android GridView如何固定间隔,item能自适应大小(笔记)
- Android开发学习笔记:Gallery和GridView浅析
- Android的listview、gridview 笔记
- Android工作学习笔记之图片自适应imageview属性android:scaleType
- Android笔记(二十二) Android中的GridView
- Android开发笔记——ScrollView中解决listview,gridview显示不全问题
- Android控件学习笔记之 GridView(实现九宫格)
- 【android控件学习笔记】GridView表格形式显示多张图片
- 善知教育笔记之Android_GridLayout和GridView
- Android开发学习笔记:Gallery和GridView浅析
- Android 学习笔记(十三):Activity-GridView