About BasePopup
masterPopupWindow. It provides a high degree of freedom and a rich API for easily implementing a wide variety of popup types within its framework.repository·master·Indexed 26 days ago
https://github.com/razerdp/basepopupAn Android library that provides a flexible, feature-rich wrapper around the standard system PopupWindow. It simplifies the creation of diverse popup window types through a rich API, supporting minimum API level 19+. Key features include background blur via RenderScript, automatic resizing (v3.2.0+), and customizable entry/exit animations.
PopupWindow. It provides a high degree of freedom and a rich API for easily implementing a wide variety of popup types within its framework.BasePopup is an Android library that wraps and improves upon the system PopupWindow. It provides a highly flexible framework with rich APIs, allowing developers to easily implement various types of popup windows.
Minimum API Level: 19+
If you intend to use the background blur feature (enabled via setBlurBackgroundEnable()), you must configure RenderScript in your build.gradle file. RenderScript requires a minimum API level of 17. If the device is lower than that, the library will fallback to fastblur.
defaultConfig {
renderscriptTargetApi 25
renderscriptSupportModeEnabled true
}BasePopup 3.0 introduces breaking changes to simplify the view initialization process. If you are upgrading from version 2.x, you must perform the following manual updates:
BaseLazyPopupWindow usage: The distinction between lazy-loading and normal popups is removed. All popups should now extend BasePopupWindow.onCreateConstructor calls: This method was specific to BaseLazyPopupWindow and is no longer required.onCreateContentView: This method has been removed. You must now use setContentView(@LayoutRes int layoutResID) or setContentView(final View view) within your constructor to initialize the content view.Recommendation: When using a layout resource, prefer setContentView(R.layout.your_layout) over manually inflating a view and passing it to setContentView(View view). This ensures the framework correctly parses XML configurations.
When submitting an issue, you must provide specific technical details to prevent the issue from being closed immediately. Ensure you include the following required fields:
Version 3.0 introduces significant breaking changes for users upgrading from the 2.x series. It is critical to review the official migration guide to ensure compatibility and avoid build errors.
[关于BasePopup 3.0的破坏性更新说明](./Update_3.0.md)To use BasePopup in your Android project, add the necessary repositories to your root build.gradle file and then add the dependency to your module-level build.gradle file.
Note: Switching between Release and Snapshot versions may cause build failures. If this occurs, perform a Clean Project in Android Studio.
// root gradle
allprojects {
repositories {
// release依赖仓库(4.1后as默认配置有)
mavenCentral()
// snapshot仓库(如果需要snapshot依赖,请配置该maven)
maven { url 'https://s01.oss.sonatype.org/content/repositories/snapshots' }
}
}
// project dependencies
dependencies {
implementation 'io.github.razerdp:BasePopup:3.2.1'
// for snapshot
// implementation 'io.github.razerdp:BasePopup:3.2.1-SNAPSHOT'
}To create a custom popup, follow these steps:
BasePopupWindow.initAnimaView(): Return the view that will host the entry/exit animations.initShowAnimation(): Return an Animation object for the entry effect.onCreatePopupView(): Initialize the popup UI. It is recommended to use createPopupById(int layoutResId).getClickToDismissView(): Return the view that, when clicked, will dismiss the popup (e.g., a background mask).showPopupWindow().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 initShowAnimation() {
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 View getClickToDismissView() {
return getPopupWindowView();
}
@Override
public View onCreatePopupView() {
return createPopupById(R.layout.popup_dialog);
}
@Override
public View initAnimaView() {
return findViewById(R.id.popup_anima);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ok:
Toast.makeText(getContext(), "click the ok button", Toast.LENGTH_SHORT).show();
break;
case R.id.cancel:
Toast.makeText(getContext(), "click the cancel button", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
// Usage:
DialogPopup popup = new DialogPopup(context);
popup.showPopupWindow();To use the stable Release version of BasePopup, add the following dependency to your dependencies block in your Gradle file. Replace {latestVersion} with the version number found on the repository's release page (e.g., 1.9.4).
Note: The Candy version is experimental and contains new features but may be unstable; it is not recommended for commercial use.
In version 3.0, the timing of setContentView is under your control. You should call setContentView in your constructor to ensure that view inflation happens immediately. This allows you to pass parameters through the constructor and access them safely within onViewCreated or via builder-style methods without encountering NullPointerExceptions (NPE).
public class DemoPopup extends BasePopupWindow {
@BindView(R.id.tv_desc)
public TextView mTvDesc;
int a;
public DemoPopup(Context context, int a) {
super(context);
// The value passed by the constructor is assigned here
this.a = a;
// Manually set the content view in the constructor
setContentView(R.layout.popup_demo);
}
@Override
public void onViewCreated(View contentView) {
ButterKnifeUtil.bind(this, contentView);
// Parameters passed to the constructor are now safely accessible
mTvDesc.setText(String.valueOf(a));
}
// Builder-style method is now safe because inflation happened in the constructor
public DemoPopup setText(CharSequence text) {
mTvDesc.setText(text);
return this;
}
}