【仿聚美系列】Gallery定时、 循环 、滚动 、加点 显示

本文介绍了一种使用自定义Adapter实现Android Gallery无限循环显示的方法,并详细展示了如何通过代码为Gallery下方添加指示点及实现定时自动滚动的功能。

   直接上图:       

                                  

  再上代码。。。。。。。。。。。。

 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;
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值