NiftyDialogEffects(各种动画的dialog)

来源:互联网 发布:qs网络语是什么意思 编辑:程序博客网 时间:2024/05/16 08:03

NiftyDialogBuilder:

该类继承Dialog,提供了快速创建dialog的方法

在构造器中就将dialog的view进行了初始化:

public NiftyDialogBuilder(Context context) {super(context);init(context);}public NiftyDialogBuilder(Context context, int theme) {super(context, theme);init(context);}

private void init(Context context) {mDialogView = View.inflate(context, R.layout.dialog_layout, null);mLinearLayoutView = (LinearLayout) mDialogView.findViewById(R.id.parentPanel);mRelativeLayoutView = (RelativeLayout) mDialogView.findViewById(R.id.main);mLinearLayoutTopView = (LinearLayout) mDialogView.findViewById(R.id.topPanel);mLinearLayoutMsgView = (LinearLayout) mDialogView.findViewById(R.id.contentPanel);mFrameLayoutCustomView = (FrameLayout) mDialogView.findViewById(R.id.customPanel);mTitle = (TextView) mDialogView.findViewById(R.id.alertTitle);mMessage = (TextView) mDialogView.findViewById(R.id.message);mIcon = (ImageView) mDialogView.findViewById(R.id.icon);mDivider = mDialogView.findViewById(R.id.titleDivider);mButton1 = (Button) mDialogView.findViewById(R.id.button1);mButton2 = (Button) mDialogView.findViewById(R.id.button2);setContentView(mDialogView);this.setOnShowListener(new OnShowListener() {@Overridepublic void onShow(DialogInterface dialogInterface) {mLinearLayoutView.setVisibility(View.VISIBLE);if (type == null) {type = Effectstype.Slidetop;}start(type);}});mRelativeLayoutView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {if (isCancelable)dismiss();}});}
并且在show的监听器里面设置了动画的开始:

private void start(Effectstype type) {BaseEffects animator = type.getAnimator();if (mDuration != -1) {animator.setDuration(Math.abs(mDuration));}animator.start(mRelativeLayoutView);}
这个方法可以通过枚举类来获得对应动画类的响应实例,然后设置动画时间,以及用animator.start(mRelativeLayoutView);开启动画:

 public void start(View view) {        reset(view);        setupAnimation(view);        mAnimatorSet.start();    }    public void reset(View view) {        ViewHelper.setPivotX(view, view.getMeasuredWidth() / 2.0f);        ViewHelper.setPivotY(view, view.getMeasuredHeight() / 2.0f);    }
首先通过setupAnimation(view);来安装动画:

 @Override    protected void setupAnimation(View view) {        getAnimatorSet().playTogether(                ObjectAnimator.ofFloat(view,"alpha",0,1).setDuration(mDuration)        );    }

安装动画是通过调用BaseEffects里面的mAnimatorSet来将不同的属性动画放在一起。


并且在onCreate中设置了窗口的属性:

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);WindowManager.LayoutParams params = getWindow().getAttributes();params.height = ViewGroup.LayoutParams.MATCH_PARENT;params.width = ViewGroup.LayoutParams.MATCH_PARENT;getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);}
这样以后在调用这个dialog.show()的时候就不会重复初始化。


把这个dialog设计成单例模式,可以防止被实例化多次而占用了更大的内存,这种单例模式是双重检查加锁的单例:

public static NiftyDialogBuilder getInstance(Context context) {if (instance == null || !tmpContext.equals(context)) {synchronized (NiftyDialogBuilder.class) {if (instance == null || !tmpContext.equals(context)) {instance = new NiftyDialogBuilder(context, R.style.dialog_untran);}}}tmpContext = context;return instance;}
比如loading的对话框这种全局使用的对话框适合做单例模式







0 0
原创粉丝点击