Take XML

<ImageSwitcher android:id="@+id/imageSwitcher"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_centerInParent="true" />

<Gallery android:id="@+id/gallery" android:background="# 55000000"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" android:spacing="0dp" />Copy the code

java

Public class extends Activity {private static int[] images = {r.map.wallpaper_x, xx}; Gallery gallery; ImageSwitcher is; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState);setContentView(R.layout.xx); gallery = (Gallery) findViewById(R.id.gallery); is = (ImageSwitcher) findViewById(R.id.imageSwitcher); gallery.setAdapter(new ImageAdapter(this)); // Make the selected image display gallery. SetSelection (images.length / 2) in the center; // Bind listener for Gallery; gallery.setOnItemSelectedListener(newOnItemSelectedListener() { public void onItemSelected(AdapterView<? > parent, View view, int position, Long id) {// When an image is selected in the Gallery, ImageSwitcher synchronizes to display the same image // position%images.length to loop the image is.setImageResource(images[position % images.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; } / / canreturnImages.lenght (), which returns integer.max_value // to make the image loop public intgetCount() {
        return images.length;
    }

    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.setScaleType(ImageView.ScaleType.CENTER_CROP);
        iv.setImageResource(images[position % images.length]);
        iv.setLayoutParams(new Gallery.LayoutParams(250, 350));
        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(-1, -1));
        returniv; }}Copy the code

}