Android Popupwindow Full Screen detail and instance code _android

Source: Internet
Author: User

Android Popupwindow Full Screen

Many applications can often see the effect of pop-up this popupwindow, do a small demo share. Demo idea is to traverse the file, find pictures and Pictures folder placed on the Popupwindow above. Click the button to eject this Popupwindow, where the animation is set for Popupwindow.

Popupwindow Full Screen Code feed

Limited need to customize Popupwindow, here do not look at the Popupwindow inside to show the content, mainly set Popupwindow height.

public class Popupwindowlist extends Popupwindow {private int mwidth;
  private int mheight;
  Private View Mcontentview;
  Private list<filebean> Mfilebeans;

  Private ListView Mlistview;
    Public popupwindowlist (Context context,list<filebean> Mfilebeans) {super (context);
    This.mfilebeans=mfilebeans;
    Calculate width and height calwidthandheight (context);
    SetWidth (Mwidth);
    SetHeight (Mheight);
    mcontentview= Layoutinflater.from (context). Inflate (R.layout.popupwidow,null);
    Set layout and related properties Setcontentview (Mcontentview);
    Setfocusable (TRUE);
    Settouchable (TRUE);
    Settouchable (TRUE); 
      Settouchinterceptor (New View.ontouchlistener () {@Override public boolean ontouch (View V, motionevent event) { Click Popupwindow outside the Popupwindow disappears if (event.getaction () = = Motionevent.action_outside) {Dismiss (
        );
      return false;

  }
    }); /** * Set the size of the Popupwindow * @param context * * private void CALwidthandheight (Context context) {WindowManager wm= (windowmanager) Context.getsystemservice (context.window_service
    );
    Displaymetrics metrics= new Displaymetrics ();

    Wm.getdefaultdisplay (). Getmetrics (metrics);
    Mwidth=metrics.widthpixels;
  Sets the 70% mheight= (int) (metrics.heightpixels*0.7) with a height of full screen height;

 }
}

Click the button to eject Popupwindow

 Mbuttonshowpopup.setonclicklistener (New View.onclicklistener () {
      @Override public
      void OnClick (View v) {
        //Click when pop-up Popupwindow, screen dimming
        popupwindowlist.setanimationstyle (r.style.listphotoselect);
        Popupwindowlist.showasdropdown (mbuttonshowpopup, 0, 0);
        Lightoff ();

      }
    );

 private void Lightoff () {
    windowmanager.layoutparams Lp=getwindow (). GetAttributes ();
    lp.alpha=0.3f;
    GetWindow (). SetAttributes (LP);
  }

One, Filebean class save information

Filebean as shown in Figure Popupwindow, you need to save the path to the file, the name of the folder, the number of files in the folder, and the path to the first picture in the folder. Basically all are set, get method

/** * This class are used to record file information like the name of the file, the path of the
  File ... * * */public class Filebean {private String mfilename;
  Private String Mfilepath;
  Private String Mfistimgpath;

  private int mphotocount;
  Public String Getmfilename () {return mfilename;
  } public void Setmfilename (String mfilename) {this.mfilename = Mfilename;
  Public String Getmfilepath () {return mfilepath;
    } public void Setmfilepath (String mfilepath) {this.mfilepath = Mfilepath;
    int Index=this.mfilepath.lastindexof (file.separator);
  Mfilename=this.mfilepath.substring (index);
  Public String Getmfistimgpath () {return mfistimgpath;
  } public void Setmfistimgpath (String mfistimgpath) {this.mfistimgpath = Mfistimgpath;
  public int Getmphotocount () {return mphotocount;
  The public void Setmphotocount (int mphotocount) {this.mphotocount = Mphotocount; }
}

Second, Popupwidow interface settings

Custom Popupwindow,

public class Popupwindowlist extends Popupwindow {private int mwidth;
  private int mheight;
  Private View Mcontentview;
  Private list<filebean> Mfilebeans;
  Private ListView Mlistview;
  Observer mode public interface onseletedlistener{void onselected (Filebean bean);
  Private Onseletedlistener listener;
  public void Setonselecterlistener (Onseletedlistener listener) {This.listener=listener;
    } public popupwindowlist (context context,list<filebean> Mfilebeans) {super (context);
    This.mfilebeans=mfilebeans;
    Calwidthandheight (context);
    SetWidth (Mwidth);
    SetHeight (Mheight);
    mcontentview= Layoutinflater.from (context). Inflate (R.layout.popupwidow,null);
    Setcontentview (Mcontentview);
    Setfocusable (TRUE);
    Settouchable (TRUE);
    Settouchable (TRUE); 
      Settouchinterceptor (New View.ontouchlistener () {@Override public boolean ontouch (View V, motionevent event) { Popupwindow disappears if (event) when clicking outside the Popupwindow area. getaction () = = Motionevent.action_outside) {dismiss ();
      return false;
    }
    });
    Initlistview (context);
  Initevent (); private void Initevent () {}///Initialize Popupwindow listview private void Initlistview (context context) {MyList
    Viewadapter adapter=new Mylistviewadapter (Context,0,mfilebeans);
    mlistview= (ListView) Mcontentview.findviewbyid (R.id.listview);
  Mlistview.setadapter (adapter); /** * Set Popupwindow Size * @param context/private void calwidthandheight (context context) {Windowman
    Ager wm= (WindowManager) Context.getsystemservice (Context.window_service);
    Displaymetrics metrics= new Displaymetrics ();

    Wm.getdefaultdisplay (). Getmetrics (metrics);
    Mwidth=metrics.widthpixels;
  mheight= (int) (metrics.heightpixels*0.7);

 }
}

Third, click the button to eject Popupwindow

public class Popupwindowtest extends Appcompatactivity {private Button mbuttonshowpopup;
  Private set<string> Mfilepath;
  Private list<filebean> Mfilebeans;
  Popupwindowlist popupwindowlist; Private Handler mhandler=new Handler () {@Override public void Handlemessage (msg) {Super.handlemessa
      GE (msg);
    Initpopupwindow ();

  }
  };
    private void Initpopupwindow () {popupwindowlist=new popupwindowlist (This,mfilebeans); 
        Popupwindowlist.setondismisslistener (New Popupwindow.ondismisslistener () {@Override public void Ondismiss () {
      Lighton ();

  }
    });
    When//popupwindow disappears, make the screen return to normal private void Lighton () {windowmanager.layoutparams Lp=getwindow (). GetAttributes ();
    lp.alpha=1.0f;
  GetWindow (). SetAttributes (LP);
    } @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (r.layout.popupwindowtestlayout); Mbuttonshowpopup= (Button) Findviewbyid (r.id.button_showpopup);
    Mfilebeans=new arraylist<> ();
    InitData ();

  Initevent ();
      private void Initevent () {Mbuttonshowpopup.setonclicklistener () {new View.onclicklistener () {@Override public void OnClick (View v) {//When clicked pops up Popupwindow, the screen darkens popupwindowlist.setanimationstyle (r.style.listphot
        Oselect);
        Popupwindowlist.showasdropdown (mbuttonshowpopup, 0, 0);

      Lightoff ();
  }
    });
    private void Lightoff () {windowmanager.layoutparams Lp=getwindow (). GetAttributes ();
    lp.alpha=0.3f;
  GetWindow (). SetAttributes (LP);
  //Open thread initialization data, traverse file to find all picture files, and its folders and paths to save. private void InitData () {if (! Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {Toastutils.showtoast (this, "
    Current sdcard not used ");
        New Thread () {@Override public void run () {super.run ();
        Uri imgsuri= MediaStore.Images.Media.EXTERNAL_CONTENT_URI; ContentrEsolver Contentresolver=getcontentresolver (); Cursor cursor=contentresolver.query (imgsuri,null,mediastore.images.media.mime_type + "=? or "+ MediaStore.Images.Media.MIME_TYPE +" =? ", New string[]{" Image/jpeg "," Image/png "},mediastore.images.media.data)
        ;
        Mfilepath=new hashset<> ();
          while (Cursor.movetonext ()) {String path=cursor.getstring (Cursor.getcolumnindex (MediaStore.Images.Media.DATA));
          File Parentfile=new file (path). Getparentfile ();
          if (parentfile==null) continue;
          String Filepath=parentfile.getabsolutepath ();

          Filebean Filebean=null;
          if (Mfilepath.contains (FilePath)) {continue;
            }else {mfilepath.add (FilePath);
            Filebean=new Filebean ();
            Filebean.setmfilepath (FilePath);
          Filebean.setmfistimgpath (path);
           } if (Parentfile.list () ==null) continue;
          int Count=parentfile.list (new FilenameFilter () {   @Override public boolean accept (File dir, String filename) {if (Filename.endswith (". jpg") | | Filename.endswith (". png") | |
               Filename.endswith (". jpeg")) {return true;
             return false;
          }). length;
          Filebean.setmphotocount (count);

        Mfilebeans.add (Filebean);
        } cursor.close ();
        The Mfilepath is empty, the message is sent, and the Popupwindow is initialized.
        Mfilepath=null;

      Mhandler.sendemptymessage (0x110);

  }}.start ();

 }


}

Four, Popupwindow animation settings.

(1) write Pop-up and vanishing animation

① Pop-up Animation

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
<translate android:fromxdelta=" 0 "
  android:toxdelta=" 0 "
  android:fromydelta=" 100% "
  android:toydelta= "0"
  android:duration= "1000" ></translate>
</set>

② Vanishing Animation

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
  <translate android:fromxdelta=" 0 "
    android:toxdelta=" 0 "
    android:fromydelta=" 0 "
    android:toydelta= "100%"
    android:duration= "1000" ></translate>
</set>

(2) Set style and call style

① Set Style

Add in Style

<resources>

  <!--Base application theme.-->
  <style name= "Apptheme" parent= " Theme.AppCompat.Light ">
    <!--Customize your Theme here-->
  </style>
  <style name=" Listphotoselect ">
    <item name=" android:windowenteranimation "> @anim/animshow</item>
    < Item Name= "Android:windowexitanimation" > @anim/animdismiss</item>
  </style>
</resources >

② Call Style

Set animation style for Popupwindow

  Popupwindowlist.setanimationstyle (R.style.listphotoselect);

Note: The layout is simply no longer displayed.

Thank you for reading, I hope to help you, thank you for your support for this site!

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.