Android basics: imageswitcher and gallery

Source: Internet
Author: User

Android basics: imageswitcher and gallery

16:16:15

When creating an image browser, imageswitcher and gallery are used in a simple way. Now let's look at their basic usage.

 

 

 

1. Configure the layout file main. xml

 

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <imageswitcher <br/> Android: Id = "@ + ID/imageswitcher" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_centerinparent = "true"/> <br/> <gallery <br/> Android: Id = "@ + ID/Gallery" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: spacing = "8dp" <br/> Android: layout_alignparentbottom = "true"/> <br/> </relativelayout> <br/>

2. Main program code mainactivity. Java

 

 

Package COM. yin. picture_view; </P> <p> Import android. app. activity; <br/> Import android. content. context; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. viewgroup; <br/> Import android. widget. adapterview; <br/> Import android. widget. adapterview. onitemselectedlistener; <br/> Import android. widget. baseadapter; <br/> Import android. widget. gallery; <br/> Import android. wi DGET. imageswitcher; <br/> Import android. widget. imageview; <br/> Import android. widget. viewswitcher. viewfactory; <br/> public class mainactivity extends activity {</P> <p> // resources for displaying images <br/> Private Static int [] images = {<br/> r. drawable. s0, R. drawable. s1, <br/> r. drawable. s2, R. drawable. s3, <br/> r. drawable. s4, R. drawable. s5, <br/> r. drawable. s6, R. drawable. s7, <br/> r. drawable. s8, R. drawable. s9 <br/> }; <Br/> gallery Gallery; <br/> imageswitcher is; <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> gallery = (Gallery) findviewbyid (R. id. gallery); <br/> is = (imageswitcher) findviewbyid (R. id. imageswitcher); </P> <p> Gallery. setadapter (New imageadapter (this); <br/> // display the selected image in the center <br/> Gallery. setselection (IMAG Es. length/2); </P> <p> // bind the listener to gallery. <br/> Gallery. setonitemselectedlistener (New onitemselectedlistener () {<br/> Public void onitemselected (adapterview <?> Parent, view, <br/> int position, long ID) {<br/> // when an image is selected in Gallery, imageswitcher synchronously displays the same image. <br/> // position % images. length is used to display images cyclically <br/> is. setimageresource (images [position % images. length]); </P> <p >}< br/> Public void onnothingselected (adapterview <?> Parent) {</P> <p >}< br/>}); </P> <p> is. setfactory (New imagefactory (this); </P> <p >}</P> <p> private class imageadapter extends baseadapter {<br/> private context; <br/> Public imageadapter (context) {<br/> This. context = context; <br/>}< br/> // return images. lenght (), returns integer here. max_value <br/> // is used to display images cyclically <br/> Public int getcount () {<br/> return integer. max_value; <br/>}< br/> Public object getitem (INT position) {<br/> return NULL; <br/>}< br/> Public long getitemid (INT position) {<br/> return 0; <br/>}< br/> Public View getview (INT position, view convertview, viewgroup parent) {<br/> imageview IV = new imageview (context); <br/> IV. setimageresource (images [position % images. length]); <br/> IV. setlayoutparams (new gallery. layoutparams (90,90); <br/> IV. setadjustviewbounds (true); <br/> return IV; <br/>}</P> <p> private class imagefactory implements viewfactory {<br/> private context; <br/> Public imagefactory (context) {<br/> This. context = context; <br/>}< br/> Public View makeview () {<br/> imageview IV = new imageview (context); <br/> IV. setlayoutparams (New imageswitcher. layoutparams (200,200); <br/> return IV; <br/>}< br/>}

Related Article

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.