Android gallery can display images cyclically

Source: Internet
Author: User

 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 resids 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 resids array has only 15 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 15, take the 0th elements of the resids array and take 1st elements at the time of 16). The simplest method is to take the remainder. The Code is as follows:

Resids [position % resids. 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.

Directly upload code

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

 

Package net. blogjava. Mobile;

Import Android. App. activity;
Import Android. content. context;
Import Android. content. res. typedarray;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. viewgroup;
Import Android. View. animation. animationutils;
Import Android. widget. adapterview;
Import Android. widget. baseadapter;
Import Android. widget. Gallery;
Import Android. widget. imageswitcher;
Import Android. widget. imageview;
Import Android. widget. adapterview. onitemselectedlistener;
Import Android. widget. Gallery. layoutparams;
Import Android. widget. viewswitcher. viewfactory;

Public class main extends activity implements onitemselectedlistener,
Viewfactory
{
Private gallery Gallery;
Private imageadapter;

Private int [] resids = new int []
{R. drawable. Item1, R. drawable. item2, R. drawable. item3, R. drawable. item4,
R. drawable. item5, R. drawable. item6, R. drawable. item7,
R. drawable. item8, R. drawable. item9, R. drawable. item10,
R. drawable. item11, R. drawable. item12, R. drawable. item13,
R. drawable. item14, R. drawable. item15 };

Public class imageadapter extends baseadapter
{
Int mgalleryitembackground;
Private context mcontext;

Public imageadapter (context)
{
Mcontext = context;
Typedarray = obtainstyledattributes (R. styleable. Gallery );
Mgalleryitembackground = typedarray. getresourceid (
R. styleable. gallery_android_galleryitembackground, 0 );
}
// Improve at 1st, and return a large value, for example, integer. max_value
Public int getcount ()
{
Return integer. max_value;
}

Public object getitem (INT position)
{
Return position;
}

Public long getitemid (INT position)
{
Return position;
}
 
Public View getview (INT position, view convertview, viewgroup parent)
{
Imageview = new imageview (mcontext );
// Improved by. The image resource ID in the resids array is obtained cyclically by taking the remainder.
Imageview. setimageresource (resids [position % resids. Length]);
Imageview. setscaletype (imageview. scaletype. fit_xy );
Imageview. setlayoutparams (new gallery. layoutparams (163,106 ));
Imageview. setbackgroundresource (mgalleryitembackground );
Return imageview;
}
}

@ Override
Public void oncreate (bundle savedinstancestate)
{
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Gallery = (Gallery) findviewbyid (R. Id. Gallery );
Imageadapter = new imageadapter (this );
Gallery. setadapter (imageadapter );
Gallery. setonitemselectedlistener (this );
}

Summary
This article describes how to implement the cyclically displayed gallery component. In fact, this loop is only a pseudo loop, but because the total number of images returned by the getcount method is large (more than 2 billion), this means that it is very close to an infinite loop. There are two key points to achieve cyclic image display:
1. The getcount method returns a large integer (for example, integer. max_value ).
2. In the getview method, the remainder method is used to obtain the resource ID of the image cyclically.

This article is reproduced and shared with you.

 

Note: QQ technology exchange group: add one if you are interested in 108614806.


 

 

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.