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
相关文章:
,交易猫的商品怎么发布到网站上去?
建站主机SSH密钥生成步骤及常见问题解答?
建站之星后台密码遗忘?如何快速找回?
Python lxml的etree和ElementTree有什么区别
如何在云主机上快速搭建多站点网站?
建站之星CMS建站配置指南:模板选择与SEO优化技巧
Bpmn 2.0的XML文件怎么画流程图
建站之星如何开启自定义404页面避免用户流失?
5种Android数据存储方式汇总
广州网站制作的公司,现在专门做网站的公司有没有哪几家是比较好的,性价比高,模板也多的?
建站之星伪静态规则如何正确配置?
完全自定义免费建站平台:主题模板在线生成一站式服务
我的世界制作壁纸网站下载,手机怎么换我的世界壁纸?
建站168自助建站系统:快速模板定制与SEO优化指南
香港服务器网站生成指南:免费资源整合与高速稳定配置方案
建站VPS选购需注意哪些关键参数?
深圳网站制作的公司有哪些,dido官方网站?
C++中的Pimpl idiom是什么,有什么好处?(隐藏实现)
如何快速辨别茅台真假?关键步骤解析
c++23 std::expected怎么用 c++优雅处理函数错误返回【详解】
重庆市网站制作公司,重庆招聘网站哪个好?
如何快速上传自定义模板至建站之星?
模具网站制作流程,如何找模具客户?
北京建设网站制作公司,北京古代建筑博物馆预约官网?
建站之星后台搭建步骤解析:模板选择与产品管理实操指南
官网网站制作腾讯审核要多久,联想路由器newifi官网
建站之星如何优化SEO以实现高效排名?
如何在IIS中新建站点并解决端口绑定冲突?
教育培训网站制作流程,请问edu教育网站的域名怎么申请?
如何快速生成橙子建站落地页链接?
保定网站制作方案定制,保定招聘的渠道有哪些?找工作的人一般都去哪里看招聘信息?
在线教育网站制作平台,山西立德教育官网?
如何通过西部数码建站助手快速创建专业网站?
C++中引用和指针有什么区别?(代码说明)
如何快速搭建二级域名独立网站?
宝塔建站助手安装配置与建站模板使用全流程解析
购物网站制作公司有哪些,哪个购物网站比较好?
高防服务器租用首荐平台,企业级优惠套餐快速部署
免费的流程图制作网站有哪些,2025年教师初级职称申报网上流程?
如何用虚拟主机快速搭建网站?详细步骤解析
如何在阿里云虚拟主机上快速搭建个人网站?
建站之星价格显示格式升级,你的预算足够吗?
免费网站制作模板下载,除了易企秀之外还有什么H5平台可以制作H5长页面,最好是免费的?
如何自定义建站之星网站的导航菜单样式?
建站之星官网登录失败?如何快速解决?
网站制作外包价格怎么算,招聘网站上写的“外包”是什么意思?
深圳 网站制作,深圳招聘网站哪个比较好一点啊?
家庭服务器如何搭建个人网站?
交易网站制作流程,我想开通一个网站,注册一个交易网址,需要那些手续?
整蛊网站制作软件,手机不停的收到各种网站的验证码短信,是手机病毒还是人为恶搞?有这种手机病毒吗?
*请认真填写需求信息,我们会在24小时内与您取得联系。