Android Study Notes 25: Use of gallery controls Gallery

Source: Internet
Author: User

In Android, the gallery control gallery is used to display the image list. You can drag the image left and right with your fingers to move it. Gallery can only display one row horizontally, and the images in the gallery list will move left or right based on different dragging conditions until the last image is displayed.

The following example demonstrates how to implement the gallery control function.

1. xml Layout

In the XML layout file, we use linearlayout to vertically layout the entire interface. Set an imageview control on the top of the interface to display the currently selected gallery list image. Set a gallery control at the lower end of the interface to display the gallery list images. The layout file is simple. The source code is as follows:

 1   <  Linearlayout  Xmlns: Android  = "Http://schemas.android.com/apk/res/android"  2   Xmlns: Tools = "Http://schemas.android.com/tools"  3   Android: Orientation  = "Vertical"  4   Android: layout_width  = "Match_parent"  5   Android: layout_height  = "Match_parent"   >  6   7       <  Imageview  8          Android: ID  = "@ + ID/imageview"  9   Android: layout_width  = "Match_parent"  10   Android: layout_height  = "Wrap_content"      >  11       </  Imageview  >  12       13       < Gallery  14           Android: ID  = "@ + ID/Gallery"  15   Android: layout_width  = "Match_parent"  16   Android: layout_height  = "Wrap_content"  17   Android: spacing  = "2dp"  18   Android: layout_margintop = "5dp"      >  19       </  Gallery  >  20       21   </  Linearlayout  > 

In the gallery attribute label, the Android: spacing = "2dp" attribute is used to specify the interval between images in the gallery list as 2dp.

ProgramThe result 1 after running is shown in.

Figure 1 Main Interface

 

2. imageadapter

To implement the gallery control function, a container is required to store the images displayed by Gallery. We can use a derived class imageadapter inherited from the baseadapter class to install these images.

In the imageadapter class, we need to implement the following four abstract methods in the adapter class:

(1) Public int getcount ();

(2) Public object getitem (INT position );

(3) Public long getitemid (INT position );

(4) Public View getview (INT position, view convertview, viewgroup parent );

The getcount () method is used to obtain the number of images in the imageadapter Adapter. The getitem () method is used to obtain the position of the image in the imageadapter Adapter. The getitemid () method is used to obtain the position of the image in the imageadapter; getview () is used to obtain the view object at the specified position in the imageadapter. The specific implementation methods of the above four abstract methods are as follows:

 1  /*  2   * Function: imageadapter class, inherited from baseadapter  3   * Author: blog Park-still indifferent  4        */  5       Public   Class ImageadapterExtends  Baseadapter {  6   7 Context mcontext; //  Context object  8           9           //  Constructor  10           Public  Imageadapter (context ){  11               This . Mcontext = Context; 12   }  13           14           //  Obtain the number of images  15           Public   Int  Getcount (){  16               Return  Mimageresourceids. length;  17   }  18   19          //  Obtain the image location in the library  20           Public Object getitem ( Int  Position ){  21               Return  Mimageresourceids [position];  22   }  23   24           //  Obtain the image location in the library  25          Public   Long Getitemid ( Int  Position ){  26               Return  Position;  27   }  28   29           //  Obtains the view object at the specified position in the adapter.  30           Public View getview ( Int Position, view convertview, viewgroup parent ){  31 Imageview = New  Imageview (mcontext );  32   Imageview. setimageresource (mimageresourceids [position]);  33 Imageview. setlayoutparams ( New Gallery. layoutparams (120,120 ));  34   Imageview. setscaletype (imageview. scaletype. fit_center );  35              Return  Imageview;  36   }  37           38 }

Here, imageview. setimageresource (mimageresourceids [position]) is used to load image resources at the specified position in imageadapter to imageview. Imageview. setlayoutparams (new gallery. layoutparams (120,120) is used to set the image display size in Gallery. Imageview. setscaletype (imageview. scaletype. fit_center) is used to set the scaling display format of images in imageview.

 

3. Load the adapter

After the imageadapter class is implemented, we can add the resource file to the gallery for display through the setadapter () method.

 

4. gallery event listener

In addition, In order to synchronously display the current image in the gallery control in the imageview control at the top of the interface shown in 1. You also need to listen to events on the gallery control to know which image of the gallery control you have selected. The specific implementation method is as follows:

1Mgallery. setonitemclicklistener (NewOnitemclicklistener (){2Public VoidOnitemclick (adapterview <?> Arg0, view arg1,IntArg2,LongArg3 ){3 Mimageview. setbackgroundresource (mimageresourceids [arg2]);4 }5});

 

 

Related information:

Gallery of Android controls

Http://www.cnblogs.com/salam/archive/2010/10/06/1844564.html

Android gallery Components

Http://www.eoeandroid.com/forum.php? MoD = viewthread & tid = 182297.

Android control gallery 3D effect

Http://android.tgbus.com/Android/tutorial/201108/362438.shtml

Androidgui21: Common imageswitcher skills

Http://blog.csdn.net/pathuang68/article/details/6541823

 

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.