Giter Club home page Giter Club logo

basepopup's Introduction

BasePopup

抽象出一个方便自定义的Basepopup类,更加方便的创建出一个popup以及动画效果

Release Candy License Api Author
Download Download license Api Author

注意事项

**请务必查看更新日志和例子预览,里面会详细解释每个版本增加或修复的功能**

**请注意引用版本的问题,Release版本是稳定版,可商用。**

**Candy不稳定(且更新很频繁),但包含着新功能或者新的优化,不建议商用。** 

更新日志

例子预览

依赖

Release Candy
Download Download

添加依赖(请把{latestVersion}替换成上面的Jcenter标签所示版本**【candy版本不一定稳定,包含有新功能或者新的修复,完善后将会发布其release版】**)

	dependencies {
	        compile 'com.github.razerdp:BasePopup:{latestVersion}'
	        
	        //candy版本,不稳定,但会带有新功能
	        //compile 'com.github.razerdp:BasePopup_Candy:{latestVersion}'
	}

使用方法

ps:从1.9.0-alpha开始支持背景模糊(只需要一个方法:setBlurBackgroundEnable()

RenderScript最低支持api 17(更低的情况将会使用fastblur),您需要在gradle配置一下代码

defaultConfig {
        renderscriptTargetApi 25
        renderscriptSupportModeEnabled true
    }
  • Step 1:

像您平时定制activity布局文件一样定制您的popup布局(请注意,展示动画的那个view必须是popupview的子view)

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"
    android:background="#8f000000">
    
    <!--播放动画的内容,可以认为是popup的主要内容布局-->
    <RelativeLayout
        android:id="@+id/popup_anima"
        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>

image

  • Step 2:

新建一个类继承BasePopupWindow

  • Step 3:

实现必要的几个方法:

initShowAnimation():初始化一个进入动画,该动画将会用到initAnimaView()返回的view

onCreatePopupView():初始化您的popupwindow界面,建议直接使用createPopupById(),不能返回空值

getClickToDismissView():如果有需要的话,可以使用这个方法返回一个点击dismiss popupwindow的view(也许是遮罩层也许是某个view,这个随您喜欢)

例如

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

    }
}
  • Step 4:

把您刚才实现的popup给new出来并调用show方法

例如

    DialogPopup popup = new DialogPopup(context);
    popup.showPopupWindow();

方法介绍:

请看wiki(陆续完善中)

Link👉WIKI

打赏(看在我那么努力维护的份上。。。给个零食呗~)

微信 支付宝

更新日志(历史更新)

  • 1.9.2(未发布)

    • 修复HackDecorView针对PopupWindow高度问题
    • 增加setBlurBackgroundEnable()模糊设置回调,允许自定义模糊操作
    • 暂时不发布1.9.2,等待其他问题一同收集。
  • 1.9.1

    • 修复可能出现的死循环问题以及去掉manifest文件冲突的问题
    • 部分方法名字修改,默认关闭 Log,如果您需要打印内部调试日志,请使用该方法:BasePopupWindow.debugLog(true)
    • 增加位移动画(百分比传值),位移动画名字修正:getTranslateAnimation() -> getTranslateVerticalAnimation()
    • 模糊背景功能已经开放,针对单个View的模糊方法开放
    • 模糊背景允许子线程执行,默认主线程执行
    • gradle请在defaultConfig下添加两句:
      • renderscriptTargetApi 25
      • enderscriptSupportModeEnabled true
    • 发布1.9.1,其余bug修复
  • 1.9.0-alpha2(candy)

    • 修复了可能导致的动画duration为-1而崩溃的问题
  • 1.9.0-alpha(candy)

    • 增加了一个window用于模糊层,增加模糊功能(请注意,并不一定稳定,仍然迭代中。。慎用!!!)
      • 如果您需要模糊功能,仅仅需要调用一个方法:setBlurBackgroundEnable()
      • gradle请在defaultConfig下添加两句:
        • renderscriptTargetApi 25
        • enderscriptSupportModeEnabled true
  • 1.8.9-beta2

    • 增加演示demo:DismissControlPopupFrag
    • 增加两个方法用于touchEvent监听:onTouchEvent()&onOutSideTouch()
    • HackPopupDecorView继承ViewGroup而非FrameLayout,以解决PopupWindow的decorView.getLayoutParams()无法强转为WindowManager.LayoutParams的异常
    • 其余问题暂时没发现
  • 1.8.8(1.8.9)

【重大修复,不一定稳定,但因为解决了某个大问题,因此强烈建议升级到该版本,希望您可以尽量提交问题】

  • BasePopupWindowProxyPopupWindowProxy权限收拢,不暴露放开

  • 优化SimpleAnimUtil,修改部分动画时间和插值器

  • 增加setOutsideTouchable()方法,和setDismissWhenTouchOutside()搭配使用有奇效哦

  • 增加BasePopupHelper优化BasePopupWindow代码可读性

  • 动画方面修正AnimaView.clearAnimation()->Animation.cancel()

  • 优化showOnTop()/showOnDown()方法。。。虽然可能没什么人用

  • 1.8.8版本因为一些问题而去除#50,替换为1.8.9

  • 【已解决】setBackPressEnable()在M以上已经可以自行决定是否允许返回键dismiss了,同时开放了keyEvent

    • 解决方案:1.8.9 解决方案
    • issue:#33
    • BasePopupWindow增加两个方法用于keyEvent的监听:onDispatchKeyEvent()&onBackPressed()
    • 感谢诸位热烈的讨论~
  • 部分方法名更改

    • setOutsideTouchable()->setInterceptTouchEvent(),该方法会影响焦点问题,即便是解决了backPress若这个方法设置为false,依然不会响应backpress
  • 1.8.7

    • 抽取PopupWindowProxy->BasePopupWindowProxy
    • 归类各种蛋疼的showAsDropDown适配->PopupCompatManager
    • 修正部分命名和方法名以及注释名错误的问题
      • 感谢简书小伙伴的评论,否则我还真发现不了。。。
      • 评论地址:点我
      • 根据简书id,只能猜测他的github id:Chenley,如果您见到并发现我这个猜测是错的,请及时联系我-V-
      • 非常感谢你们的issue
    • 修复部分issue:#46

一些例子

对应popup 预览
BlurSlideFromBottomPopup.java image
CommentPopup.java image
ScalePopup.java image
SlideFromBottomPopup.java image
InputPopup.java image
ListPopup.java image
MenuPopup.java image

代码解析

(很旧的文章了,考虑迟点重新编写)

http://www.jianshu.com/p/069f57e14a9c

License

Apache-2.0

basepopup's People

Contributors

razerdp avatar 430public avatar dadengpao avatar bryant1410 avatar hshare avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.