抽象出一个方便自定义的Basepopup类,更加方便的创建出一个popup以及动画效果
从1.8.6.1开始,将不再支持Jitpack
BasePopupWindowProxy
和PopupWindowProxy
权限收拢,不暴露放开- 优化
SimpleAnimUtil
,修改部分动画时间和插值器 - 增加
setOutsideTouchable()
方法,和setDismissWhenTouchOutside()
搭配使用有奇效哦 - 增加
BasePopupHelper
优化BasePopupWindow
代码可读性 - 动画方面修正
AnimaView.clearAnimation()
->Animation.cancel()
- 优化
showOnTop()
/showOnDown()
方法。。。虽然可能没什么人用 - 【未解决】
setBackPressEnable()
在M以上依然未能解决,除非我hook掉(但是代价太大),详情请看#33/问题描述:查看描述
- 抽取
PopupWindowProxy
->BasePopupWindowProxy
- 归类各种蛋疼的
showAsDropDown
适配->PopupCompatManager
- 修正部分命名和方法名以及注释名错误的问题
- 修复部分issue:#46
- 使用Jcenter代替JitPack
- 现在可以在onCreate里面showPopup啦~
- set方法返回
BasePopupWindow
,可以来个“伪链式”调用哈哈 - 针对诸位提出的setBackPress在6.0以上失效的问题,请查看这份MD文件(没错,暂时无法修复)
- 删除
setRelativeToAnchorView()
方法,该方法本身就没有什么用处。。。
添加依赖(请把{latestVersion}替换成上面的jitpack标签所示版本)
dependencies {
compile 'com.github.razerdp:BasePopup:{latestVersion}'
}
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>
Step 2:
新建一个类继承Basepopup
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
对应popup | 预览 |
---|---|
CommentPopup.java | |
ScalePopup.java | |
SlideFromBottomPopup.java | |
InputPopup.java | |
ListPopup.java | |
MenuPopup.java |
例子更新日志:
https://github.com/razerdp/BasePopup/blob/master/UpdateLog.md
http://www.jianshu.com/p/069f57e14a9c
Apache-2.0