Gallery has a common parent class with previous spinner: Absspinner, which indicates that both gallery and spinner are a list box. The difference between them is that spinner displays a vertical list box, and gallery displays a horizontal list box. There is a difference between gallery and spinner: Spinner's role is for the user to choose, and Gallery allows the user to drag to view the previous and next list items.
XML attributes and related methods commonly used in gallery
| XML properties |
Related methods |
Description |
| Android:animationduration |
setanimationduration (int) |
Set the animation duration when a list item is toggled |
| Android:gravity |
setgravity (int) |
Set alignment |
| Android:spacing |
setspacing (int) |
Set the spacing between list items within a gallery |
| Android:unselectedalpha |
Setunselectedalpha (int) |
Set transparency for no selected list items |
The usage of gallery itself is very simple----basically similar to spinner's usage, as long as he is provided with a content adapter, the adapter GetView method returns the view as a list item of the gallery list If the program needs to monitor changes to the gallery selection, it can be done by adding a onitemselectedlistener listener to the gallery.
This is a case study of the "slideshow" image browser, which is similar to the one we wrote earlier about the Android self-study grid attempt (GridView) and the image switcher (imageswitcher) function and usage, both of which have the effect of previewing pictures. But this case uses gallery as the image preview, so the interface will be more friendly. Gallery generates a horizontal list, each list item is a picture preview, and gallery generates a horizontal list that lets the user drag to toggle the list item.
Let's look at the code for the Layout section of the case first: Layout/main.xml
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 android:orientation= "vertical"4 Android:layout_width= "Fill_parent"5 Android:layout_height= "Fill_parent">6 <!--defining a Imageswitcher component -7 <ImageswitcherAndroid:id= "@+id/switcher"8 Android:layout_width= "320DP"9 Android:layout_height= "320DP"Ten /> One <!--Defining a gallery component - A <GalleryAndroid:id= "@+id/gallery" - Android:layout_width= "Fill_parent" - Android:layout_height= "Wrap_content" the Android:layout_margintop= "25DP" - Android:unselectedalpha= "0.6" - android:spacing= "3pt" - /> + </LinearLayout>
The layout above is simple, with only two components defined: Imageswitcher and gallery components. The main program is also simple, passing in a Viewfactory object for Imageswitcher, and passing in a adapter object for gallery. So Imageswitcher and gallery can work.
In order for Imageswitcher to display the selected picture in Gallery, a gallery is bound to a Onitemselectedlistener listener.
Main program: Gallerytest.java
1 Packagecom.yangjing.gallarytest;2 3 Importandroid.app.Activity;4 ImportAndroid.content.res.TypedArray;5 ImportAndroid.os.Bundle;6 ImportAndroid.view.View;7 ImportAndroid.view.ViewGroup;8 ImportAndroid.view.ViewGroup.LayoutParams;9 Importandroid.view.animation.AnimationUtils;Ten ImportAndroid.widget.AdapterView; One ImportAndroid.widget.AdapterView.OnItemSelectedListener; A ImportAndroid.widget.BaseAdapter; - ImportAndroid.widget.Gallery; - ImportAndroid.widget.ImageSwitcher; the ImportAndroid.widget.ImageView; - Importandroid.widget.ViewSwitcher.ViewFactory; - - Public classGallarytestextendsactivity{ + - int[] Imageids =New int[] + { A R.drawable.shuangzi, R.drawable.shuangyu, at R.DRAWABLE.CHUNV, R.drawable.tiancheng, R.drawable.tianxie, - R.drawable.sheshou, R.drawable.juxie, r.drawable.shuiping, - R.drawable.shizi, R.drawable.baiyang, R.drawable.jinniu, - R.drawable.mojie}; - - @Override in Public voidonCreate (Bundle savedinstancestate) - { to Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.main); - FinalGallery Gallery =(Gallery) Findviewbyid (r.id.gallery); the //gets the Imageswitcher object that displays the picture * FinalImageswitcher switcher =(Imageswitcher) $ Findviewbyid (r.id.switcher);Panax Notoginseng //set Viewfactory objects for Imageswitcher objects -Switcher.setfactory (Newviewfactory () the { + @Override A PublicView Makeview () the { +ImageView ImageView =NewImageView (gallarytest. This); -Imageview.setbackgroundcolor (0xff0000); $ Imageview.setscaletype (ImageView.ScaleType.FIT_CENTER); $Imageview.setlayoutparams (NewImageswitcher.layoutparams ( - layoutparams.wrap_content, layoutparams.wrap_content)); - returnImageView; the } - });Wuyi //Animate a picture change theSwitcher.setinanimation (Animationutils.loadanimation ( This, - Android. r.anim.fade_in)); WuSwitcher.setoutanimation (Animationutils.loadanimation ( This, - Android. r.anim.fade_out)); About //creates a Baseadapter object that is responsible for providing the picture that the gallery displays $ Baseadapter adapter =NewBaseadapter () - { - @Override - Public intGetCount () A { + returnimageids.length; the } - @Override $ PublicObject GetItem (intposition) the { the returnposition; the } the @Override - Public LongGetitemid (intposition) in { the returnposition; the } About the //The returned view of the method represents each list item the @Override the PublicView GetView (intposition, View Convertview, viewgroup parent) + { - //Create a ImageView theImageView ImageView =NewImageView (gallarytest. This);BayiImageview.setimageresource (imageids[position%imageids.length]); the //set the scale type of ImageView the Imageview.setscaletype (ImageView.ScaleType.FIT_XY); -Imageview.setlayoutparams (NewGallery.layoutparams (75, 100)); -TypedArray TypedArray =obtainstyledattributes (r.styleable.gallery); theImageview.setbackgroundresource (Typedarray.getresourceid (r.styleable.gallery_android_galleryitembackground, 0)); the returnImageView; the } the }; - Gallery.setadapter (adapter); theGallery.setonitemselectedlistener (NewOnitemselectedlistener () the { the //This method is triggered when a gallery selected item has changed94 @Override the Public voidOnitemselected (adapterview<?>Parent, view view, the intPositionLongID) the {98Switcher.setimageresource (imageids[position%imageids.length]); About } - 101 @Override102 Public voidOnnothingselected (adapterview<?>parent)103 {104 } the });106 }107}
The effect of the program after the operation shows: