您的位置:首页 > 移动开发 > Android开发

android Gallery 实现循环播放的效果

2012-02-25 11:13 387 查看
/*让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

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery">
<attr name="android:galleryStyle" />
</declare-styleable>
</resources>


第三步:编写主文件Gallery.java文件

package com.example.android.gallery;

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.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class gallery extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Gallery g = (Gallery) findViewById(R.id.gallery);
/* 新增一ImageAdapter并设定给Gallery对象 */
g.setAdapter(new ImageAdapter(this));

}

public class ImageAdapter extends BaseAdapter {

int mGallerystyle;
//类成员变量
private Context mContext;

public ImageAdapter(Context c) {
this.mContext = c;
/* 使用在res/values/attrs.xml中的定义 的Gallery属性. */
TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
//取得Gallery属性的Index
mGallerystyle = a.getResourceId(
R.styleable.Gallery_android_galleryStyle, 0);
/* 让对象的styleable属性能够反复使用 */
a.recycle();
}
// /* 一定要重写的方法getCount,传回图片数目总数 */
public int getCount() {
//返回图片的总数
//return mImageIds.length;
//返回一个很大的值,
return Integer.MAX_VALUE;

}
/* 一定要重写的方法getItem,传回position */
public Object getItem(int position) {
return position;
}
/* 一定要重写的方法getItemId,传回position */
public long getItemId(int position) {
return position;
}
/* 一定要重写的方法getView,传回一View对象 */
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
/* 设定图片给imageView对象 */  //下面这句主要起到循环的作用
i.setImageResource(mImageIds[position%mImageIds.length]);
//i.setImageResource(mImageIds[position]);
/* 重新设定图片的宽高 */
i.setScaleType(ImageView.ScaleType.FIT_XY);
//设置这个imageview对象的宽高,单位为dpi
i.setLayoutParams(new Gallery.LayoutParams(136, 88));
/* 传回imageView物件 */
return i;
}

private Integer[] mImageIds = {
R.drawable.photo1,
R.drawable.photo2,
R.drawable.photo3,
R.drawable.photo4,
R.drawable.photo5,
R.drawable.photo6,
R.drawable.photo7,
R.drawable.photo8
};
}
}


第四步,查看效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: