1. Introduction
The result of the combination of gallery and imageswitcher is imageswitcher. below is gallery. You can jump to the imageswitcher image by moving the image in the gallery or clicking the image!
2.
3. xml file
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageSwitcher android:id="@+id/imageSwitcher" android:layout_width="wrap_content" android:layout_height="wrap_content" 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="10dp" /></RelativeLayout>
4. Java code
Package Wei. ye. g1; 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 hualang extends activity {// Private Static int [] images = {R. drawable. baos, R. drawable. caoc, R. drawable. chenyj, R. drawable. chenyy, R. drawable. gouj, R. drawable. guany, R. drawable. hanx, R. drawable. LP, R. drawable. liub, R. drawable. qinq, R. drawable. tiemz, R. drawable. wus, R. drawable. xiangy, R. drawable. yuef, R. drawable. zhaoky, R. drawable. zhugl, R. drawable. Xis, R. drawable. yingz}; gallery Gallery; imageswitcher is; @ override protected void oncreate (bundle savedinstancestate) {// todo auto-generated method stub super. oncreate (savedinstancestate); setcontentview (R. layout. hualang); settitle ("this is the effect of gallery and imageswitcher"); gallery = (Gallery) findviewbyid (R. id. gallery); is = (imageswitcher) findviewbyid (R. id. imageswitcher); Gallery. setadapter (New imagea Dapter (this); // display the selected image gallery in the center. setselection (images. length/2); // bind the listener to the gallery. setonitemselectedlistener (New onitemselectedlistener () {public void onitemselected (adapterview <?> Parent, view, int position, long ID) {// when an image is selected in Gallery, imageswitcher synchronously displays the same image. // position % images. length is used to display images cyclically. setimageresource (images [position % images. length]);} public void onnothingselected (adapterview <?> Parent) {}}); is. setfactory (New imagefactory (this);} private class imageadapter extends baseadapter {private context; Public imageadapter (context) {This. context = context;} // return images. lenght (), returns integer here. max_value // to make the image display public int getcount () {return integer cyclically. 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 (images [position % images. length]); IV. setlayoutparams (new gallery. layoutparams (90, 90); IV. setadjustviewbounds (true); Return IV ;}} private class imagefactory implements viewfactory {private context; Public imagefactory (context) {This. context = context;} public view makeview () {imageview IV = new imageview (context); IV. setlayoutparams (New imageswitcher. layoutparams (200,200); Return IV ;}}}
PS: The image is read from the drawable file, not from the SD card.