/* Method for loop playback of galllery
* 1. Make the getcount method return a large value. We recommend that you return integer. max_value ., This value can reach more than 2 billion
* 2. cyclically obtain the image resource ID in the resids array by taking the remainder in the getview method.
* 3. Loop gallery reference http://blog.csdn.net/herryz/article/details/6141957
*/
Step 1: Write the layout file main. xml. The specific code is as follows:
View code
1 <?xml version="1.0" encoding="utf-8"?>
2 <Gallery xmlns:android="http://schemas.android.com/apk/res/android"
3 android:id="@+id/gallery"
4 android:layout_width="fill_parent"
5 android:layout_height="wrap_content"
6 />
Step 2: Compile the grallly attribute under values. The file name is the attrs. xml file.
View code
1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3 <declare-styleable name="Gallery">
4 <attr name="android:galleryStyle" />
5 </declare-styleable>
6 </resources>
Step 3: Compile the master file gallery. Java
1 package com. example. Android. Gallery;
2
3 Import Android. App. activity;
4 Import Android. content. context;
5 import Android. content. res. typedarray;
6 Import Android. OS. Bundle;
7 Import Android. View. view;
8 Import Android. View. viewgroup;
9 Import Android. widget. baseadapter;
10 Import Android. widget. Gallery;
11 import Android. widget. imageview;
12
13 public class gallery extends activity {
14 @ override
15 public void oncreate (bundle savedinstancestate ){
16 super. oncreate (savedinstancestate );
17 setcontentview (R. layout. Main );
18
19 gallery G = (Gallery) findviewbyid (R. Id. Gallery );
20/* add an imageadapter and set it to the gallery object */
21G. setadapter (New imageadapter (this ));
22
23}
24
25 public class imageadapter extends baseadapter {
26
27 int mgallerystyle;
28 // class member variables
29 private context mcontext;
30
31 public imageadapter (context c ){
32 This. mcontext = C;
33/* use the gallery attribute defined in RES/values/attrs. xml .*/
34 typedarray A = obtainstyledattributes (R. styleable. Gallery );
35 // obtain the index of the gallery attribute
36 mgallerystyle = A. getresourceid (
37 R. styleable. gallery_android_gallerystyle, 0 );
38/* enable the object's styleable attribute to be used repeatedly */
39 A. Recycle ();
40}
41 // * The method getcount to be rewritten, total number of returned images */
42 Public int getcount (){
43 // total number of returned Images
44 // return mimageids. length;
45 // return a large value,
46 Return integer. max_value;
47
48}
49/* The method getitem that must be rewritten, returns the position */
50 public object getitem (INT position ){
51 return position;
52}
53/* The method getitemid to be rewritten, and return the position */
54 public long getitemid (INT position ){
55 return position;
56}
57/* The method getview to be rewritten to return a view object */
58 public view getview (INT position, view convertview, viewgroup parent ){
59 imageview I = new imageview (mcontext );
60/* set the image to the imageview object * // The following sentence mainly serves as a loop.
61 I. setimageresource (mimageids [position % mimageids. Length]);
62 // I. setimageresource (mimageids [position]);
63/* reset the width and height of the image */
64 I. setscaletype (imageview. scaletype. fit_xy );
65 // set the width and height of the imageview object, in DPI
66 I. setlayoutparams (new gallery. layoutparams (136, 88 ));
67/* return the imageview object */
68 return I;
69}
70
71 private integer [] mimageids = {
72 R. drawable. photo1,
73 R. drawable. photo2,
74 R. drawable. photo3,
75 R. drawable. photo4,
76 R. drawable. photo5,
77 R. drawable. photo6,
78 R. drawable. photo7,
79 R. drawable. photo8
80 };
81}
82}
Step 4: View: