1.x版本已经成为了过去,经过重构后更新到2.x版本。
2.x版本相比于1.x版本而言,主要做了以下工作:
- 背景层和内容层拆分,再也不用对xml做出背景蒙层+内容层的要求了(当然wrapContent也可以设置背景了~)
- 修改了方法名字,更加直观而且更加的方便
- 删掉冗余代码
- 增加QuickPopupBuilder,支持链式调用快速构建一个Popup,这样针对简单的Popup使用就不再需要继承BasePopup了
看到这里,是不是想快点使用上2.x呢?然而,由于2.x是属于破坏性的重构,因此与1.x差异还是挺大的,因此本文档旨在帮助开发者可以尽快的从1.x迁移到2.x。
在1.x版本中,我们要求布局必须包含背景蒙层,因此xml基本要求两层布局,具体如下:
<!--根布局,常用作蒙层(就是变暗的背景)-->
<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>2.x中,因为背景层和内容层已经拆分,因此我们对xml布局不再硬性要求2层了,比如Demo中的popup_comment 因此如果您在1.x中使用了两层的xml,您可以去掉根view的背景或者优化布局层次。
在1.x中,继承BasePopupWindow,您需要实现以下必须方法:
public class Test extends BasePopupWindow implements View.OnClickListener{
public DialogPopup(Activity context) {
super(context);
}
@Override
protected Animation initShowAnimation() {
return null;
}
@Override
protected View getClickToDismissView() {
return null;
}
@Override
public View onCreatePopupView() {
return null;
}
@Override
public View initAnimaView() {
return null;
}
}虽说大部分都可以返回null,但总归还是容易混淆。
因此在2.x我们进行了优化,现在继承BasePopupWindow只需要实现以下方法:
public class Test extends BasePopupWindow {
public Test(Context context) {
super(context);
}
//入场动画和退场动画已经不再强制实现了,一般建议实现
@Override
protected Animation onCreateShowAnimation() {
return null;
}
//入场动画和退场动画已经不再强制实现了,一般建议实现
@Override
protected Animation onCreateDismissAnimation() {
return null;
}
@Override
public View onCreateContentView() {
return null;
}
}咋一看只是减少了一个方法,然而实际上按功能区分我们只需要开发者返回三个点:
- 弹出动画(可选)
- 消失动画(可选)
- contentView
相比于1.x来说,简单清晰了不少。当然,默认情况下我们针对onCreateContentView返回的View进行动画,如果您的布局存在2层或者只想对其中某个View做动画,您可以重载onCreateAnimateView方法进行设定。
在平时的使用中,我们很多时候其实是对Popup做一个简单的使用,不同的仅仅是内容的不同,如果使用1.x,我们需要每个继承BasePopupWindow重复写着枯燥的代码。 因此在2.x中我们添加了一个新的快速构建工具:QuickPopupBuilder,该Builder提供了大多数常用的方法,帮助您快速构建出一个Popup以供使用(该Builder仍在优化丰富功能),示例代码如下:
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);以上就是本次帮助文档了,不排除继续优化的可能~