Effect of WindowManager picture toggle

Source: Internet
Author: User

I've been having a problem with this for a while. The original plan is to put Viewpager in the WindowManager, and then set a timer, let the picture automatically switch, but for a long time, found in any case, this picture only show one. Although the log can see that the picture is actually switching, but in addition to this initialization of this image, the other is a black window, which is not displayed.

Blocked, find another way out. In fact, Pageradapter is not necessary. Android is this convenient, anyway view, no matter how the whole, are can be placed inside the window, I do not understand the interface, so here also can not say why. But this demand, there are also some (such as some of the more disgusting advertising programs, that is, to monopolize the top, then using WindowManager is the more appropriate way).

Nonsense not much to say directly on the code

1  Public classMainactivityextendsActivity {2 PrivateRunnable viewpagerrunnable;3 PrivateHandler Mainthreadhandler;4 PrivateWindowManager mwndmgr;5 PrivateViewpager Mviewpager;6 PrivateImageView Mimgview;7 Privatewindowmanager.layoutparams Mlayoutparams;8 PrivateContext Mcontext;9 PrivateList<imageview>mimgviewlist;Ten Private int[] Picres ={R.drawable.first,r.drawable.second,r.drawable.third}; One intCurrentindex = 0; A @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); the Setcontentview (r.layout.activity_main); -Mcontext = This. Getapplicationcontext (); -Mainthreadhandler =NewHandler (); - Prepareimageview (); + Initview (); - Startautomaticroll (); + } A  at  - @Override -  Public BooleanOncreateoptionsmenu (Menu menu) { - //inflate the menu; This adds items to the action bar if it is present. - getmenuinflater (). Inflate (R.menu.main, menu); - return true; in } -  to @Override +  Public Booleanonoptionsitemselected (MenuItem item) { - //Handle Action Bar item clicks here. The Action Bar would the //automatically handle clicks on the Home/up button, so long * //As you specify a the parent activity in Androidmanifest.xml. $ intID =Item.getitemid ();Panax Notoginseng if(id = =r.id.action_settings) { - return true; the } + return Super. onoptionsitemselected (item); A } the  + Private voidPrepareimageview () { -Mviewpager =NewViewpager (mcontext); $Mwndmgr =(WindowManager) Mcontext.getsystemservice (context.window_service); $Displaymetrics Outmetrics =Newdisplaymetrics (); - Mwndmgr.getdefaultdisplay (). Getmetrics (outmetrics); -Mlayoutparams =Newwindowmanager.layoutparams (); theMlayoutparams.type =WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; -Mlayoutparams.width =Outmetrics.widthpixels;WuyiMlayoutparams.height = outmetrics.heightpixels-150; the //Mviewpager.setlayoutparams (mlayoutparams); -Mimgviewlist =NewArraylist<imageview>(); Wu intLen =picres.length; -  for(inti = 0; i < Len; i++){ AboutImageView view =NewImageView (mcontext); $ View.setbackgroundresource (Picres[i]); - mimgviewlist.add (view); - } - //Mviewpager.setadapter (New Viewadapha (mimgviewlist)); A } +  the  Public voidInitview () { - //Mwndmgr.addview (Mviewpager, mlayoutparams); $Mimgview =Mimgviewlist.get (currentindex); the Mimgview.setlayoutparams (mlayoutparams); the Mwndmgr.addview (Mimgview, mlayoutparams); the //mwndmgr.updateviewlayout (Mimgview, mlayoutparams); the } -  in Private voidStartautomaticroll () { theViewpagerrunnable =NewRunnable () { the @Override About  Public voidrun () { the //int nowindex = Mviewpager.getcurrentitem ();  the //int count = Mviewpager.getadapter (). GetCount ();  the if(Currentindex + 1 >= 3) { + //mviewpager.setcurrentitem (0); -Currentindex = 0; the}Else {BayiCurrentindex = currentindex + 1; the } the Mwndmgr.removeview (mimgview); -Mimgview =Mimgviewlist.get (currentindex); - Mimgview.setlayoutparams (mlayoutparams); the //mviewpager.removeallviews (); theLOG.I ("index", "index=" +currentindex); the //mviewpager.removeallviews (); the //Mviewpager.addview (Mimgviewlist.get (Currentindex)); - Mwndmgr.addview (Mimgview, mlayoutparams); the //mwndmgr.updateviewlayout (Mimgview, mlayoutparams); theMainthreadhandler.postdelayed (viewpagerrunnable, 3000); the }94 }; theMainthreadhandler.postdelayed (viewpagerrunnable, 3000); the } the}

This way compared to the stamp, in the picture between the switch, there will be a flash phenomenon, that is, the transition is not smooth. If you have a friend to know, please advise what is the situation. Or how to do in my way, put Viewpager into the windowmanager inside, and then put the ImageView array in Viewpager, and then can achieve the effect of normal switching.

===========================================

Modified again, the effect is better. haha ~ ~ ~

public class Mainactivity extends Activity {private Runnable viewpagerrunnable;private Handler Mainthreadhandler; Private WindowManager mwndmgr;private ImageView mimgview;private windowmanager.layoutparams mlayoutparams;private Context mcontext;private list<imageview> mimgviewlist;private int[] picres = {R.drawable.first,    R.drawable.second,r.drawable.third};int currentindex = 0;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Mcontext = This.getapplicationcontext ();        Mainthreadhandler = new Handler ();        Prepareview ();        Initview ();    Startautomaticroll (); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Actio        n Bar if it is present.        Getmenuinflater (). Inflate (R.menu.main, menu);    return true; } @Override public boolean onoptionsitemselected (MenuItem Item){//Handle Action Bar item clicks here.  The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent        Activity in Androidmanifest.xml.        int id = item.getitemid ();        if (id = = r.id.action_settings) {return true;    } return super.onoptionsitemselected (item); private void Prepareview () {new Viewpager (mcontext); mwndmgr = (WindowManager) mcontext.getsystemservice (Contex T.window_service);D isplaymetrics outmetrics = new Displaymetrics (); Mwndmgr.getdefaultdisplay (). GetMetrics ( Outmetrics); mlayoutparams = new Windowmanager.layoutparams (); mlayoutparams.type = WindowManager.LayoutParams.TYPE_    System_alert;mlayoutparams.width = Outmetrics.widthpixels;mlayoutparams.height = outmetrics.heightpixels-150;    Mimgviewlist = new arraylist<imageview> ();    int len = picres.length;    for (int i = 0; i < len; i++) {ImageView view = new ImageView (mcontext); View.setbackgroundResource (Picres[i]);    Mimgviewlist.add (view); }} public void Initview () {Mimgview = Mimgviewlist.get (currentindex); Mimgview.setlayoutparams (mlayoutparams); mWn        Dmgr.addview (Mimgview, mlayoutparams);} private void Startautomaticroll () {viewpagerrunnable = new Runnable () {@Overridepublic void run () {if (Currentindex + 1 &G T;= 3) {currentindex = 0;} else {currentindex = Currentindex + 1;} Mimgview.setbackgroundresource (Picres[currentindex]); LOG.I ("index", "index=" +currentindex); Mainthreadhandler.postdelayed (viewpagerrunnable, 3000);}; Mainthreadhandler.postdelayed (viewpagerrunnable, 3000);}}

  

Effect of WindowManager picture toggle

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.