Android 画廊控件Gallary。将图片显示成连续的带状。

 

package com.gallerydemo;

import java.lang.reflect.Field;
import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class MainActivity extends Activity {

/*************************************
* 画廊控件的使用Gallery
*
* 1.Gallery 显示的是一个水平的列表选择框允许用户通过拖动来显示上一个下一个列表项 2.使用步骤:a:定义布局文件 b:定义数据适配器类
* c:关联Gallery控件和数据适配器 d:为Gallery每个item添加事件。
* 3.由于使用大量图片可能出现内存溢出,API16已经废弃,HorizontableScroolView和ViewPager
*
************************************/

private Gallery gallery; // 在API 16中已经不再支持

private String TAG = "GalleryDemo";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

gallery = (Gallery) findViewById(R.id.gallery);

// 关联Gallery和数据适配器
try {
gallery.setAdapter(new ImageAdapter(this));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i(TAG, "====5");

gallery.setOnItemClickListener(new OnItemClickListener() {

// @Override
/*
* parent:指向数据适配器的指针 v:对应于item view的句柄
* position:item在数据适配器adapter中的位置 id:item在数据适配器的第几行
*/
public void onItemClick(AdapterView parent, View v, int position,
long id) {
// TODO Auto-generated method stub
MainActivity.this.setTitle(String.valueOf(position));
Log.i(TAG, "position: " + position + "id: " + id);
}
});

}

/*
* BaseAdapter就Android应用程序中经常用到的基础数据适配器,它的主要用途是将一组数据传到像ListView、Spinner、
* Gallery及GridView等UI显示组件,它是继承自接口类Adapter, 有关BaseAdapter
*
*/
// 为Gallery提供数据源
private class ImageAdapter extends BaseAdapter {

private Context mContext;
private ArrayList<Integer> imgList = new ArrayList<Integer>();
private ArrayList<Object> imgSize = new ArrayList<Object>();

// 建立与资源之间的关联
public ImageAdapter(Context context) throws IllegalArgumentException,
IllegalAccessException {
mContext = context;

// 利用反射机制来获取资源中的图片ID和尺寸
Field[] fields = R.drawable.class.getDeclaredFields();

for (Field field : fields) {

if (!"ic_launcher".equals(field.getName()))// 除了icon之外的图片
{
int index = field.getInt(R.drawable.class);

imgList.add(index);// 这里把int 转换成了Integer
int size[] = new int[2];
Bitmap bmImg = BitmapFactory.decodeResource(getResources(),
index);
size[0] = bmImg.getWidth();
size[1] = bmImg.getHeight();
imgSize.add(size);
}
}
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return imgList.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// int是JAVA的一个基本类型,而Integer是JAVA的一个类,对应
// int。因为在某些地方不可以用int而要用Integer。而且基本类型运算的速度也要快。
ImageView i = new ImageView(mContext);
i.setImageResource(imgList.get(position).intValue());
i.setScaleType(ImageView.ScaleType.FIT_XY);

int size[] = new int[2];
size = (int[]) imgSize.get(position);

i.setLayoutParams(new Gallery.LayoutParams(size[0], size[1]));

return i;
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

资源文件main.xml

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></Gallery>

</LinearLayout>