直接上图:
再上代码。。。。。。。。。。。。
activity类代码:
//画廊 数据适配器
MyGalleryAdapter myGalleryAdapter = new MyGalleryAdapter(this.getApplicationContext(),null);
myGallery.setAdapter(myGalleryAdapter);
//取第301张图片作为默认居中图片,这样,解决用户可以向左滑动
//gallery.setselection(300);
myGallery.setAnimationDuration(230);
myGallery.setOnItemSelectedListener(gallerySelectListener);
Bitmap bitmap = MeiLiIndexUtil.b(dotNormal, dotHeight, size, 0);
point.setImageBitmap(bitmap);
//定时滚动myGallery画廊
initTimerPoint();
}
@Override
protected void findViewById() {
//画廊
myGallery = (MyGallery)findViewById(R.id.big_gallery);
//画廊下面的点
point = (ImageView)findViewById(R.id.point);
//正在显示的(红色)
dotNormal = ((BitmapDrawable)getResources().getDrawable(R.drawable.dot_normal)).getBitmap();
//未显示的(灰色)
dotHeight = ((BitmapDrawable)getResources().getDrawable(R.drawable.dot_highlight)).getBitmap();
}
加点部分的代码:public class MeiLiIndexUtil {
//给画廊下面加点
public static Bitmap b(Bitmap dotNormal, Bitmap dotheight, int count,
int index) {
Bitmap localBitmap = Bitmap.createBitmap(count * 25, 25,
Bitmap.Config.ARGB_4444);
Canvas localCanvas = new Canvas(localBitmap);
int i2 = 0;
for (int i1 = 0; i1 < count; i1++) {
if (i1 == index) {
localCanvas.drawBitmap(dotheight, i2, 0.0F, null);
} else {
localCanvas.drawBitmap(dotNormal, i2, 0.0F, null);
}
i2 += 25;
}
return localBitmap;
}
}
定时滚动代码:
//定时滚动myGallery画廊
private final Handler mHandler = new Handler() {
public void handleMessage(Message message) {
super.handleMessage(message);
switch (message.what) {
case 1:
myGallery.setSelection(myGallery.getSelectedItemPosition() + 1);
break;
}
}
};
private void initTimerPoint() {
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
mHandler.sendEmptyMessage(1);
}
}, 5000, 5000);//5s
}
适配器代码,关键代码是:
getCount 返回无限大,imageView.setImageResource(resIds[position % resIds.length]); 取余
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
v = flater.inflate(R.layout.jumei_mall_gallery_item, null);
}
ImageView imageView = (ImageView)v.findViewById(R.id.product_image);
imageView.setImageResource(resIds[position % resIds.length]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//imageView.setLayoutParams(new Gallery.LayoutParams(163, 106));
return v;
}