全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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

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

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

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


# Android  # PopupWindow  # 详解  # PopupWindow全屏  # Android 应用的全屏和非全屏实现代码  # Android 实现全屏显示的几种方法整理  # Android UI体验之全屏沉浸式透明状态栏样式  # Android 全屏无标题栏的三种实现方法  # Android编程之界面实现全屏显示的方法(2种方法)  # Android编程实现WebView自适应全屏方法小结  # Android编程使WebView支持HTML5 Video全屏播放的解决方法  # Android全屏设置的方法总结  # 弹出  # 全屏  # 遍历  # 自定义  # 变暗  # 夹中  # 希望能  # 很简单  # 可以看到  # 不看  # 所示  # 谢谢大家  # 第一张  # 恢复正常  # 主要是  # 发送消息  # 保存文件  # 如上图  # ListphotoSelect  # showAsDropDown 


相关文章: 常州自助建站工具推荐:低成本搭建与模板选择技巧  如何在IIS服务器上快速部署高效网站?  中山网站制作网页,中山新生登记系统登记流程?  如何用西部建站助手快速创建专业网站?  TestNG的testng.xml配置文件怎么写  建设网站制作价格,怎样建立自己的公司网站?  如何制作新型网站程序文件,新型止水鱼鳞网要拆除吗?  清单制作人网站有哪些,近日“兴风作浪的姑奶奶”引起很多人的关注这是什么事情?  制作假网页,招聘网的薪资待遇,会有靠谱的吗?一面试又各种折扣?  安云自助建站系统如何快速提升SEO排名?  如何通过服务器快速搭建网站?完整步骤解析  教育培训网站制作流程,请问edu教育网站的域名怎么申请?  IOS倒计时设置UIButton标题title的抖动问题  建站主机是否等同于虚拟主机?  如何在Windows环境下新建FTP站点并设置权限?  C#如何使用XPathNavigator高效查询XML  如何高效利用亚马逊云主机搭建企业网站?  怀化网站制作公司,怀化新生儿上户网上办理流程?  网站制作服务平台,有什么网站可以发布本地服务信息?  实例解析Array和String方法  内网网站制作软件,内网的网站如何发布到外网?  非常酷的网站设计制作软件,酷培ai教育官方网站?  小说建站VPS选用指南:性能对比、配置优化与建站方案解析  已有域名和空间如何快速搭建网站?  建站之星代理如何优化在线客服效率?  建站之星安装模板失败:服务器环境不兼容?  建站之星安装失败:服务器环境不兼容?  在线制作视频的网站有哪些,电脑如何制作视频短片?  广德云建站网站建设方案与建站流程优化指南  如何在万网主机上快速搭建网站?  深圳企业网站制作设计,在深圳如何网上全流程注册公司?  陕西网站制作公司有哪些,陕西凌云电器有限公司官网?  已有域名如何快速搭建专属网站?  网站制作公司哪里好做,成都网站制作公司哪家做得比较好,更正规?  详解jQuery中基本的动画方法  高性能网站服务器配置指南:安全稳定与高效建站核心方案  怎么将XML数据可视化 D3.js加载XML  高防服务器租用如何选择配置与防御等级?  如何通过宝塔面板实现本地网站访问?  网站制作公司广州有几家,广州尚艺美发学校网站是多少?  安徽网站建设与外贸建站服务专业定制方案  制作无缝贴图网站有哪些,3dmax无缝贴图怎么调?  建站主机默认首页配置指南:核心功能与访问路径优化  建站之星免费版是否永久可用?  高端网站建设与定制开发一站式解决方案 中企动力  微网站制作教程,我微信里的网站怎么才能复制到浏览器里?  天河区网站制作公司,广州天河区如何办理身份证?需要什么资料有预约的网站吗?  ,如何利用word制作宣传手册?  XML的“混合内容”是什么 怎么用DTD或XSD定义  制作宣传网站的软件,小红书可以宣传网站吗? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。