Android PopupWindow全屏详细介绍及实例代码
发布时间 - 2026-01-10 21:53:20 点击率:次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
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Linux系统运维自动化项目教程_Ansible批量管理实战
Laravel如何处理和验证JSON类型的数据库字段
Python进程池调度策略_任务分发说明【指导】
Laravel如何处理跨站请求伪造(CSRF)保护_Laravel表单安全机制与令牌校验
魔方云NAT建站如何实现端口转发?
Python自动化办公教程_ExcelWordPDF批量处理案例
JavaScript如何实现继承_有哪些常用方法
如何用wdcp快速搭建高效网站?
如何快速打造个性化非模板自助建站?
HTML5段落标签p和br怎么选_文本排版常用标签对比【解答】
如何在云指建站中生成FTP站点?
Laravel如何实现事件和监听器?(Event & Listener实战)
Laravel怎么实现观察者模式Observer_Laravel模型事件监听与解耦开发【指南】
Laravel如何发送系统通知?(Notification渠道示例)
南京网站制作费用,南京远驱官方网站?
高防网站服务器:DDoS防御与BGP线路的AI智能防护方案
消息称 OpenAI 正研发的神秘硬件设备或为智能笔,富士康代工
html5audio标签播放结束怎么触发事件_onended回调方法【教程】
Laravel如何使用Scope本地作用域_Laravel模型常用查询逻辑封装技巧【手册】
Laravel如何正确地在控制器和模型之间分配逻辑_Laravel代码职责分离与架构建议
Laravel如何实现API资源集合?(Resource Collection教程)
如何在腾讯云服务器快速搭建个人网站?
专业企业网站设计制作公司,如何理解商贸企业的统一配送和分销网络建设?
中山网站推广排名,中山信息港登录入口?
网站制作大概要多少钱一个,做一个平台网站大概多少钱?
javascript中的数组方法有哪些_如何利用数组方法简化数据处理
三星、SK海力士获美批准:可向中国出口芯片制造设备
详解jQuery中基本的动画方法
logo在线制作免费网站在线制作好吗,DW网页制作时,如何在网页标题前加上logo?
用v-html解决Vue.js渲染中html标签不被解析的问题
移动端脚本框架Hammer.js
怎么制作网站设计模板图片,有电商商品详情页面的免费模板素材网站推荐吗?
laravel服务容器和依赖注入怎么理解_laravel服务容器与依赖注入解析
Laravel怎么返回JSON格式数据_Laravel API资源Response响应格式化【技巧】
如何在IIS中新建站点并配置端口与IP地址?
百度输入法ai组件怎么删除 百度输入法ai组件移除工具
JavaScript中的标签模板是什么_它如何扩展字符串功能
INTERNET浏览器怎样恢复关闭标签页_INTERNET浏览器标签恢复快捷键与方法【指南】
C#如何调用原生C++ COM对象详解
Python企业级消息系统教程_KafkaRabbitMQ高并发应用
使用PHP下载CSS文件中的所有图片【几行代码即可实现】
如何快速搭建虚拟主机网站?新手必看指南
做企业网站制作流程,企业网站制作基本流程有哪些?
如何快速搭建自助建站会员专属系统?
黑客如何利用漏洞与弱口令入侵网站服务器?
Laravel如何使用Service Container和依赖注入?(代码示例)
专业商城网站制作公司有哪些,pi商城官网是哪个?
ChatGPT怎么生成Excel公式_ChatGPT公式生成方法【指南】
深圳防火门网站制作公司,深圳中天明防火门怎么编码?
桂林网站制作公司有哪些,桂林马拉松怎么报名?

