很多应用中经常可以看到弹出这种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