Android PopupWindow全屏

本文介绍了一个Android应用中使用PopupWindow的实例,通过遍历文件创建一个全屏PopupWindow,展示图片及文件信息。点击按钮弹出PopupWindow,并详细讲解了如何设置弹出与消失的动画效果,包括在style中定义动画并调用。
摘要由CSDN通过智能技术生成

这里写图片描述
很多应用中经常可以看到弹出这种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
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值