Android L循环显示对话框

时间:2014-08-15 18:16:43

标签: android-animation android-dialog android-dialogfragment android-5.0-lollipop material-design

我正在尝试在Dialog上创建一个循环显示。我认为您可以在View的{​​{1}}中获取onCreate,隐藏它,然后使用Dialog显示它。

由于ViewAnimationUtils.createCircularReveal错误而导致View出现问题,我似乎无法解决此问题。

您如何将Material Design Animations应用于对话框?

修改

我已成功隐藏了对话框,方法是将code here应用于requestWindowFeature()方法。但是当我试图通过循环显示来显示时,视图保持不可见。

1 个答案:

答案 0 :(得分:13)

以下是解决方案:

片段

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       final View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        if(rootView != null){
            rootView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                @Override
                public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                    rootView.findViewById(R.id.btn_reveal).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            showDialog();
                        }
                    });
                }
            });
        }

        return rootView;
    }


    private void showDialog(){
        final View dialogView = View.inflate(getActivity(), R.layout.dialog_view, null);

        AlertDialog.Builder builder =  new AlertDialog.Builder(getActivity());
        builder.setView(dialogView)
               .setCancelable(false);


        final AlertDialog dialog = builder.create();
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                revealShow(dialogView, true, null);
            }
        });
        dialogView.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                revealShow(dialogView, false, dialog);
            }
        });
        dialogView.findViewById(R.id.btn_save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                revealShow(dialogView, false, dialog);
            }
        });


        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.show();

    }

    private void revealShow(View rootView, boolean reveal, final AlertDialog dialog){
        final View view = rootView.findViewById(R.id.reveal_view);
        int w = view.getWidth();
        int h = view.getHeight();
        float maxRadius = (float) Math.sqrt(w * w / 4 + h * h / 4);

        if(reveal){
            Animator revealAnimator = ViewAnimationUtils.createCircularReveal(view,
                    w / 2, h / 2, 0, maxRadius);

            view.setVisibility(View.VISIBLE);
            revealAnimator.start();

        } else {

            Animator anim =
                    ViewAnimationUtils.createCircularReveal(view, w / 2, h / 2, maxRadius, 0);

            anim.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    dialog.dismiss();
                    view.setVisibility(View.INVISIBLE);

                }
            });

            anim.start();
        }

    }
}

dialog_view的XML:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@android:color/transparent">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/reveal_view"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingBottom="8dp"
    android:paddingTop="8dp"
    android:background="@color/theme_primary"
    android:visibility="invisible">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="First Name"
        android:layout_marginTop="20dp"
        android:textColor="@android:color/white"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="Last Name"
        android:textColor="@android:color/white"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="Additional Info"
        android:textColor="@android:color/white"/>

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="20dp">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Male"
            android:textColor="@android:color/white"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Female"
            android:textColor="@android:color/white"/>

     </RadioGroup>

       <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="20dp">
        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Cancel"
            android:textColor="@android:color/white"
            android:background="?android:attr/selectableItemBackground"/>
        <Button
            android:id="@+id/btn_save"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Save"
            android:textColor="@android:color/white"
            android:background="?android:attr/selectableItemBackground"/>
        </LinearLayout>
   </LinearLayout>