The gallery component, known as a gallery, is a list of landscape views, and when using Android API Platform, Gallery is painted with a dash to indicate that Google has deprecated the component.
* @deprecated This widget is no longer supported. Other horizontally scrolling
* Widgets include {@link Horizontalscrollview} and {@link Android.support.v4.view.ViewPager}
* from the Support library.
Instead, Horizontalscrollview, which is the horizontal ScrollView and Viewpager, is known in front of both components.
Gallery and the ListView are all the same. You need to load the resource into the adapter and then set the adapter for the control through Setadapter.
1, customizing an adapter to inherit from Baseadapter
PackageCom.example.lining.gallery;ImportAndroid.content.Context;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.Gallery;ImportAndroid.widget.ImageView;Importjava.util.List;/*** Created by lining on 2015/11/27.*/ Public classImageadapterextendsBaseadapter {Private int[]res; Privatecontext Context; PublicImageadapter (int[]res,context Context] { This. res =Res; This. Context =context; } @Override Public intGetCount () {returnres.length; } @Override PublicObject GetItem (intposition) { returnRes[position]; } @Override Public LongGetitemid (intposition) { returnposition; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {ImageView ImageView=NewImageView (context); Imageview.setimageresource (Res[position]); Imageview.setlayoutparams (NewGallery.layoutparams (300, 200) ;//Set ImageView width and height imageview.setscaletype (ImageView.ScaleType.FIT_XY);//use transverse and longitudinal stretchingreturnImageView; }}
2. Set the adapter and listen for the event onitemselectlistening
Public classMainactivityextendsActivityImplementsAdapterview.onitemselectedlistener {Private int[]res={R.DRAWABLE.A1,R.DRAWABLE.A2,R.DRAWABLE.A3,R.DRAWABLE.A5,R.DRAWABLE.A6}; PrivateGallery Gallery; PrivateImageadapter Imageadapter;@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Gallery=(Gallery) Findviewbyid (r.id.gallery); Imageadapter=NewImageadapter (Res, This); Gallery.setadapter (Imageadapter); Gallery.setonitemselectedlistener ( This); } @Override Public voidOnitemselected (adapterview<?> Parent, view view,intPositionLongID) {} @Override Public voidOnnothingselected (adapterview<?>parent) { }
}
At this point we can print out the image information of our choice in the onitemselected response function, onnothingselected is triggered as long as the other options are not selected
Now let's show the selected picture with Imageswitch.
Imageswitch and ImageView feature a bit similar, they all can be used to display pictures, the difference is imageswitch effect is more dazzling, he can
Specify the effect when the picture is toggled
Imageswitch rough Understanding is the Imamgeview selector, he needs to set Viewfactory method, we let Viewfactory's Makeview method return
ImageView
3, add the Imageswitch tag to the XML and set the Fade effect
Imageswitcher = (imageswitcher) Findviewbyid (r.id.iamgeswitch); Imageswitcher.setfactory (this); Imageswitcher.setinanimation (animationutils.loadanimation (this, Android. r.anim.fade_in)); Imageswitcher.setoutanimation (animationutils.loadanimation (this, Android. R.anim.fade_out));
Also need to implement Viewfactory's viewswitcher.viewfactory monitoring event
@Override Public View Makeview () { new ImageView (this); Imageview.setscaletype (ImageView.ScaleType.CENTER); // Scaling of equal proportions return imageView; }
4, then everything is ready, just need to set Imageswitch resources in the gallery selection event
Imageswitcher.setbackgroundresource (Res[position]);
Android Learning Gallery and Imageswitch use