码迷,mamicode.com
首页 > 移动开发 > 详细

Android广告展示ViewPager

时间:2014-12-05 12:52:49      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:广告   图片   viewpager   

从资源文件里面获取

public class MainActivity extends Activity {

	private LayoutInflater inflater;
	private LinearLayout line_mountainflower_main;
	private ViewPager adViewPager;
	private View adview1, adview2, adview3;
	private TimerTask timertask;
	private Timer timer;
	private int[] drawables = { R.drawable.five, R.drawable.four,
			R.drawable.six };
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			// 设置他的滑动速度
			adViewPager.setCurrentItem(currentitem % 5, false);
			currentitem++;
		};
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		inflater = LayoutInflater.from(MainActivity.this);
		initViews();
		initAds();
	}

	private void hehe() {
		// TODO Auto-generated method stub
		if (timertask != null) {
			timertask.cancel();
			timertask = null;
		}
		if (timer != null) {
			timer.cancel();
			timer = null;
		}
		if (timertask == null) {
			timertask = new TimerTask() {
				@Override
				public void run() {
					handler.obtainMessage().sendToTarget();
				}
			};
			timer = new Timer();
			timer.schedule(timertask, 2000, 2000);
		}
	}

	private ArrayList<View> adViews;
	private ImageView adpic1, adpic2, adpic3;
	private ImageView[] images;
	private int currentitem = 0;
	private AdViewPagerAdapter adPagerAdapter;

	private void initAds() {
		// TODO Auto-generated method stub
		line_mountainflower_main.removeAllViews();
		View adView = inflater.inflate(R.layout.mountainflower_admain, null);
		line_mountainflower_main.addView(adView);
		adViewPager = (ViewPager) findViewById(R.id.adviewpager);
		adViews = new ArrayList<View>();
		adview1 = inflater.inflate(R.layout.adview1, null);
		adview2 = inflater.inflate(R.layout.adview2, null);
		adview3 = inflater.inflate(R.layout.adview3, null);
		adpic1 = (ImageView) adview1.findViewById(R.id.adpic1);
		adpic2 = (ImageView) adview2.findViewById(R.id.adpic2);
		adpic3 = (ImageView) adview3.findViewById(R.id.adpic3);
		images = new ImageView[] { adpic1, adpic2, adpic3 };
		for (int i = 0; i < images.length; i++) {
			images[i].setImageResource(drawables[i]);
		}
		adViews.add(adview1);
		adViews.add(adview2);
		adViews.add(adview3);
		currentitem = 0;
		adPagerAdapter = new AdViewPagerAdapter(adViews);
		adViewPager.setAdapter(adPagerAdapter);
		hehe();
		
	}

	private void initViews() {
		// TODO Auto-generated method stub
		line_mountainflower_main = (LinearLayout) findViewById(R.id.line_mountainflower_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}


Adapter:

public class AdViewPagerAdapter extends PagerAdapter {
	private ArrayList<View> Views;
	private int[] drawables = {R.drawable.five,R.drawable.four,R.drawable.six};
	public AdViewPagerAdapter(ArrayList<View> views) {
		super();
		this.Views = views;

	}

	@Override
	public int getCount() {

		return Views.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {

		return arg0 == arg1;
	}

	@Override
	public void destroyItem(View container, int position, Object object) {

		((ViewPager) container).removeView(Views.get(position));

	}

	@Override
	public Object instantiateItem(View container, int position) {

		if (position >= Views.size() - 1) {

			int newPosition = position % Views.size();

			position = newPosition;
		}

		try {
			((ViewPager) container).addView(Views.get(position), 0);
			
			
		} catch (Exception e) {
		}
		return Views.get(position);
	}

}


FileUtil:

public class FileUtil {

	/**
	 * 创建目录
	 * 
	 * @param dirName
	 *            目录名字
	 * @return
	 */
	public static File createSDDir(String dirName) {
		File dir = new File(dirName);
		dir.mkdirs();
		return dir;
	}

	/**
	 * 创建文件
	 * 
	 * @param fileName
	 *            文件名字
	 * @return
	 * @throws IOException
	 */
	public static File createSDFile(String fileName) throws IOException {
		File file = new File(fileName);
		file.createNewFile();
		return file;
	}

	/**
	 * 得到设备外部存储目录
	 */
	public static final String SDPath = android.os.Environment
			.getExternalStorageDirectory().getPath() + "/";
	/**
	 * 保存文件到SD卡
	 * 
	 * @param bitmap
	 *            图片对象
	 * @param file
	 *            保存文件的路径
	 */
	public static void copyFile(Bitmap bitmap, File file) {
		try {
			BufferedOutputStream bos = new BufferedOutputStream(
					new FileOutputStream(file));
			bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
			bos.flush();
			bos.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}


Android广告展示ViewPager

标签:广告   图片   viewpager   

原文地址:http://blog.csdn.net/zhangjiaming8889/article/details/41744549

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!