组件介绍
封装了不同样式的Dialog和动画,在需要使用的时候直接new出来,经过简单的配置就可以使用,可扩展性强。
地址:https://github.com/H07000223/FlycoDialog_Master
NormalDialog(一)
代码部分:
final NormalDialog dialog = new NormalDialog(this);
dialog.content("是否确定退出程序")
.showAnim(new SlideLeftEnter())//从左边出来的动画
.dismissAnim(new SlideRightExit())//从右边离开的动画
.show();
dialog.setOnBtnClickL(new OnBtnClickL() {
@Override
public void onBtnClick() {
Log.i("=======", "点击取消");
dialog.dismiss();
}
}, new OnBtnClickL() {
@Override
public void onBtnClick() {
Log.i("=======", "点击确定");
dialog.dismiss();
}
});
NormalDialog(二)
代码部分:
final NormalDialog dialog = new NormalDialog(mContext);
dialog.content("为保证咖啡豆的新鲜度和咖啡的香味,并配以特有的传统烘焙和手工冲。")//
.style(NormalDialog.STYLE_TWO)//
.titleTextSize(23)//设置title字体大小
.showAnim(new BounceTopEnter())//顶部弹出动画
.dismissAnim(new SlideBottomExit())//底部退出动画
.show();
//设置点击时间
dialog.setOnBtnClickL(
new OnBtnClickL() {
@Override
public void onBtnClick() {
T.showShort(mContext, "left");
dialog.dismiss();
}
},
new OnBtnClickL() {
@Override
public void onBtnClick() {
T.showShort(mContext, "right");
dialog.dismiss();
}
});
NormalDialogCustomAttr
自定义属性的Dialog
代码部分:
final NormalDialog dialog = new NormalDialog(mContext);
dialog.isTitleShow(false)//是否显示title
.bgColor(Color.parseColor("#383838"))//设置背景颜色
.cornerRadius(5)//设置圆角大小
.content("是否确定退出程序?")//
.contentGravity(Gravity.CENTER)
.contentTextColor(Color.parseColor("#ffffff"))//
.dividerColor(Color.parseColor("#222222"))//
.btnTextSize(15.5f, 15.5f)//
.btnTextColor(Color.parseColor("#ffffff"), Color.parseColor("#ffffff"))//
.btnPressColor(Color.parseColor("#2B2B2B"))//设置button按下时的颜色
.widthScale(0.85f)//设置对话框宽度,占屏幕宽的比例0-1
.showAnim(new SlideLeftEnter())//从左边出来的动画
.dismissAnim(new SlideRightExit())//从右边离开的动画
.show();
**NormalListDialog
圆角ListViewDialog
代码部分:
final NormalListDialog dialog = new NormalListDialog(mContext, mMenuItems);
dialog.title("请选择")//
.showAnim(mBasIn)//
.dismissAnim(mBasOut)//
.show();
dialog.setOnOperItemClickL(new OnOperItemClickL() {
@Override
public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
T.showShort(mContext, mMenuItems.get(position).mOperName);
dialog.dismiss();
}
});
BottomDialog和TopDialog
从底部/顶部出现的Dialog
代码部分:
final BottomDialog dialog = new BottomDialog(mContext, viewNeedAnim);//viewNeedAnim是需要执行动画的view
dialog.showAnim(mBasIn)//
.show();//
final TopDialog dialog = new TopDialog(mContext, viewNeedAnim);
dialog.showAnim(mBasIn)//
.show();//
扩展
Dialog扩展
step1:继承BaseDialog(or BottomBaseDialog or TopBaseDialog)
step2:重写onCreateView,return用来填充Dialog的View
step3:重写setUIBeforeShow,设置界面数据和逻辑
public class CustomBaseDialog extends BaseDialog<CustomBaseDialog> {
private TextView tv_cancel;
private TextView tv_exit;
public CustomBaseDialog(Context context) {
super(context);
}
@Override
public View onCreateView() {
widthScale(0.85f);////设置对话框宽度,占屏幕宽的比例0-1
showAnim(new Swing());
View inflate = View.inflate(context, R.layout.dialog_custom_base, null);
tv_cancel = ViewFindUtils.find(inflate, R.id.tv_cancel);
tv_exit = ViewFindUtils.find(inflate, R.id.tv_exit);
inflate.setBackgroundDrawable(
CornerUtils.cornerDrawable(Color.parseColor("#ffffff"), dp2px(5)));
return inflate;
}
@Override
public boolean setUiBeforShow() {
tv_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
tv_exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
return false;
}
}
动画扩展
继承BaseAnimatorSet,重写setAnimation(View view),该方法返回的view就是
需要执行动画的view
public class FadeEnter extends BaseAnimatorSet {
@Override
public void setAnimation(View view) {
animatorSet.playTogether(//
ObjectAnimator.ofFloat(view, "alpha", 0, 1).setDuration(duration));
}
}
封装思路
先要分清楚这三个对象
mTopContainer 最顶层的容器 全屏显示的
mControlHeightContainer 用于控制对话框宽度和高度的容器
mOnCreateView 真正填充Dialog的View
执行动画时,targetView是mControlHeightContainer 。
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mOnCreateView = onCreateView();//在Dialog的onCreate方法中,调用了抽象方法onCreateView,子类通过重写该方法,返回需要填充Dialog的View
// 当Dialog依附在Window上,设置对话框宽高以及显示动画
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
//调用子类重写的该方法,加载数据和需要执行的逻辑代码
setUIBeforeShow();
//设置Dialog的宽度
int width;
if (mWidthScale == 0) {
width = ViewGroup.LayoutParams.WRAP_CONTENT;
} else {//如果设置了缩放比例
width = (int) (mDisplayMetrics.widthPixels * mWidthScale);
}
int height;
if (mHeightScale == 0) {
height = ViewGroup.LayoutParams.WRAP_CONTENT;
} else if (mHeightScale == 1) {
height = (int) mMaxHeight;
} else {
height = (int) (mMaxHeight * mHeightScale);
}
if (mShowAnimator != null) {//如果设置了ShowAnimator,则执行,并在动画监听中设置动画的flag
mShowAnimator.listener(new BaseAnimatorSet.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
mIsShowAnimator = true;
}
@Override
public void onAnimationRepeat(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
mIsShowAnimator = false;
delayDismiss();
}
@Override
public void onAnimationCancel(Animator animator) {
mIsShowAnimator = false;
}
}).playOn(mControlHeightContainer);
//重写dismiss方法,如果设置了消失动画,监听中修改flag,并在动画结束或cancel时在dismiss掉Dialog
if (mDismissAnimator != null) {
mDismissAnimator.listener(new BaseAnimatorSet.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
mIsDismissAnimator = true;
}
@Override
public void onAnimationRepeat(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
mIsDismissAnimator = false;
superDismiss();
}
@Override
public void onAnimationCancel(Animator animator) {
mIsDismissAnimator = false;
superDismiss();
}
}).playOn(mControlHeightContainer);
} else {
superDismiss();
}
版本控制
版本号 | 更新内容 | 修改人 | 修改时间 |
---|---|---|---|
1.0 | 初次发布 | lucky_tiger | 2017/7/6 |
项目地址
所在文件夹 | demo位置 |
---|---|
widget.FlycoDialog | com.qr.demo.widget.dialog_pop.ui.SimpleHomeActivity |