Android AD Show Viewpager

Source: Internet
Author: User

Get from the resource file

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};p rivate Handler Handler = new Handler ( {public void Handlemessage (Android.os.Message msg) {//set his sliding speed Adviewpager.setcurrentitem (currentitem% 5, false); currentitem++;};}; @Overridepublic 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 stubif (TimerTask! = null) {Timertask.cancel (); timertask = null;} if (timer! = null) {timer.cancel (); timer = null;} if (TimerTask = = null) {TimerTask = new TimerTask () {@Overridepublic 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 Stubline_ 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 Stubline_mountainflower_main = (linearlayout) Findviewbyid ( R.id.line_mountainflower_main);} @Overridepublic 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.dr Awable.five,r.drawable.four,r.drawable.six};p ublic Adviewpageradapter (arraylist<view> views) {super (); views = views;} @Overridepublic int GetCount () {return views.size ();} @Overridepublic boolean isviewfromobject (View arg0, Object arg1) {return arg0 = = arg1;} @Overridepublic void Destroyitem (View container, int position, object object) {(Viewpager) container). Removeview ( Views.get (position));} @Overridepublic Object Instantiateitem (View container, int position) {if (position >= views.size ()-1) {int Newpositio n = position% views.size ();p osition = newposition;} try {(Viewpager) container). AddView (Views.get (position), 0);} catch (Exception e) {}return views.get (position);}}


Fileutil:

public class Fileutil {/** * Create directory *  * @param dirName *            directory name * @return */public static File createsddir (String Dirn AME) {File Dir = new File (dirName);d ir.mkdirs (); return dir;} /** * Create file *  * @param fileName *            file name * @return * @throws ioexception */public static file Createsdfile (String fil ename) throws IOException {File File = new file (fileName); File.createnewfile (); return file;} /** * Get the device external storage directory */public static final String Sdpath = Android.os.Environment.getExternalStorageDirectory (). GetPath () + " /";/** * save file to SD card *  * @param bitmap *            Picture Object * @param file *            save path */public static void CopyFile (bitmap bitmap , file file) {try {bufferedoutputstream bos = new Bufferedoutputstream (new FileOutputStream (file)); Bitmap.compress ( Bitmap.CompressFormat.JPEG, Bos.flush, BOS); Bos.close (); catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}


Android AD Show Viewpager

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.