BasePopup

repository·master·Indexed 26 days ago

https://github.com/razerdp/basepopup

An 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.

Tokens
2.9K
Snippets
5
Records
15
Agent score
90%

What's inside BasePopup

  1. BasePopup Overview

    master

    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+

  2. Configure RenderScript for Background Blur

    master

    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
    }
  3. Migrate from BasePopup 2.x to 3.0

    master

    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:

    1. Remove BaseLazyPopupWindow usage: The distinction between lazy-loading and normal popups is removed. All popups should now extend BasePopupWindow.
    2. Remove onCreateConstructor calls: This method was specific to BaseLazyPopupWindow and is no longer required.
    3. Replace 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.

  4. Submit a bug report or issue

    master

    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:

    • System version (Required)
    • Library version (Required)
    • Problem code or screenshot (Optional)
    • Error reporting information (Optional)
    • Problem description/Reproduction steps (Required)
  5. Migrate from BasePopup 2.x to 3.x

    master

    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)
  6. Install BasePopup via Gradle

    master

    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'
    }
  7. Implement a custom BasePopupWindow

    master

    To create a custom popup, follow these steps:

    1. Define Layout: Create an XML layout. Ensure the view intended for animations is a child of the main popup view.
    2. Extend BasePopupWindow: Create a new class that extends BasePopupWindow.
    3. Override Required Methods:
      • 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).
    4. Show the Popup: Instantiate your class and call 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();
  8. Install BasePopup 1.x via Gradle

    master

    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.

  9. Implement BasePopupWindow in version 3.0

    master

    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;
        }
    }