在android4.1之前,这个使用的比较多,但在4.1之后,谷歌推荐我们使用ViewPager,使用Gallery可以实现实现图片滚动,配合ImageSwitcher可以实现图片查看器效果,下面是一个demo:
GalleryActivity.java
public class GalleryActivity extends Activity implements AdapterView.OnItemSelectedListener ,ViewSwitcher.ViewFactory {
private final String TAG = "GalleryActivity";
private Gallery mGallery;
private ImageSwitcher imageSwitcher;
private int[] ids;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_view_test);
ids = new int[]{R.mipmap.little_boy_black,R.mipmap.little_boy_blue,R.mipmap.little_boy_green,R.mipmap.little_boy_grey,
R.mipmap.little_boy_orange,
R.mipmap.little_boy_pink,R.mipmap.little_boy_purple,R.mipmap.little_boy_red,
R.mipmap.little_boy_white,R.mipmap.little_boy_yellow};
mGallery = (Gallery)findViewById(R.id.gallery);
mGallery.setAdapter(new ImageAdapter(this,ids));
mGallery.setOnItemSelectedListener(this);
//要实现ViewSwitcher.ViewFactory,重写makeView方法产生ImageView
imageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher);
imageSwitcher.setFactory(this);
//设置动画
imageSwitcher.setInAnimation(this,android.R.anim.fade_in);
imageSwitcher.setOutAnimation(this,android.R.anim.fade_out);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.i(TAG,"position:" + position + ",currentPos:" + position%ids.length);
imageSwitcher.setBackgroundResource(ids[position%ids.length]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
@Override
public View makeView() {
ImageView imageView = new ImageView(this);
//缩放模式:按比例缩放,且居中
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
return imageView;
}
}
要实现OnItemSelectedListener 接口去监听当前选中那张图片,在onItemSelected方法里面会获取但当前图片在Gallery的位置,因此可以在这里做文章,下面是Gallery的Adapter,很简单,具体代码如下,而如果我们需要ImageSwitcher来显示图片的话,需要实现ViewFactory接口,重写makeView,在里面创建ImageSwitcher需要的ImageViwe。
ImageAdapter.java
public class ImageAdapter extends BaseAdapter{
private Context mContext;
private int[] mIds;
public ImageAdapter(Context context, int[] ids) {
this.mContext = context;
this.mIds = ids;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;//为了实现无线滚动
}
@Override
public Object getItem(int position) {
return mIds[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setBackgroundResource(mIds[position%mIds.length]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY); //设置图片x,y自动拉伸
imageView.setLayoutParams(new Gallery.LayoutParams(200,150)); //设置在gallery大小
return imageView;
}
}
布局文件:gallery_view_test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent">
<Gallery
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/gallery" />
<ImageSwitcher
android:id="@+id/imageSwitcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>