Android广告展示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();
		}
	}

}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。