今天做了一下Gallery与ImageSwitcher的综合运用,下面是运行的效果,可以点击或者手动拖动图片: 下面附上源码: package com.android.cf; import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.Window; import android.view.View.OnTouchListener; import android.view.ViewGroup.LayoutParams; import android.view.animation.AnimationUtils; import android.widget.AdapterView; import android.widget.Gallery; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ViewSwitcher.ViewFactory; public class MyMain extends Activity implements ViewFactory, OnItemSelectedListener { private ImageSwitcher mSwitcher; private Gallery mGallery; private int selectedTag = 0; private int upX, downX; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); setTitle("图片显示"); mSwitcher = (ImageSwitcher) findViewById(R.id.switcher); mSwitcher.setFactory(MyMain.this); mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); mSwitcher.setOnTouchListener(touchlistener); mGallery = (Gallery) findViewById(R.id.gallery); mGallery.setAdapter(new ImageAdapter(MyMain.this)); mGallery.setOnItemSelectedListener(this); } /** * (non-Javadoc) * * @see android.widget.ViewSwitcher.ViewFactory#makeView() */ @Override public View makeView() { ImageView i = new ImageView(this); i.setBackgroundColor(0xFF000000); i.setScaleType(ImageView.ScaleType.FIT_CENTER); i.setLayoutParams(new ImageSwitcher.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); return i; } OnTouchListener touchlistener = new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { downX = (int) event.getX(); // 取得按下时 的坐标x return true; } else if (event.getAction() == MotionEvent.ACTION_UP) { upX = (int) event.getX(); // 取得松开时的坐标x if (upX - downX > 100) { // 从左拖到右,即看前一张 // 如果是第一,则去到尾部 if (mGallery.getSelectedItemPosition() == 0) selectedTag = mGallery.getCount() - 1; else selectedTag = mGallery.getSelectedItemPosition() - 1; } else if (downX - upX > 100)// 从右拖到左,即看后一张 { // 如果是最后,则去到第一 if (mGallery.getSelectedItemPosition() == (mGallery .getCount() - 1)) selectedTag = 0; else selectedTag = mGallery.getSelectedItemPosition() + 1; } // 改变gallery图片所选,自动触发ImageSwitcher的setOnItemSelectedListener mGallery.setSelection(selectedTag, true); return true; } return false; } }; /** * (non-Javadoc) * * @see android.widget.AdapterView.OnItemSelectedListener#onItemSelected(android.widget.AdapterView, * android.view.View, int, long) */ @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { mSwitcher.setImageResource(ImageAdapter.imageIDs[arg2]); selectedTag = arg2; } /** * (non-Javadoc) * * @see android.widget.AdapterView.OnItemSelectedListener#onNothingSelected(android.widget.AdapterView) */ @Override public void onNothingSelected(AdapterView<?> arg0) { } } 这个是布局文件: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageSwitcher android:layout_width="fill_parent" android:id="@+id/switcher" android:layout_height="fill_parent" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" /> <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="60dip" android:background="#55000000" android:layout_alignParentLeft="true" android:layout_alignParentBottom="true" android:gravity="center_vertical" android:spacing="16dip" /> </RelativeLayout> 这个是ImageAdapter的类,供Gallery调用: package com.android.cf; import java.lang.reflect.Field; import java.util.ArrayList; 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{ /** * @author cf * @category 下面注销掉的一部分,是是一种反射机制,能够读取系统的图片ID * 有值得的借鉴的地方。 */ // private ArrayList<Integer> imgList=new ArrayList<Integer>();//图像ID // //用反射机制来获取资源中的图片ID // Field[] fields = R.drawable.class.getDeclaredFields(); // for (Field field : fields) // { // if (!"icon".equals(field.getName()))//除了icon之外的图片 // { // int index = 0; // try { // index = field.getInt(R.drawable.class); // } catch (IllegalArgumentException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (IllegalAccessException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // //保存图片ID // imgList.add(index); // } // } private Context context; public static Integer[] imageIDs={ R.drawable.black_horse,R.drawable.blueflower,R.drawable.head_paint1, R.drawable.liruotong,R.drawable.melada_kerr,R.drawable.p1,R.drawable.panda, R.drawable.sanxia }; public ImageAdapter(Context context){ this.context=context; } /** * (non-Javadoc) * @see android.widget.Adapter#getCount() */ @Override public int getCount() { return imageIDs.length; } /** * (non-Javadoc) * @see android.widget.Adapter#getItem(int) */ @Override public Object getItem(int position) { return position; } /** * (non-Javadoc) * @see android.widget.Adapter#getItemId(int) */ @Override public long getItemId(int position) { return position; } /** * (non-Javadoc) * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup) */ @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView image=new ImageView(context); image.setImageResource(imageIDs[position]); image.setAdjustViewBounds(true); image.setLayoutParams(new Gallery.LayoutParams(120,120)); image.setScaleType(ImageView.ScaleType.FIT_CENTER); return image; } }