Android PopupWindow全屏详细介绍及实例代码

Android PopupWindow全屏详细介绍及实例代码,第1张

概述 AndroidPopupWindow全屏很多应用中经常可以看到d出这种PopupWindow的效果,做了一个小demo分享一下。demo的思路是通过遍历文件,找到图片以及图片文件夹放置在PopupWindow上面。点击按钮可以d出这个PopupW

 AndroID PopupWindow全屏

很多应用中经常可以看到d出这种PopupWindow的效果,做了一个小demo分享一下。demo的思路是通过遍历文件,找到图片以及图片文件夹放置在PopupWindow上面。点击按钮可以d出这个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);  }}

点击按钮d出PopupWindow

 mbuttonShowPopup.setonClickListener(new VIEw.OnClickListener() {      @OverrIDe      public voID onClick(VIEw v) {        //点击时d出PopupWindow,屏幕变暗        popupwindowList.setAnimationStyle(R.style.ListphotoSelect);        popupwindowList.showAsDropDown(mbuttonShowPopup,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,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);  }}

三、点击按钮d出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) {        //点击时d出PopupWindow,屏幕变暗        popupwindowList.setAnimationStyle(R.style.ListphotoSelect);        popupwindowList.showAsDropDown(mbuttonShowPopup,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)编写d出与消失动画

①d出动画

<?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);

备注:布局很简单不再展示。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

总结

以上是内存溢出为你收集整理的Android PopupWindow全屏详细介绍及实例代码全部内容,希望文章能够帮你解决Android PopupWindow全屏详细介绍及实例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1147495.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存