版权声明:本文为延成原创文章,转载请标明出处
/**
* 自定义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>