android Gallery 实现循环播放的效果

/*让Galllery循环播放的方法
 * 1. 使getCount方法返回一个很大的值。建议返回Integer.MAX_VALUE。,这个值可以到达20亿多
 * 2. 在getView方法中通过取余来循环取得resIds数组中的图像资源ID。
 * 3.循环Gallery参考http://blog.csdn.net/herryz/article/details/6141957
 */

第一步:编写布局文件main.xml,具体代码如下

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 />

第二步:在values下编写Grallly的属性,文件名为attrs.xml文件

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>

第三步:编写主文件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 /* 新增一ImageAdapter并设定给Gallery对象 */
21 g.setAdapter(new ImageAdapter(this));
22
23 }
24
25 public class ImageAdapter extends BaseAdapter {
26
27 int mGallerystyle;
28 //类成员变量
29 private Context mContext;
30
31 public ImageAdapter(Context c) {
32 this.mContext = c;
33 /* 使用在res/values/attrs.xml中的定义 的Gallery属性. */
34 TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
35 //取得Gallery属性的Index
36 mGallerystyle = a.getResourceId(
37 R.styleable.Gallery_android_galleryStyle, 0);
38 /* 让对象的styleable属性能够反复使用 */
39 a.recycle();
40 }
41 // /* 一定要重写的方法getCount,传回图片数目总数 */
42 public int getCount() {
43 //返回图片的总数
44 //return mImageIds.length;
45 //返回一个很大的值,
46 return Integer.MAX_VALUE;
47
48 }
49 /* 一定要重写的方法getItem,传回position */
50 public Object getItem(int position) {
51 return position;
52 }
53 /* 一定要重写的方法getItemId,传回position */
54 public long getItemId(int position) {
55 return position;
56 }
57 /* 一定要重写的方法getView,传回一View对象 */
58 public View getView(int position, View convertView, ViewGroup parent) {
59 ImageView i = new ImageView(mContext);
60 /* 设定图片给imageView对象 */ //下面这句主要起到循环的作用
61 i.setImageResource(mImageIds[position%mImageIds.length]);
62 //i.setImageResource(mImageIds[position]);
63 /* 重新设定图片的宽高 */
64 i.setScaleType(ImageView.ScaleType.FIT_XY);
65 //设置这个imageview对象的宽高,单位为dpi
66 i.setLayoutParams(new Gallery.LayoutParams(136, 88));
67 /* 传回imageView物件 */
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 }

第四步,查看效果图:


 

原文地址:https://www.cnblogs.com/shaoyangjiang/p/2367567.html