Gallery----附录上一篇源码

自定义gallery:

/**
 * 广告滚动类
 * 
 * 
 */
public class AdvertGallery extends Gallery {

	public static final int GUIDE_TIMER = 1;
	private long periodTime = 0;// 图片跳转时间间隔
	private MotionEvent e;

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case GUIDE_TIMER:
				/**
				 * gallery设置setSpacing()后,onKeyDown()会失效,添加onScroll方法可解决,
				 * 后面两个参数分别是自上次调用onScroll方法在X、Y上的距离
				 **/
				onScroll(null, null, 1, 0);
				onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
				break;
			}
		};
	};

	/**
	 * 构造方法,并开始计时
	 * 
	 * @param context
	 * @param attrs
	 */
	public AdvertGallery(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	/**
	 * 一次只滑动一张图片
	 */
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		// TODO Auto-generated method stub

		int kEvent;
		if (isScrollingRight(e1, e2)) {
			kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
		} else {
			kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
		}
		return onKeyDown(kEvent, null);
	}

	/**
	 * 解决滑动和点击事件冲突,重写该方法拦截触屏事件,返回false将会上抛该事件到子view上面, 返回true则事件不会上抛,将在
	 * Gallery内部的OnTouchEvent() 接收做处理
	 */
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		boolean b = super.onInterceptTouchEvent(ev);
		if (ev.getAction() == MotionEvent.ACTION_DOWN) {
			e = MotionEvent.obtain(ev);
			super.onTouchEvent(ev);
		} else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
			if (Math.abs(ev.getX() - e.getX()) > 20 || Math.abs(ev.getY() - e.getY()) > 20) {
				b = true;
			}
		}
		return b;
	}

	/**
	 * 判断手指是否向右滑动
	 * 
	 * @param e1
	 * @param e2
	 * @return
	 */
	private boolean isScrollingRight(MotionEvent e1, MotionEvent e2) {
		return e2.getX() > e1.getX();
	}

	/**
	 * 构造计时器
	 */
	private Timer timer = new Timer();
	private TimerTask task = new TimerTask() {
		@Override
		public void run() {
			handler.sendEmptyMessage(GUIDE_TIMER);
		}
	};

	/**
	 * 设置跳转间隔
	 * 
	 * @param periodTime
	 *            时间间隔
	 * @param num
	 *            自动滚动标志位, 广告数量
	 */
	public void setTimer(long periodTime, int num) {
		this.periodTime = periodTime;
		if (num > 1) {
			startTimer();
		}
	}

	/**
	 * 开始计时
	 */
	public void startTimer() {
		timer.schedule(task, periodTime, periodTime);

	}

	/**
	 * 注销计时器
	 */
	public void cancelTimer() {
		timer.cancel();
	}

}
自定义adapter:

public class AdvertGalleryAdapter extends BaseAdapter {

	private Context context;

	private int icon[] = null;// 图片资源

	public AdvertGalleryAdapter(Context context, int icon[]) {
		this.context = context;
		this.icon = icon;

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return Integer.MAX_VALUE;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return icon[position];
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (view == null) {
			view = LayoutInflater.from(context).inflate(R.layout.gallery_item, null);
		}
		ImageView imageView = (ImageView) view.findViewById(R.id.item_iv);
		imageView.setImageResource(icon[position % icon.length]);
		return view;
	}

}
自定义进度显示:

/**
 * 滚动广告进度图标 使用时在布局文件中定义该类,java文件中调用setProSize()和setCurrentPage()方法
 * 
 * 
 */
public class ProsLayout extends LinearLayout {
	private Context context;
	private int size;
	private List<ImageView> imageViewList = new ArrayList<ImageView>();


	public ProsLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		this.setGravity(Gravity.CENTER_HORIZONTAL);
	}


	/**
	 * 根据图片个数添加点
	 * 
	 * @param size
	 */
	public void setProSize(int size) {
		this.size = size;
		this.imageViewList.clear();
		this.removeAllViews();
		for (int i = 0; i < this.size; i++) {
			ImageView imageView = new ImageView(context);
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			params.leftMargin = 8;
			imageView.setBackgroundResource(R.drawable.point_advert);
			imageViewList.add(imageView);
			this.addView(imageView, params);
		}
	}


	/**
	 * 设置当前图标
	 * 
	 * @param curPro
	 */
	public void setCurrentPro(int curPro) {
		for (int i = 0, len = imageViewList.size(); i < len; i++) {
			ImageView imageView = imageViewList.get(i);
			if (i == curPro) {
				imageView.setBackgroundResource(R.drawable.point_advert_selected);
			} else {
				imageView.setBackgroundResource(R.drawable.point_advert);
			}
		}
		this.invalidate();
	}


}

java 文件中使用:

	advertGellery.setTimer(interval, advertNum);// 设置图片跳转间隔
			advertGalleryAdapter = new AdvertGalleryAdapter(this, handler, advertises, 1);// 滚动广告适配器
			advertGellery.setAdapter(advertGalleryAdapter);
			if (advertNum > 1) { // 多于一张,可以滚动
				prosLayout.setProSize(advertNum);
				advertGellery.setOnItemSelectedListener(new MyOnItemSelectedListener());// gallery监听选中状态
				int centerNum = Integer.MAX_VALUE / 2;
				centerNum -= centerNum % advertNum;
				advertGellery.setSelection(centerNum);// 设置默认选中
			}
/**
	 * 监听AdvertGallery选中事件
	 */
	private class MyOnItemSelectedListener implements OnItemSelectedListener {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
			prosLayout.setCurrentPro(position % advertNum); // 设置Gallery当前位置
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
	}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值