先在Transitions中插入ImageSwitcherandroid
package com.example.Galleryphotoshow; import com.example.Galleryphotoshow.R; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.ViewSwitcher.ViewFactory; public class MainActivity extends Activity implements OnItemSelectedListener,ViewFactory { private int[] resIds = new int[] { R.drawable.item1, R.drawable.item2, R.drawable.item3, R.drawable.item4, R.drawable.item5, R.drawable.item6, R.drawable.item7, R.drawable.item8, R.drawable.item9, R.drawable.item10, R.drawable.item11, R.drawable.item12, R.drawable.item13, R.drawable.item14, R.drawable.item15 }; private Gallery gallery; private ImageSwitcher is; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gallery = (Gallery) findViewById(R.id.gallery1); is = (ImageSwitcher) findViewById(R.id.imageSwitcher1); gallery.setAdapter(new ImageAdapter(this)); //讓選定的圖片在中心顯示 gallery.setSelection(resIds.length/2); //爲Gallery綁定監聽器; gallery.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { //當在Gallery中選定一張圖片是 ImageSwitcher同步顯示同一張 //position%images.length 爲了讓圖片循環顯示 is.setImageResource(resIds[position%resIds.length]); } public void onNothingSelected(AdapterView<?> parent) { } }); is.setFactory(new ImageFactory(this)); } private class ImageAdapter extends BaseAdapter{ private Context context; public ImageAdapter(Context context) { this.context = context; } //能夠return images.lenght(),在這裏返回Integer.MAX_VALUE //是爲了使圖片循環顯示 public int getCount() { return Integer.MAX_VALUE; } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } public View getView(int position, View convertView, ViewGroup parent) { ImageView iv = new ImageView(context); iv.setImageResource(resIds[position%resIds.length]); iv.setLayoutParams(new Gallery.LayoutParams(90,90)); iv.setAdjustViewBounds(true); return iv; } } private class ImageFactory implements ViewFactory{ private Context context; public ImageFactory(Context context){ this.context = context; } public View makeView() { ImageView iv = new ImageView(context); iv.setLayoutParams(new ImageSwitcher.LayoutParams(200,200)); return iv; } } @Override public View makeView() { // TODO Auto-generated method stub return null; } @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }