Gallery是Android中的图片库控件。可以浏览自己的图片相册。
效果图
本程序main.xml源码
本程序ImageAdapter类源码
package com.sx.Gallery;
<?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"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="fill_parent" android:spacing="2px" /> <!-- android:spacing="2px"用来设置图片之间的间距 --> </LinearLayout>
JAVA源码
import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; public class ImageAdapter extends BaseAdapter { private Context context; private int[] MyImageIDs = { R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7, R.drawable.img8, R.drawable.img9, R.drawable.img10, R.drawable.img11, R.drawable.img12, R.drawable.img13, R.drawable.img14, R.drawable.img15, R.drawable.img16, R.drawable.img17, R.drawable.img18, R.drawable.img19, R.drawable.img20 }; public ImageAdapter(Context context) { this.context = context; } // 获取图片个数 public int getCount() { return MyImageIDs.length; //return Integer.MAX_VALUE; } // 获取图片在库中的位置 public Object getItem(int position) { return position; } // 获取图片在库中的ID public long getItemId(int id) { return id; } public View getView(int position, View convertView, ViewGroup parent) { ImageView imageview = new ImageView(this.context); // 给ImageView设置资源 imageview.setImageResource(this.MyImageIDs[position]); // 设置布局 图片120*120 imageview.setLayoutParams(new Gallery.LayoutParams(120, 120)); // 设置显示比例类型 imageview.setScaleType(ImageView.ScaleType.FIT_XY); //imageview.setScaleType(ImageView.ScaleType.FIT_CENTER); return imageview; } } //要想实现图片无限循环可拖动修改getCount() 函数改为返回值为return Integer.MAX_VALUE; //imageview.setImageResource(this.MyImageIDs[position] % MyImageIDs.length.); 即可 Activity源码 package com.sx.Gallery; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Gallery; import android.widget.Toast; public class GalleryActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); CreateGallery(); } private void CreateGallery() { //获得Gallery对象 Gallery gellery=(Gallery)this.findViewById(R.id.gallery); //添加ImageAdapter给Gallery对象 注意哦Gallery类并没有setAdapter这个方法 这个方法是从AbsSpinner类继承的 gellery.setAdapter(new ImageAdapter(this)); //设置Gallery的背景图片 //gellery.setBackgroundResource(R.drawable.bg0); //设置Gallery的事件监听 gellery.setOnItemClickListener(new GalleryItemListener()); } class GalleryItemListener implements OnItemClickListener { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(GalleryActivity.this, "你选择了" + (position + 1) + " 号图片", Toast.LENGTH_SHORT).show(); } } }