« | September 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | |
| 公告 |
本博客在此声明所有文章均为转摘,只做资料收集使用。并无其他商业用途。 |
Blog信息 |
blog名称: 日志总数:210 评论数量:205 留言数量:-19 访问次数:920827 建立时间:2007年5月10日 |

| |
[Android]Android学习笔记(8) - GridView与ImageView 文章收藏, 网上资源, 软件技术, 电脑与网络
李小白 发表于 2008/10/31 16:11:32 |
颜承ID:sharetop
http://blog.csdn.net/sharetop/archive/2008/02/15/2097399.aspx
很久没有进行我的Android学习之旅了,今天抽空继续。
简单一点吧,就瞧瞧那个Grid的效果,Android提供了一个GridView,不过从APIDemo中看来,它似乎与PC上的GRID差别还是挺大的,更像那个IconView的感觉。不知道Android中如何实现表格界面?虽然在移动终端上,表格一般不会有谁使用,大家似乎更倾向于使用ListView,而Android对于ListView则有更简单的实现ListActivity。
废话不说,还是自己写几句代码来实验一下。
500)this.width=500'><GridView id="@+id/grid"500)this.width=500'> android:layout_width="fill_parent" 500)this.width=500'> android:layout_height="fill_parent"500)this.width=500'> android:padding="10dip"500)this.width=500'> android:verticalSpacing="10"500)this.width=500'> 500)this.width=500'> android:horizontalSpacing="10"500)this.width=500'> android:numColumns="auto_fit"500)this.width=500'> android:columnWidth="60"500)this.width=500'> android:stretchMode="columnWidth"500)this.width=500'> 500)this.width=500'> android:gravity="center"500)this.width=500'> />
从描述文件中的这些属性来看,与表格非常类似,除了padding和spacing以外,它还多了那个gravity,这里是center表示单元格中的内容居中放,在类GridView中也提供了方法setGravity(int)来实现这个效果。
接着,我们沿用以前那个fillMaps方法来构造SimpleAdapter,以前将这个adapter赋给ListActivity,现在同样的Adapter,却是赋给了GridView,效果又会是怎样呢?
500)this.width=500'>List<HashMap<String, String>> items = fillMaps();500)this.width=500'>500)this.width=500'>GridView grd=(GridView)this.findViewById(R.id.grid);500)this.width=500'>500)this.width=500'>SimpleAdapter adapter=new SimpleAdapter(this,items,R.layout.list_row,new String[]...{"name"},new int[]...{R.id.item});500)this.width=500'>grd.setAdapter(adapter);
我觉得GridView并不象表格,倒更象IconView,下面试试用图像作为GridView的内容。现在,不能用简单Adapter了,得自己弄一个ImageAdapter,就让它衍生于BaseAdapter类吧。
500)this.width=500'>500)this.width=500'>public class ImageAdapter extends BaseAdapter ...{500)this.width=500'> //这是资源ID的数组500)this.width=500'>500)this.width=500'> private Integer[] mThumbIds = ...{500)this.width=500'> R.drawable.a,R.drawable.b,R.drawable.c,500)this.width=500'> R.drawable.d,R.drawable.e,R.drawable.f,500)this.width=500'> R.drawable.g,R.drawable.h,R.drawable.i500)this.width=500'> };500)this.width=500'> 500)this.width=500'>500)this.width=500'> public ImageAdapter(Context c) ...{500)this.width=500'> mContext = c;500)this.width=500'> }500)this.width=500'>500)this.width=500'>500)this.width=500'> public int getCount() ...{500)this.width=500'> return mThumbIds.length;500)this.width=500'> }500)this.width=500'>500)this.width=500'>500)this.width=500'> public Object getItem(int position) ...{500)this.width=500'> return position;500)this.width=500'> }500)this.width=500'>500)this.width=500'>500)this.width=500'> public long getItemId(int position) ...{500)this.width=500'> return position;500)this.width=500'> }500)this.width=500'>500)this.width=500'>500)this.width=500'> public View getView(int position, View convertView, ViewGroup parent) ...{500)this.width=500'> ImageView i = new ImageView(mContext);500)this.width=500'> //设置图像源于资源ID。500)this.width=500'> i.setImageResource(mThumbIds[position]);500)this.width=500'> i.setAdjustViewBounds(true); 500)this.width=500'> i.setBackground(android.R.drawable.picture_frame);500)this.width=500'>500)this.width=500'> return i;500)this.width=500'> }500)this.width=500'> 500)this.width=500'> private Context mContext;500)this.width=500'>500)this.width=500'> }
很简单,只要重载几个方法就可以了,关键是那个getView方法,它负责构建出每个单元格中的对象实例。这里我们构造的是一个ImageView实例。
然后就是同样的将这个Adapter赋给GridView即可,大家可以看看效果,注意在做这个例子前,先放几个小图片到res/drawable目录下,buildproject一下就可以得到那个R.drawable.a了(这里的a是图像文件名,如a.png)。
在getView方法中我们使用了ImageView类,这又是一个widget。除了上面用到的几个方法以外,还有以下几个方法值得注意:
与图像来源有关的方法,我们只用了资源文件的方式。
//不同的图像来源500)this.width=500'>public void setImageBitmap(Bitmap bm) 500)this.width=500'>public void setImageDrawable(Drawable drawable) 500)this.width=500'>public void setImageResource(int resid) 500)this.width=500'>public void setImageURI(ContentURI uri)
图像效果的操作。
500)this.width=500'>//颜色过滤500)this.width=500'>public void setColorFilter(int color, Mode mode) 500)this.width=500'>//矩阵变换500)this.width=500'>public void setImageMatrix(Matrix matrix) 500)this.width=500'>//透明度500)this.width=500'>public void setAlpha(int alpha)
具体的使用可以参考API,动手试一下就差不多了。 |
|
|