Android PopupWindow全屏详细介绍及实例代码
程序员文章站
2024-02-24 23:50:46
android popupwindow全屏
很多应用中经常可以看到弹出这种popupwindow的效果,做了一个小demo分享一下。demo的思路是通过遍...
android popupwindow全屏
很多应用中经常可以看到弹出这种popupwindow的效果,做了一个小demo分享一下。demo的思路是通过遍历文件,找到图片以及图片文件夹放置在popupwindow上面。点击按钮可以弹出这个popupwindow,这里为popupwindow设置了动画。
popupwindow全屏代码提要
受限需要自定义popupwindow,这里不看popupwindow里面要展示的内容,主要是设置popupwindow的高度。
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; //计算宽度和高度 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以外区域时popupwindow消失 if (event.getaction() == motionevent.action_outside) { dismiss(); } return false; } }); } /** * 设置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; //设置高度为全屏高度的70% mheight= (int) (metrics.heightpixels*0.7); } }
点击按钮弹出popupwindow
mbuttonshowpopup.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //点击时弹出popupwindow,屏幕变暗 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); }
一、filebean类保存信息
filebean如上图popupwindow所示,需要保存文件的路径,文件夹的名称,文件夹中文件的数量,文件夹中第一张图片的路径。基本全部为set、get方法
/** * this class is 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; } public void setmphotocount(int mphotocount) { this.mphotocount = mphotocount; } }
二、popupwidow界面设置
自定义popupwindow,
public class popupwindowlist extends popupwindow { private int mwidth; private int mheight; private view mcontentview; private list<filebean> mfilebeans; private listview mlistview; //观察者模式 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以外区域时popupwindow消失 if (event.getaction() == motionevent.action_outside) { dismiss(); } return false; } }); initlistview(context); initevent(); } private void initevent() { } //初始化popupwindow的listview private void initlistview(context context) { mylistviewadapter adapter=new mylistviewadapter(context,0,mfilebeans); mlistview= (listview) mcontentview.findviewbyid(r.id.listview); mlistview.setadapter(adapter); } /** * 设置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; mheight= (int) (metrics.heightpixels*0.7); } }
三、点击按钮弹出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(message msg) { super.handlemessage(msg); initpopupwindow(); } }; private void initpopupwindow() { popupwindowlist=new popupwindowlist(this,mfilebeans); popupwindowlist.setondismisslistener(new popupwindow.ondismisslistener() { @override public void ondismiss() { lighton(); } }); } //popupwindow消失时,使屏幕恢复正常 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) { //点击时弹出popupwindow,屏幕变暗 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); } //开启线程初始化数据,遍历文件找到所有图片文件,及其文件夹与路径进行保存。 private void initdata() { if(!environment.getexternalstoragestate().equals(environment.media_mounted)){ toastutils.showtoast(this,"当前sdcard不用使用"); } 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(); //将mfilepath置空,发送消息,初始化popupwindow。 mfilepath=null; mhandler.sendemptymessage(0x110); } }.start(); } }
四、popupwindow动画设置。
(1)编写弹出与消失动画
①弹出动画
<?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>
②消失动画
<?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)设置style与调用style
①设置style
在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>
②调用style
为popupwindow设置动画style
popupwindowlist.setanimationstyle(r.style.listphotoselect);
备注:布局很简单不再展示。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!