这是一个快速实现PopupWindow的基类,本基类易于扩展,并且几乎没有使用限制,便于您快速实现各种各样的PopupWindow。
| Release | Candy | License | Api | Author |
|---|---|---|---|---|
Android P已经适配,感谢@Guolei1130收集的方法。
文章地址:android_p_no_sdkapi_support
本库一开始采用360的方法,但不得不走Native,为了个Popup不得不引入so感觉很不值得,在看到这篇文章后,才想起UnSafe类
本库采用方法5。
如果以后UnSafe类移除掉的话,再考虑Native方法。
最后再一次感谢大牛提供的方法~
请务必查看更新日志和例子预览,里面会详细解释每个版本增加或修复的功能
请注意引用版本的问题,Release版本是稳定版,可商用。
Candy不稳定(且更新很频繁),但包含着新功能或者新的优化,不建议商用。
| Release | Candy |
|---|---|
添加依赖(请把{latestVersion}替换成上面的Jcenter标签所示版本
【candy版本不一定稳定,包含有新功能或者新的修复,完善后将会发布其release版】
dependencies { implementation 'com.github.razerdp:BasePopup:{latestVersion}' //candy版本,不稳定,但会带有新功能 //implementation 'com.github.razerdp:BasePopup_Candy:{latestVersion}' }- Step 1:
像您平时定制activity布局文件一样定制您的popup布局
etc.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_dialog" android:layout_centerInParent="true" android:layout_margin="25dp"> <... many views> </RelativeLayout> </RelativeLayout>- Step 2:
新建一个类继承BasePopupWindow
- Step 3:
实现必要的几个方法:
该方法从2.0.6开始不再抽象强制实现,但建议实现入场和退场动画 onCreateShowAnimation()/onCreateDismissAnimation():初始化一个显示/退出动画,该动画将会用到onCreatePopupView()所返回的view,可以为空。
onCreatePopupView():初始化您的popupwindow界面,建议直接使用createPopupById()
例如
public class DialogPopup extends BasePopupWindow implements View.OnClickListener{ private TextView ok; private TextView cancel; public DialogPopup(Activity context) { super(context); ok= (TextView) findViewById(R.id.ok); cancel= (TextView) findViewById(R.id.cancel); setViewClickListener(this,ok,cancel); } @Override protected Animation onCreateShowAnimation() { AnimationSet set=new AnimationSet(false); Animation shakeAnima=new RotateAnimation(0,15,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); shakeAnima.setInterpolator(new CycleInterpolator(5)); shakeAnima.setDuration(400); set.addAnimation(getDefaultAlphaAnimation()); set.addAnimation(shakeAnima); return set; } @Override protected Animation onCreateDismissAnimation() { return null; } @Override public View onCreateContentView() { return createPopupById(R.layout.popup_dialog); } @Override public void onClick(View v) { //... click event } }- Step 4:
把您刚才实现的popup给new出来并调用show方法
例如
DialogPopup popup = new DialogPopup(context); popup.showPopupWindow();如果您并不需要很详细的定义一个PopupWindow,您也可以选择QuickPopupBuilder采取链式写法快速编写出一个Popup以使用。
QuickPopupBuilder.with(getContext()) .contentView(R.layout.popup_menu_small) .wrapContentMode() .config(new QuickPopupConfig() .withShowAnimation(enterAnimation) .withDismissAnimation(dismissAnimation) .offsetX(offsetX, offsetRatioOfPopupWidth) .offsetY(offsetY, offsetRatioOfPopupHeight) .blurBackground(true, new BasePopupWindow.OnBlurOptionInitListener() { @Override public void onCreateBlurOption(PopupBlurOption option) { option.setBlurRadius(6) .setBlurPreScaleRatio(0.9f); } }) .withClick(R.id.tx_1, new View.OnClickListener() { @Override public void onClick(View v) { ToastUtils.ToastMessage(getContext(), "tx1"); } })) .show(v);ps:从1.9.0-alpha开始支持背景模糊(只需要一个方法:setBlurBackgroundEnable())
RenderScript最低支持api 17(更低的情况将会使用fastblur),您需要在gradle配置一下代码
defaultConfig { renderscriptTargetApi 25 renderscriptSupportModeEnabled true }请看wiki(陆续完善中)
Link👉WIKI
因为目前还有朋友圈项目,建立了一个交流群,出于懒得管理那么多,所以如果有想法或者优化建议或者其他问题,欢迎加入“朋友圈交流群”
| 微信 | 支付宝 |
|---|---|
![]() | ![]() |
更新日志(历史更新)
-
【Release】2.0.8(2018/10/29)
- fixed #93
- 修复部分崩溃问题,发布release
-
【Candy】2.0.8-alpha3(2018/10/25)
-
【Candy】2.0.8-alpha2(2018/10/19)
- 修复QuickPopupBuilder的click事件无响应问题,增加background方法
- 修复设置background(0)时无法找到资源而崩溃的问题
-
【Release】2.0.7(2018/10/15)
- 绕开Android P的非公开api方法反射
- 思路参考&&感谢android_p_no_sdkapi_support
- 发布2.0.7 release
- 绕开Android P的非公开api方法反射
-
【Release】2.0.6(2018/10/09)
- 不再抽象强制实现入场和退场动画
- 针对自动弹出输入法的Popup,在dismiss()中默认关闭输入法
| 对应popup | 预览 |
|---|---|
| LocatePopupFrag.java | ![]() |
| BlurSlideFromBottomPopup.java | ![]() |
| CommentPopup.java | ![]() |
| ScalePopup.java | ![]() |
| SlideFromBottomPopup.java | ![]() |
| InputPopup.java | ![]() |
| ListPopup.java | ![]() |
| MenuPopup.java | ![]() |
Apache-2.0











