Android UI学习 - GridView和ImageView的使用
http://www.drovik.com/ 2012-9-19 21:21:35 来源:百度搜索 点击:
GridView: A view that shows items in two-dimensional scrolling grid. The items in the grid come from the ListAdapter associated with this view. 简单说,GridView就是我们资源管理器平常见到的一个个文件的icon显示方式。
上面提及到了,GridView的Item是来自ListAdapter的,所以一般在Activity的onCreate使用GridView的代码:
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.grid_2);
- GridView g = (GridView) findViewById(R.id.myGrid);
- g.setAdapter(new ImageAdapter(this));
- }
而ImageAdapter一般是extends BaseAdapter。BaseAdapter是implements ListAdapter SpinnerAdapter,但很多时候自定义的Adapter都是override ListAdapter的父类Adapter接口里面的方法:
int getCount() 获取当前Adapter的Items数目
Object getItem(int position) 获取相应position的Item
long getItemId(int position) 获取相应position的Item在List中的row id
View getView(int position, View convertView, ViewGroup parent) 获取在指定position所要显示的data的View
这些方法函数和swing的差不多,都是基于MVC。大概原理过程是这样的:程序需要显示GridView,那么要把data一个一个地显示出来是通过一个for循环,首先call Adapter.getCount()得到有多少个data,然后position++地getItem,getView得到要显示的view,这样子逐一地显示出来!
下面是官方sample里面的Photo Grid的例子,本人省略了某些代码:
- public class ImageAdapter extends BaseAdapter {
- public ImageAdapter(Context c) {
- mContext = c;
- }
- public int getCount() {
- return mThumbIds.length;
- }
- public Object getItem(int position) {
- return position;
- }
- public long getItemId(int position) {
- return position;
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- ImageView imageView;
- if (convertView == null) {
- imageView = new ImageView(mContext);
- imageView.setLayoutParams(new GridView.LayoutParams(45, 45));//设置ImageView宽高
- imageView.setAdjustViewBounds(false);
- imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
- imageView.setPadding(8, 8, 8, 8);
- } else {
- imageView = (ImageView) convertView;
- }
- imageView.setImageResource(mThumbIds[position]);
- return imageView;
- }
- private Context mContext;
- private Integer[] mThumbIds = {
- R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
- R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
- R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
- R.drawable.sample_thumb_6, R.drawable.sample_thumb_7
- };
- }
留意getView里面的代码,要判断convertView是否为null,以便重用,减少对象的创建,减少内存占用。
XML布局文件内容,原来就只是指明GridView:
- <?xml version="1.0" encoding="utf-8"?>
- <GridView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/myGrid"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:padding="10dp"
- android:verticalSpacing="10dp"
- android:horizontalSpacing="10dp"
- android:numColumns="auto_fit"
- android:columnWidth="60dp"
- android:stretchMode="columnWidth"
- android:gravity="center"
- />
可以看到getView,和ImageView是重点,影响图片的显示效果。而且发现列数是不确定的,取决于每个ImageView的宽度和屏幕的宽度。接下来看看ImageView。
ImageView:Displays an arbitrary image, such as an icon. The ImageView class can load images from various sources (such as resources or content providers), takes care of computing its measurement from the image so that it can be used in any layout manager, and provides various display options such as scaling and tinting。 ImageView就是用来显示Image,icon的。
这里我们重点理解ImageView的属性android:scaleType,即ImageView.setScaleType(ImageView.ScaleType)。android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType / android:scaleType值的意义区别:
CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示
CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽)
CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽
FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示
FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置
FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置
FIT_XY / fitXY 把图片不按比例扩大/缩小到View的大小显示
MATRIX / matrix 用矩阵来绘制
一开始我不明白MATRIX矩阵,网上搜索后发现原来MATRIX矩阵可以动态缩小放大图片来显示,这里不展开深入的了解,只是贴出相关语句,缩小图片:
- //获得Bitmap的高和宽
- int bmpWidth=bmp.getWidth();
- int bmpHeight=bmp.getHeight();
- //设置缩小比例
- double scale=0.8;
- //计算出这次要缩小的比例
- scaleWidth=(float)(scaleWidth*scale);
- scaleHeight=(float)(scaleHeight*scale);
- //产生resize后的Bitmap对象
- Matrix matrix=new Matrix();
- matrix.postScale(scaleWidth, scaleHeight);
- Bitmap resizeBmp=Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true);
应用ImageView的例子很多,看看上次FrameLayout里面的:
- <ImageView
- android:id="@+id/image"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:scaleType="center"
- android:src="@drawable/candle"
- />
** 要注意一点,我发现Drawable文件夹里面的图片命名是不能大写的。
本文出自 “学习Android” 博客,请务必保留此出处http://android.blog.51cto.com/268543/316255
发表评论(2)
- 1楼 cartier watches knockoff 发表于 2015-3-30 19:22:30
- Yup, it definitely works, just takes patience. Glad you’re having a good experience! cartier watches knockoff http://www.vinbottle.com/home.php?mod=space&uid=7626
- 2楼 link 发表于 2015-9-18 0:38:25
- Today I received a watch. That guy is not my favorite, just have him as friend, good friends. [url=http://www.shoppingbookmarks.com/items/high-end-store-replica-cartier-watches-fake-cartier-jewelry-cartier-te/]link[/url] link
查看更多评论>>>