Android beginners: gallery + imageswitcher

Source: Internet
Author: User

This article from http://blog.csdn.net/hellogv/, reference must indicate the source!

The last time I talked about how to use the gallery control, this time I will talk about the combination of gallery and imageswitcher. This article implements a simple Image Browsing function. First paste the program running:

In addition to gallery, you can drag and switch images. I added the setontouchlistener event implementation to the imageswitcher control, so that imageswitcher can also switch images in the drag. In this example, we still use the Java reflection mechanism to automatically read images from resources.

 

The source code of Main. XML is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "match_parent" <br/> Android: layout_height = "match_parent"> </P> <p> <imageswitcher Android: Id = "@ + ID/switcher" <br/> Android: layout_width = "match_parent" Android: layout_height = "match_parent"/> </P> <p> <gallery Android: Id = "@ + ID/Gallery" <br/> Android: background = "#55000000" <br/> Android: layout_width = "match_parent" <br/> Android: layout_alignparentbottom = "true" <br/> Android: layout_alignparentleft = "true" </P> <p> Android: gravity = "center_vertical" <br/> Android: spacing = "16dp" Android: layout_height = "100dp"/> <br/> </relativelayout> </P> <p>

 

The source code of the program is as follows:

Package COM. testimageview; <br/> Import Java. lang. reflect. field; <br/> Import Java. util. arraylist; <br/> Import android. app. activity; <br/> Import android. content. context; <br/> Import android. OS. bundle; <br/> Import android. view. motionevent; <br/> Import android. view. view; <br/> Import android. view. view. ontouchlistener; <br/> Import android. view. viewgroup; <br/> Import android. view. animation. animationutils; <Br/> Import android. widget. adapterview; <br/> Import android. widget. baseadapter; <br/> Import android. widget. gallery; <br/> Import android. widget. imageswitcher; <br/> Import android. widget. imageview; <br/> Import android. widget. adapterview. onitemselectedlistener; <br/> Import android. widget. gallery. layoutparams; <br/> Import android. widget. viewswitcher. viewfactory; <br/> public class testimageview extend S activity implements viewfactory {<br/> private imageswitcher is; <br/> private gallery Gallery; <br/> private int downx, UPX; <br/> private arraylist <integer> imglist = new arraylist <integer> (); // image id </P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> // todo auto-generated method stub <br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> // use reflection Function to obtain the image ID in the resource. <br/> Field [] fields = R. drawable. class. getdeclaredfields (); <br/> for (field: fields) <br/>{< br/> If (! "Icon ". equals (field. getname () // The image except the icon <br/>{< br/> int Index = 0; <br/> try {<br/> Index = field. getint (R. drawable. class); <br/>} catch (illegalargumentexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>} catch (illegalaccessexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> // Save the image id <br/> imglist. add (INDEX); <br/>} <Br/>}</P> <p> // set the imageswitcher Control <br/> is = (imageswitcher) findviewbyid (R. id. switcher); <br/> is. setfactory (this); <br/> is. setinanimation (animationutils. loadanimation (this, <br/> android. r. anim. fade_in); <br/> is. setoutanimation (animationutils. loadanimation (this, <br/> android. r. anim. fade_out); <br/> is. setontouchlistener (New ontouchlistener () {<br/>/* <br/> * slide on the imageswitcher control to switch between images <br/> */< Br/> @ override <br/> Public Boolean ontouch (view V, motionevent event) {<br/> If (event. getaction () = motionevent. action_down) <br/>{< br/> downx = (INT) event. getx (); // obtain the coordinates of the press <br/> return true; <br/>}< br/> else if (event. getaction () = motionevent. action_up) <br/>{< br/> UPX = (INT) event. getx (); // obtain the coordinate when releasing <br/> int Index = 0; <br/> If (UPX-downx> 100) // drag from left to right, check the previous <br/>{< br/> // if it is the first, go to the end <br/> If (Gallery. getsel Ecteditemposition () = 0) <br/> Index = Gallery. getcount ()-1; <br/> else <br/> Index = Gallery. getselecteditemposition ()-1; <br/>}< br/> else if (downx-UPX> 100) // drag from the right to the left, the last one is <br/>{< br/> // if it is the last one, go to the first <br/> If (Gallery. getselecteditemposition () = (Gallery. getcount ()-1) <br/> Index = 0; <br/> else <br/> Index = Gallery. getselecteditemposition () + 1; <br/>}< br/> // Changes the gallery image selection. The setonitemselectedlistener of imageswitcher is automatically triggered. <Br/> Gallery. setselection (index, true); <br/> return true; <br/>}< br/> return false; <br/>}</P> <p >}); </P> <p> // set the gallery Control <br/> gallery = (Gallery) findviewbyid (R. id. gallery); <br/> Gallery. setadapter (New imageadapter (this); <br/> Gallery. setonitemselectedlistener (New onitemselectedlistener () {<br/> @ override <br/> Public void onitemselected (adapterview <?> Arg0, view arg1, <br/> int position, long arg3) {<br/> is. setimageresource (imglist. get (position); <br/>}< br/> @ override <br/> Public void onnothingselected (adapterview <?> Arg0) {<br/> // todo auto-generated method stub <br/>}</P> <p> }); <br/>}< br/> // set imgaeswitcher <br/> @ override <br/> Public View makeview () {<br/> imageview I = new imageview (this); <br/> I. setbackgroundcolor (0xff000000); <br/> I. setscaletype (imageview. scaletype. center); // center <br/> I. setlayoutparams (New imageswitcher. layoutparams (// adaptive image size <br/> layoutparams. fill_parent, layoutparams. fill_parent); <br/> return I; <br/>}< br/> public class imageadapter extends baseadapter {<br/> Public imageadapter (context c) {<br/> mcontext = C; <br/>}< br/> Public int getcount () {<br/> return imglist. size (); <br/>}< br/> Public object getitem (INT position) {<br/> return position; <br/>}< br/> Public long getitemid (INT position) {<br/> return position; <br/>}< br/> Public View getview (INT position, view convertview, viewgroup parent) {<br/> imageview I = new imageview (mcontext); <br/> I. setimageresource (imglist. get (position); <br/> I. setadjustviewbounds (true); <br/> I. setlayoutparams (new gallery. layoutparams (<br/> layoutparams. wrap_content, layoutparams. wrap_content); <br/> return I; <br/>}< br/> private context mcontext; <br/>}</P> <p >}< 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.