Gallery and Imageswicther analysis in Android

Source: Internet
Author: User

1.Gallery

Gallery controls are typically used to display a list of images, so also known as gallery controls, gallery can only display one row horizontally, and horizontal sliding effects are supported. That is, when you click, select, or drag an image in gallery, the list in the gallery image moves left to right, depending on the situation, until the last image is displayed.

2.ImageSwicther

Imageswitcher controls can be used to toggle between different images, in which the process of switching can be animated, such as fading effects.   Imageswitcher requires an image factory (viewfactory) to create a ImageView object to display the image, so we need a class that implements the Android.widget.ViewSwitcher.ViewFactory interface. When using Imageswitcher, the basic steps: 1. Must implements Viewswitcher.viewfactory 2. Call Setfactory Method: Imageswitcher.setfactory (This) 3. Rewrite the function Makeview () to do the following: (This method is called when the activity is entered) 4. Write the following code where you need to display the image: In the Main.xml file:
1<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3android:layout_height= "Fill_parent"4Android:layout_width= "Fill_parent" >5<Imageswitcher6Android:id= "@+id/imageswitcher"7Android:layout_width= "Fill_parent"8android:layout_height= "Fill_parent" >9</ImageSwitcher>Ten<Gallery OneAndroid:id= "@+id/gallery" AAndroid:layout_width= "Fill_parent" -android:layout_height= "Wrap_content" -Android:layout_alignparentbottom= "true"/> the</RelativeLayout>

In the Java file:

1  Public classMainactivityextendsActivityImplementsviewfactory,onitemselectedlistener{2 3 PrivateGallery Gallery;4 PrivateImageswitcher is;5 Private intGalleryitembackgroundid;6 Private intimageid[]={R.DRAWABLE.GRIL1,R.DRAWABLE.GRIL2,R.DRAWABLE.GRIL3,R.DRAWABLE.GRIL4,R.DRAWABLE.GRIL5,7 R.drawable.gril6,r.drawable.gril7,r.drawable.gril8,r.drawable.gril9,r.drawable.gril10,8 R.drawable.gril11};9 @OverrideTen     protected voidonCreate (Bundle savedinstancestate) { One         Super. OnCreate (savedinstancestate); A requestwindowfeature (Window.feature_no_title); Remove title - Setcontentview (r.layout.main); -          theGallery =(Gallery) Findviewbyid (r.id.gallery); -Gallery.setadapter (NewMygalleryadapter (mainactivity. This)); -Gallery.setonitemselectedlistener ( This); Setting options for listening with Setonitemselectedlistener -          +is =(Imageswitcher) Findviewbyid (r.id.imageswitcher); -Is.setfactory ( This); +     AIs.setinanimation (Animationutils.loadanimation ( This, Android. r.anim.fade_in)); atIs.setoutanimation (Animationutils.loadanimation ( This, Android. r.anim.fade_out)); -          -     } -  - @Override -      Public BooleanOncreateoptionsmenu (Menu menu) { in         //inflate the menu; This adds items to the action bar if it is present. - getmenuinflater (). Inflate (R.menu.main, menu); to         return true; +     } -  the     classMygalleryadapterextendsbaseadapter{ *  $         Privatecontext context;Panax Notoginseng      -          PublicMygalleryadapter (Context context) { the              This. Context =context; +TypedArray typed =obtainstyledattributes (r.styleable.gallery); Get Property Collection AGalleryitembackgroundid = Typed.getresourceid (r.styleable.gallery_android_galleryitembackground, 0); the         } +  - @Override $          Public intGetCount () { $             //TODO Auto-generated method stubs -             returnInteger.max_value; In order to achieve the picture in the gallery loop effect, only set return_integer.max_value, in fact, can return imageid.length; -         } the  - @OverrideWuyi          PublicObject GetItem (intposition) { the             //TODO Auto-generated method stubs -             returnImageid[position]; Wu         } -  About @Override $          Public LongGetitemid (intposition) { -             //TODO Auto-generated method stubs -             returnposition; -         } A  + @Override the          PublicView GetView (intposition, View Convertview, ViewGroup parent) { -             //TODO Auto-generated method stubs $ImageView ImageView =NewImageView (context); theImageview.setimageresource (imageid[position%Imageid.length]); Set the picture to the view the Imageview.setscaletype (SCALETYPE.FIT_XY); theImageview.setlayoutparams (NewGallery.layoutparams (156, 100));  the Imageview.setbackgroundresource (Galleryitembackgroundid); Set ImageView background -             returnImageView; in         } the          the     } About  the @Override the      PublicView Makeview () { theImageView ImageView =NewImageView (mainactivity. This); +Imageview.setadjustviewbounds (true); Whether to maintain the aspect ratio. Need to be used with maxwidth, maxheight, otherwise no effect is used alone
 - Imageview.setscaletype (SCALETYPE.FIT_XY); theImageview.setlayoutparams (NewImageswitcher.layoutparams (layoutparams.fill_parent,layoutparams.fill_parent));Bayi          the         returnImageView; the     } -  - @Override the      Public voidOnitemselected (adapterview<?> Parent, view view,intposition,//Gallery (Gallery) Selected items the             LongID) { the         //TODO Auto-generated method stubs the          -Is.setimagedrawable (Getresources (). Getdrawable (imageid[position%imageid.length])); the     } the  the @Override94      Public voidOnnothingselected (adapterview<?>parent) { the         //TODO Auto-generated method stubs the          the     }98}

Operating effect:

Gallery and Imageswicther analysis in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.