The android gallery component allows you to display images cyclically.

Source: Internet
Author: User

The gallery component is mainly used to display the image list horizontally. The gallery component can only display the specified image in a limited manner. That is to say, if 10 images are specified for the gallery component, when the gallery component shows up to 10th images, it will not be displayed any more. This does not matter in most cases, but in some cases, we want the image to be displayed at the end of the screen, and then start to display the remaining 1st images, that is, loop display. To implement this style of gallery components, you need to make some improvements to the gallery adapter object.

Traditional gallery component usage

Before implementing the gallery component that can display images cyclically, let's review the traditional usage of the gallery component. The gallery component displays an image list horizontally. When you click the next image of the current image, the image list moves one cell to the left. When you click the previous image of the current image, the image list will move to the right. You can also move the image list to the left and right by dragging it. Currently, 1st images are displayed, as shown in effect 1. The gallery component shows the effect of the last image 2.

 

Figure 1

 

Figure 2

 

 

 

As shown in figure 2, when the last image is displayed, there is no image after the list, which is also the basic Display Effect of the gallery component. Later in this article, we will detail how to make the gallery component display the last image from the first image.

Now let's take a look at how the effects of steps 1 and 2 are made. Since gallery is used to display images, there must be some image files for display in step 1. Now you can prepare some images at will. In this example, I wrote 6 JPG files (item1.jpgto item15.jpg ). Put these files in the Res/drawable directory.

The following code stores the resource IDs of these images in the int array:

Private int [] myimageids = {R. drawable. photo1, <br/> r. drawable. photo2, <br/> r. drawable. photo3, <br/> r. drawable. photo4, <br/> r. drawable. photo5, <br/> r. drawable. photo6 ,};

 

Configure a gallery component in the main. xml file in this example. The Code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <gallery Android: Id = "@ + ID/Gallery" Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" Android: layout_margintop = "30dp"/> <br/> </linearlayout>

 

 

The code for loading this component in the oncreate method is as follows:

Public void oncreate (bundle savedinstancestate) <br/>{< br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> // load the gallery component <br/> gallery = (Gallery) findviewbyid (R. id. gallery); <br/> // create an imageadapter object used to describe image data <br/> imageadapter = new imageadapter (this ); <br/> // set the adapter object of the gallery component <br/> Gallery. setadapter (imageadapter); <br/>}< br/>

 

 

The above Code involves a very important class: imageadapter. This class is a subclass of Android. widget. baseadapter used to describe image information. Next, let's take a look at the complete code of this class.

Public class imageadapter extends baseadapter <br/>{< br/> int mgalleryitembackground; <br/> private context mcontext; <br/> Public imageadapter (context) <br/>{< br/> mcontext = context; <br/> // obtain the attributes of the gallery component <br/> typedarray = obtainstyledattributes (R. styleable. gallery); <br/> mgalleryitembackground = typedarray. getresourceid (<br/> r. styleable. gallery_android_galleryitembackground, 0); <br/>}< br/> // total number of returned images <br/> Public int getcount () <br/>{< br/> return resids. length; <br/>}< br/> Public object getitem (INT position) <br/>{< br/> return position; <br/>}< br/> Public long getitemid (INT position) <br/>{< br/> return position; <br/>}< br/> // return the imageview object in the specified position <br/> Public View getview (INT position, view convertview, viewgroup parent) <br/>{< br/> imageview = new imageview (mcontext); <br/> // set the current image (position is the position of the current image list) <br/> imageview. setimageresource (myimageids [position]); <br/> imageview. setscaletype (imageview. scaletype. fit_xy); <br/> imageview. setlayoutparams (new gallery. layoutparams (163,106); <br/> // set the background style of the gallery component <br/> imageview. setbackgroundresource (mgalleryitembackground); <br/> return imageview; <br/>}< br/>

 

Note the following two points when compiling the imageadapter class:

1. The property information of the gallery component is obtained in the construction method of the imageadapter class. The information is defined in the Res/values/attrs. xml file. The Code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <resources> <br/> <declare-styleable name = "Gallery"> <br/> <ATTR name = "Android: galleryitembackground "/> <br/> </declare-styleable> <br/> </resources> <br/>

 

 

The property information above is used to set the background style of gallery.

2. There are two very important methods in the imageadapter class: getcount and getview. The getcount method is used to return the total number of images. Note that the total number cannot be greater than the actual number of images (which can be smaller than the actual number of images). Otherwise, an out-of-bounds exception is thrown. To display an image, the gallery component calls the getview method and passes the current image index (position parameter) to this method. Generally, the getview method is used to return the component (imageview object) of each displayed image ). From this point, we can see that the gallery component displays images in real time, rather than displaying all the images. In addition to creating an imageview object, the getview method also obtains the corresponding image resource ID from the resids array to set the image displayed in the imageview. The background display style of the gallery component is also set.

OK. Now run this program and drag the image list back and forth to see the effects shown in Figure 1 and figure 2.

How images are displayed cyclically

The circular display is similar to the circular linked list. The next node of the last node is 1st. This can also be simulated when images are displayed cyclically.

You may find something from the imageadapter class implemented in the previous section. Yes! Is the relationship between the position parameter and the getcount method in the getview method. The value of the position parameter cannot exceed the value returned by the getcount method. That is to say, the value range of the position parameter is 0 to getcount ()-1.

 

If the gallery component displays the last image, the position parameter value is exactly getcount ()-1. So how can we make gallery show the next image? That is to say, let the position parameter value increase by 1, right! Increase the return value of the getcount () method by 1.

There is another problem here. If the position parameter value is infinitely increased, it means that the myimageids array is constantly increasing, which will greatly consume system resources. To think of this, we need to solve two problems: the position must be constantly increased, and the ID of the image resources stored in the resids array must be limited. How can this problem be solved? The getcount () method is a good solution, so that the getcount method can return a large number, for example, integer. max_value. The position parameter value increases as the image of the gallery component moves forward. Currently, the myimageids array has only six elements. If the value of position exceeds the array boundary, you must continue to obtain the elements in the array (that is, when the value of position is 6, take the 0th elements in the myimageids array, and take 1st elements at the time of 6). The simplest method is to get the remainder. The Code is as follows:

 

 

Myimageids [position % myimageids. Length]

 

In this section, the imageadapter class is improved as follows:

1. Make the getcount method return a large value. We recommend that you return integer. max_value.

2. In the getview method, the image resource ID in the resids array is obtained cyclically by taking the remainder.

Through the above two improvements, the image list will be displayed cyclically when moving to the right. Of course, this method is essentially a pseudo loop. That is to say, if you really move the image to the value returned by the getcount method, It will be displayed to the last image. However, here the getcount method returns integer. max_value, which exceeds 2 billion. Unless someone really wants to move the image to a position of 20th billion, the gallery component is a component that displays the image cyclically.

Implement the gallery component to display images cyclically

In this section, the complete code of the imageadapter class related to the circular display image is set up. You can see the two improvements described in the previous section. To make the interface look more plump, this example shows an enlarged image (using the imageswitcher component) at the bottom when you click an image in a gallery component ). In this example, 3 is displayed. When moving the image backward, the image can be displayed continuously. You can run this example to try it out.

In this example, the complete code of the main class is as follows:

Package IRDC. ex04_10; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle;/* class used in this example */<br/> Import android. content. context; <br/> Import android. content. res. typedarray; <br/> Import android. view. view; <br/> Import android. view. viewgroup; <br/> Import android. widget. adapterview; <br/> Import android. widget. baseadapter; <br/> Import android. widget. gallery; <br/> Import android. widget. imageview; <br/> Import android. widget. toast; <br/> Import android. widget. adapterview. onitemclicklistener; </P> <p> public class ex04_10 extends activity <br/> {<br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) <br/>{< br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main);/* Get Through findviewbyid */<br/> gallery G = (Gallery) findviewbyid (R. id. mygallery);/* add an imageadapter and set it to the gallery object */<br/> G. setadapter (New imageadapter (this);/* set an itemclicklistener and toast the position of the image to be clicked */<br/> settitle ("Gallery implements cyclic Image Browsing "); <br/> G. setonitemclicklistener (New onitemclicklistener () <br/>{< br/> Public void onitemclick (adapterview parent, view V, int position, long ID) <br/>{< br/> toast. maketext (ex04_10.this, getstring (R. string. my_gallery_text_pre) + Position + getstring (R. string. my_gallery_text_post), toast. length_short ). show (); <br/>}< br/> }); <br/>}</P> <p> public class imageadapter extends baseadapter/* rewrite baseadapter to customize an imageadapter class */<br/> {<br/> int mgalleryitembackground; <br/> private context mcontext;/* imageadapter construction sub-*/<br/> private int [] myimageids = {R. drawable. photo1, <br/> r. drawable. photo2, <br/> r. drawable. photo3, <br/> r. drawable. photo4, <br/> r. drawable. photo5, <br/> r. drawable. photo6 ,}; </P> <p> Public imageadapter (context c) <br/>{< br/> mcontext = C; <br/> typedarray A = obtainstyledattributes (R. styleable. gallery);/* used in res/values/attrs. the gallery attribute defined in XML. */<br/> mgalleryitembackground =. getresourceid (R. styleable. gallery_android_galleryitembackground, 0); // * obtain the index of the gallery attribute <br/>. recycle ();/* enables repeated use of the object's styleable attribute */<br/>}</P> <p> Public int getcount () /* The method getcount to be rewritten. The total number of returned images */<br/> {<br/> // return myimageids. length; <br/> return integer. max_value; <br/>}</P> <p> Public object getitem (INT position)/* The method getitem to be overwritten, return position */<br/>{< br/> return position; <br/>}</P> <p> Public long getitemid (INT position) /* The method getitemid to be overwritten, and return the position */<br/> {<br/> return position; <br/>}</P> <p> Public View getview (INT position, view convertview, viewgroup parent)/* method to be overwritten getview, returns a view object */<br/> {<br/> // If (position = getcount ()) <br/> // {<br/> // position = 0; <br/> //} <br/> imageview I = new imageview (mcontext ); <br/> I. setimageresource (myimageids [position % myimageids. length]);/* set the image to the imageview object */<br/> I. setscaletype (imageview. scaletype. fit_xy);/* reset the width and height of the image */<br/> I. setlayoutparams (new gallery. layoutparams (136, 88);/* reset the width and height of Layout */<br/> I. setbackgroundresource (mgalleryitembackground);/* set the gallery background image */<br/> return I; /* return the imageview object */<br/>}< br/>}

 

 

2011-01-

14:33:17

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.