简单自定义PopupWindow

版权声明:本文为延成原创文章,转载请标明出处

/**
 * 自定义PopupWindow
 */

public class AutoAttentionPopupWindow extends PopupWindow {

    private static final String TAG = AutoAttentionPopupWindow.class.getSimpleName();
    private View mMenuView;
    private Context mContext;
    private View.OnClickListener mOnClick;
    private SimpleDraweeView mSdv_head_icon;

    public AutoAttentionPopupWindow(Context context, View.OnClickListener onClick) {
        super(context);
        this.mContext = context;
        this.mOnClick = onClick;
        init();
    }

    private void init() {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.window_auto_attention, null);
        mSdv_head_icon = (SimpleDraweeView) mMenuView.findViewById(R.id.sdv_head_icon);

        ImageView iv_close = (ImageView) mMenuView.findViewById(R.id.iv_close);
        TextView tv_attention = (TextView) mMenuView.findViewById(R.id.tv_attention);

        tv_attention.setOnClickListener(mOnClick);
        iv_close.setOnClickListener(mOnClick);

        this.setContentView(mMenuView);
        this.setBackgroundDrawable(mContext.getResources().getDrawable(R.color.transparent));
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setAnimationStyle(R.style.popwindow_anim_right_style);
        this.setClippingEnabled(false);//全屏显示
    }

    public void show(View view,String icon) {

        FrescoUtils.showUrl(mSdv_head_icon, icon);
        this.showAtLocation(view, Gravity.BOTTOM, 0, 0);
        showBackground(mContext);
    }
    
	@Override
    public void dismiss() {
        super.dismiss();
        cancelBackground(mContext);
    }

 	public static void showBackground(Activity activity) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = 0.5f;
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        activity.getWindow().setAttributes(lp);
    }

    public static void cancelBackground(Activity activity) {
        WindowManager.LayoutParams lp1 = activity.getWindow().getAttributes();
        lp1.alpha = 1f;
        activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        activity.getWindow().setAttributes(lp1);
    }
}
 <style name="popwindow_anim_right_style">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowEnterAnimation">@anim/anim_enter_right</item>
        <item name="android:windowExitAnimation">@anim/anim_exit_right</item>
    </style>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromXDelta="100%"
        android:toXDelta="0"/>
</set>

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:toXDelta="100%"/>
</set>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值