PopupWindow 弹窗基本创建和显示(处理了外区域点击事件)

PopupWindow 弹窗基本创建和显示

效果图:

在这里插入图片描述

/**
 * 弹窗
 */
private PopupWindow mPopWindow;
private View mPopWindowContentView;

// PopupWindow的视图容器 View
mPopWindowContentView = LayoutInflater.from(view.getContext()).inflate(R.layout.layout_more_operations, null);

// PopupWindow布局中有还有其他控件使用容器View去FBI
ll_operation_scan =mPopWindowContentView.findViewById(R.id.ll_operation_scan);
ll_operation_identify_song = mPopWindowContentView.findViewById(R.id.ll_operation_identify_songs);
ll_operation_todo = mPopWindowContentView.findViewById(R.id.ll_operation_todo);

 /**
  * 显示弹窗
  * @param context
  * @param targetView    指定在哪个View弹出
  * @param contentView   弹窗的布局 view,已经经过 inflate 讲布局绘制成 view
  */
public void showPopupWindow(Context context, View targetView, View contentView){
    // 显示弹窗布局
    mPopWindow = new PopupWindow(contentView, DipAndPxUtils.dip2px(context, 130), DipAndPxUtils.dip2px(context, 150));
    // 设置背景色
    //mPopWindow.setBackgroundDrawable(mActivity.getDrawable(R.drawable.shape_operation));
    // 设置焦点
    mPopWindow.setFocusable(true);
    // 设置可以触摸弹窗以外的地方
    mPopWindow.setOutsideTouchable(false);
    // 点击其他区域和物理按键时,隐藏window,但是隐藏的同时需要将按钮还原成原状态(执行还原动画)
    // 所有得设置一下 触摸事件拦截器 setTouchInterceptor
     mPopWindow.setTouchInterceptor(new View.OnTouchListener() {
     @Override
     public boolean onTouch(View v, MotionEvent event) {
         final int x = (int)event.getX();
         final int y = (int)event.getY();
         if(event.getAction() == MotionEvent.ACTION_DOWN && ((x < 0) || (x >= mPopWindowContentView.getWidth()) || (y < 0) || (y >= mPopWindowContentView.getHeight()))){
                 mPopWindow.dismiss();
                 img_more_operation.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.anim_rotate_operation_close));
                 return true;
              }
            
 return false;
            }
        });
        // 给弹窗设置 弹出和收起动画
        mPopWindow.setAnimationStyle(R.style.popupwindow_anim);
        // 更新弹出窗口的尺寸。调用此函数还将更新窗口,使其具有当前弹出状态
        mPopWindow.update();
        // 在指定位置弹出,右偏300px,下偏20px
        mPopWindow.showAsDropDown(targetView, -300, 20);
    }

DipAndPxUtils工具类:

public class DipAndPxUtils {
    /**
     * 根据手机的分辨率从 dip 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dip
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}
给弹窗设置 弹出和收起动画:

收起:anim_window_close.xml

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200"
    android:fillAfter="true"
    >
    <alpha
        android:fromAlpha="1"
        android:toAlpha="0" />

    <scale
        android:pivotX="100%"
        android:pivotY="0"
        android:fromYScale="1"
        android:toYScale="0"
        android:fromXScale="1"
        android:toXScale="0"
        />
</set>

弹出:anim_window_open.xml

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200"
    android:fillAfter="true"
    >
    <alpha
        android:fromAlpha="0"
        android:toAlpha="1" />

    <scale
        android:pivotX="100%"
        android:pivotY="0"
        android:fromYScale="0"
        android:toYScale="1"
        android:fromXScale="0"
        android:toXScale="1"
        />
</set>

styles.xml :

<!-- PopupWindow的弹出和收起动画style,通过.setAnimationStyle()设给PopupWindow-->
<style name="popupwindow_anim">
    <item name="android:windowEnterAnimation">@anim/anim_window_open</item>
    <item name="android:windowExitAnimation">@anim/anim_window_close</item>
</style>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值