1、布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10px"
android:horizontalSpacing="3px"
android:numColumns="4"
android:verticalSpacing="3px" />
</LinearLayout>
2、MainActivity中,定义一个用于保存要显示图片id的数组
public int[] imageId = new int[]{R.drawable.head01,R.drawable.head02,R.drawable.head03,R.drawable.head04,
R.drawable.head05,R.drawable.head06,R.drawable.head07,R.drawable.head08,
R.drawable.head09,R.drawable.head10,R.drawable.head11,R.drawable.head12};//定义并初始化保存图片id的数组
3、在MainActivity的onCreate()方法中
GridView gridView = (GridView)findViewById(R.id.gridView1);//获取GridView组件
BaseAdapter adapter = new BaseAdapter() {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;//声明ImageView的对象
if(convertView==null){
imageView = new ImageView(MainActivity.this);//实例化ImageView对象
//设置图像的宽度和高度
imageView.setAdjustViewBounds(true);
imageView.setMaxWidth(180);
imageView.setMaxHeight(135);
imageView.setPadding(5, 5, 5, 5);//设置ImageView的内边距
}else{
imageView = (ImageView)convertView;
}
imageView.setImageResource(imageId[position]);//为ImageView设置要显示的图片
return imageView;//返回ImageView
}
/**
* 获得当前选项的id
*/
@Override
public long getItemId(int position) {
return position;
}
/**
* 获得当前选项
*/
@Override
public Object getItem(int position) {
return position;
}
/**
* 获得数量
*/
@Override
public int getCount() {
return imageId.length;
}
};
<span style="white-space:pre"> </span>gridView.setAdapter(adapter);//将适配器与GridView关联
<span style="white-space:pre"> </span>gridView.setOnItemClickListener(new OnItemClickListener() {
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void onItemClick(AdapterView<?> parent, View view, int position,
<span style="white-space:pre"> </span>long id) {
<span style="white-space:pre"> </span>Intent intent = new Intent(MainActivity.this, BigAcitivyt.class);
<span style="white-space:pre"> </span>Bundle bundle = new Bundle();//创建并实例化一个Bundle对象
<span style="white-space:pre"> </span>bundle.putInt("imageId", imageId[position]);//保存图片id1
<span style="white-space:pre"> </span>intent.putExtras(bundle);//将Bundle对象添加到intent对象中
<span style="white-space:pre"> </span>startActivity(intent);//启动新的Activity
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>});
4、big.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:paddingTop="6px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/head01" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回" />
</LinearLayout>
5、创建一个继承Activity类的BigActivity
public class BigActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.big);
Intent intent = getIntent();//获取Intent对象
Bundle bundle = intent.getExtras();//获取传递过来的数据包
int imageId = bundle.getInt("iamgeId");
ImageView iv = (ImageView)findViewById(R.id.imageView1);
iv.setImageResource(imageId);
Button button = (Button)findViewById(R.id.button1);//获取“返回”按钮
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();//返回
}
});
}
}
6、在AndroidManifest.xml中注册BigActivity
<activity
android:name=".BigActivity"
android:label="原图"></activity>