Android自定义控件之广告视图

效果么就是每隔能秒钟切换一张广告图片,下面有圆点....直接上代码

public abstract class Adapter {
	public abstract int getCount();
	/**
	 * 页面数组
	 * @return
	 */
	public View[] getPages(){
		int count=getCount();
		View[] views=new View[count];
		for (int i = 0; i < count; i++) {
			views[i]=getPage(i);
		}
		return views;
	}
	/**
	 * 页面
	 * @param position
	 * @return
	 */
	public abstract View getPage(int position);
	
	/**
	 * 已选中背景视图
	 * @return
	 */
	public abstract View getCheckedBg();
	
	/**
	 * 获得屏幕宽度
	 * @param context
	 * @return
	 */
	public int getScreenWidth(Context context){
		return context.getResources().getDisplayMetrics().widthPixels;
	}
	/**
	 * 获得屏幕高度
	 * @param context
	 * @return
	 */
	public int getScreenHeight(Context context){
		return context.getResources().getDisplayMetrics().heightPixels;
	}
	/**
	 * DP转PIX
	 * @param context
	 * @param dp
	 * @return
	 */
	public int getFixPx(Context context,int dp){
		float scale=context.getResources().getDisplayMetrics().density;
		return (int)(scale*dp+0.5);
	}
}

/**
 * <h1>广告组件适配器</h1><p>
 * </p>
 * @author: linxcool.hu
 */
public abstract class GangedAdvertAdaper extends Adapter {
	protected Drawable dotNormal;
	protected Drawable dotChecked;
	
	public Drawable getDotNormal(Context context) {
		if(dotNormal==null)
			dotNormal=createPonit(context,Color.rgb(163, 163, 163));
		return dotNormal;
	}


	public Drawable getDotChecked(Context context) {
		if(dotChecked==null)
			dotChecked=createPonit(context,Color.rgb(238, 238, 238));
		return dotChecked;
	}

	public View[] getDots(Context context){
		View[] views=new View[getCount()];
		for (int i = 0; i < getCount(); i++) {
			views[i]=getDot(context, i);
		}
		return views;
	}
	
	
	public View getDot(Context context,int position){
		ImageView iv=new ImageView(context);
		iv.setBackgroundDrawable(getDotNormal(context));
		LinearLayout.LayoutParams pointParams=new LinearLayout.LayoutParams(
				getPointSize(context), getPointSize(context));
		pointParams.setMargins(
				getFixPx(context, 3), 0, 
				getFixPx(context, 3), 0);
		iv.setLayoutParams(pointParams);
		return iv;
	}
	
	public int getWidth(Context context){
		return getScreenWidth(context);
	}
	
	public int getHeight(Context context){
		return getFixPx(context, 140);
	}
	
	public int getFootHeight(Context context){
		return getFixPx(context, 20);
	}
	
	public int getPointSize(Context context){
		return getFixPx(context, 7);
	}
	
	public Drawable createPonit(Context context,int color){
		int size=getPointSize(context);
		Bitmap bitmap=Bitmap.createBitmap(size, size,Config.ARGB_8888);
		Drawable drawable=new BitmapDrawable(bitmap);
		
		Canvas canvas=new Canvas(bitmap);
		Paint paint= new Paint();
		paint.setColor(color);
		paint.setAntiAlias(true);
		
		canvas.drawCircle(size*1.0f/2, size*1.0f/2, size*1.0f/2, paint);
		drawable.draw(canvas);
		//canvas.restore();
		
		return drawable;
	}


	@Override
	public View getCheckedBg() {
		return null;
	}
	
}

/**
 * 广告视图
 * @author: linxcool.hu
 */
public class GangedAdvert extends FrameLayout implements OnGestureListener{
	private GestureDetector gestureDetector;

	private ViewFlipper viewFlipper;
	private GangedAdvertAdaper adaper;
	private LinearLayout pointsLayout;
	private View[] dotViews;
	//private TextView tv;

	private int lastIndex;
	private int currentIndex;

	public GangedAdvertAdaper getAdaper() {
		return adaper;
	}

	public GangedAdvert(Context context) {
		super(context);
	}
	
	public GangedAdvert(Context context,GangedAdvertAdaper adaper) {
		super(context);
		this.adaper=adaper;

		initResource();
		initPages();
		initPoints();

		viewFlipper.startFlipping();
	}

	protected void initResource(){
		dotViews=adaper.getDots(getContext());
		gestureDetector=new GestureDetector(getContext(), this);
		currentIndex=0;
		dotViews[currentIndex].setBackgroundDrawable(
				adaper.getDotChecked(getContext()));
	}

	protected void initPoints(){
		LinearLayout layout=new LinearLayout(getContext());
		layout.setGravity(Gravity.BOTTOM);
		LayoutParams params=new LayoutParams(
				LayoutParams.FILL_PARENT,adaper.getHeight(getContext()));
		this.addView(layout,params);
		
		pointsLayout=new LinearLayout(getContext());
		//pointsLayout.setBackgroundColor(Color.argb(250, 212, 212, 212));
		pointsLayout.setOrientation(LinearLayout.HORIZONTAL);
		pointsLayout.setGravity(Gravity.CENTER);
		LayoutParams ptParams=new LayoutParams(
				LayoutParams.FILL_PARENT,adaper.getFootHeight(getContext()));
		ptParams.setMargins(0, 0, 0, 0);
		
		layout.addView(pointsLayout,ptParams);

		for (int i = 0; i < adaper.getCount(); i++) {
			pointsLayout.addView(dotViews[i]);
		}
	}

	protected Drawable createPonit(int color){
		int size=adaper.getPointSize(getContext());
		Bitmap bitmap=Bitmap.createBitmap(size, size,Config.ARGB_8888);
		Drawable drawable=new BitmapDrawable(bitmap);

		Canvas canvas=new Canvas(bitmap);
		Paint paint= new Paint();
		paint.setColor(color);

		canvas.drawCircle(size*1.0f/2, size*1.0f/2, size*1.0f/2, paint);
		drawable.draw(canvas);
		//canvas.restore();

		return drawable;
	}

	protected void initPages(){
		viewFlipper=new ViewFlipper(getContext()){
			protected void onDetachedFromWindow() {
				try {super.onDetachedFromWindow(); 
				}catch (Exception e){stopFlipping(); }
			}
			@Override
			public void showNext() {
				super.showNext();
				changeDot();
			}
			@Override
			public void showPrevious() {
				super.showPrevious();
				changeDot();
			}
			private void changeDot(){
				lastIndex=currentIndex;
				currentIndex=getCurrentView().getId();
				dotViews[currentIndex].setBackgroundDrawable(
						adaper.getDotChecked(getContext()));
				dotViews[lastIndex].setBackgroundDrawable(
						adaper.getDotNormal(getContext()));
			}
		};
		viewFlipper.setLongClickable(true);
		viewFlipper.setFlipInterval(3000);
		LayoutParams vfParams=new LayoutParams(
				adaper.getWidth(getContext()),adaper.getHeight(getContext()));
		this.addView(viewFlipper,vfParams);

		for (int i = 0; i < adaper.getCount(); i++) {
			View c=adaper.getPage(i);
			c.setId(i);
			viewFlipper.addView(c);
		}
		setLeftAnimation();
	}

	@Override
	public boolean onDown(MotionEvent e) {return false;}
	@Override
	public void onShowPress(MotionEvent e) {}
	@Override
	public boolean onSingleTapUp(MotionEvent e) {return false;}
	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {
		return false;
	}
	@Override
	public void onLongPress(MotionEvent e) {}
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
		float x=e1.getX() - e2.getX();
		/*float y=e1.getY() - e2.getY();
		if(Math.abs(y)>Math.abs(x) || y> adaper.getHeight(getContext())/4)
			return true;*/
		if (x > 5) { 
			viewFlipper.showNext();
		}else if(x < -5) {  
			setRightAnimation();
			viewFlipper.showPrevious();
			setLeftAnimation();
		}
		return true;
	}

	/**
	 * 设置向左滑动动画
	 */
	public void setLeftAnimation(){
		int width=adaper.getWidth(getContext());
		Animation inAnimation=new TranslateAnimation(width, 0, 0, 0);
		inAnimation.setDuration(200);
		viewFlipper.setInAnimation(inAnimation);
		Animation outAnimation=new TranslateAnimation(0,-width, 0, 0);
		outAnimation.setDuration(200);
		viewFlipper.setOutAnimation(outAnimation);
	}

	/**
	 * 设置向右滑动动画
	 */
	public void setRightAnimation(){
		int width=adaper.getWidth(getContext());
		Animation inAnimation=new TranslateAnimation(-width, 0, 0, 0);
		inAnimation.setDuration(200);
		viewFlipper.setInAnimation(inAnimation);
		Animation outAnimation=new TranslateAnimation(0,width, 0, 0);
		outAnimation.setDuration(200);
		viewFlipper.setOutAnimation(outAnimation);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		gestureDetector.onTouchEvent(event);
		return true;
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		this.onTouchEvent(ev);
		return true;
	}
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值