声明:本人博客纯属个人学习过程中的一些仿写的简单练习记录,其他论坛也有类似内容!(可能不免有错误之处,还望见谅,指出)
 这是一个最简单可以滑动查看图片的应用程序:
 首先创建项目名为: Gallery My photo
代码如下:
 package com.slide.demo;
  import android.app.Activity;
 import android.content.Context;
 import android.os.Bundle;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.BaseAdapter;
 import android.widget.Gallery;
 import android.widget.ImageView;
  public class Slide extends Activity {
 private Gallery mgallery; //设置Gallery对象名
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 mgallery = (Gallery) findViewById(R.id.Picture); //联系到视图布局
 ImageAdapter madapter = new ImageAdapter(this); //构建一个适配器
 mgallery.setAdapter(madapter);
 mgallery.setSpacing(6); //设置图片之间的距离
 }
  private class ImageAdapter extends BaseAdapter {
 int[] images = { R.drawable.image0, R.drawable.image1,
 R.drawable.image3, R.drawable.image4, R.drawable.image5,
 R.drawable.image2, }; //联系到drawable文件夹中要显示的图片(这里有6张)
 Context mContext;
 //构造函数
 public ImageAdapter(Context ctx) {
 mContext = ctx;
 }
  public int getCount() {
 return images.length;
 } //得到图片的数量
  public Object getItem(int position) {
 return images[position];
 } //继承父类中的方法,获取图片数据中指定位置的设置
  public long getItemId(int position) {
 return 0;
 } //继承父类中的方法,获取id和图片集里面的位置
  @Override
 public View getView(int position, View convertView, ViewGroup parent) {
 ImageView iv = null;
 if (convertView == null) {
 iv = new ImageView(mContext);
 iv.setImageResource(images[position]);
 } else {
 iv = (ImageView) convertView;
 }
 return iv; //判断图片显示位置是否为空,若为空即设置下一张图片填充
 }
 }
 }
 视图布局文件 main xml :
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent" android:gravity="center"
 android:longClickable="true">
 >
 <Gallery android:id="@+id/Picture" android:layout_width="fill_parent"
 android:layout_height="wrap_content"/>
 </LinearLayout>
  应用配置文件 AndroidMainfest xml :
 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.slide.demo"
 android:versionCode="1"
 android:versionName="1.0">
 <application android:icon="@drawable/icon" android:label="@string/app_name">
 <activity android:name=".Slide"
 android:label="@string/app_name">
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 </application>
 </manifest>