无法在对话框中获得视图中心

时间:2017-03-15 07:49:16

标签: android android-animation

我在对话框中实际上有两个视图。 buttonLinearLayout作为背景。我尝试使用LinearLayout从按钮中心为CircularReveal设置动画,以便用彩色覆盖整个对话框。

这是我用作对话框的xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ScrollView android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            ...

            <com.dd.CircularProgressButton
                android:id="@+id/sign_in_enter_button"
                android:layout_gravity="bottom|center_horizontal"
                android:layout_width="match_parent"
                android:layout_height="42dp"
                android:layout_marginRight="24dp"
                android:layout_marginLeft="24dp"
                android:textColor="@android:color/white"
                android:textSize="15dp"
                app:cpb_cornerRadius="1dp"
                app:cpb_textIdle="@string/login_sign_in"
                app:cpb_iconComplete="@drawable/ic_done_mini"
                app:cpb_selectorIdle="@color/morph_button_idle_colors"
                app:cpb_selectorComplete="@color/morph_button_complete_colors"
                app:cpb_colorIndicator="@color/colorAccent"
                app:cpb_colorIndicatorBackground="@android:color/transparent"
                app:cpb_colorProgress="@android:color/transparent"/>

            ...

        </LinearLayout>

    </ScrollView>

    <!-- Background to be animated behind the CircularProgressButton -->
    <LinearLayout
        android:id="@+id/sign_in_dialog_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:visibility="invisible"
        android:layout_gravity="center"
        android:gravity="center"
        android:background="@color/colorAccent">

        ...

    </LinearLayout>

</FrameLayout>

要做这个动画,我已经使用过这种方法,我已经在我的应用程序的其他地方使用过这种方法并且运行得很好:

final CircularProgressButton enterButton = (CircularProgressButton) parent.findViewById(R.id.sign_in_enter_button);

...

// This is called inside a listener, so the button is already drawn.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
    int enterButtonX = (enterButton.getLeft()
            + enterButton.getRight()) / 2;

    int enterButtonY = ((int) enterButton.getY()
            + enterButton.getHeight()) / 2;

    View background = mAlertDialogView.findViewById(R.id.sign_in_dialog_background);

    int radiusReveal = Math.max(background.getWidth()
            , background.getHeight());

    background.setVisibility(View.VISIBLE);

    Animator animator =
            android.view.ViewAnimationUtils.createCircularReveal(background
                    , enterButtonX
                    , enterButtonY
                    , 0
                    , radiusReveal);

    animator.setDuration(500);
    animator.setInterpolator(
            AnimationUtils.loadInterpolator(LoginUI.this, R.anim.accelerator_interpolator));

    animator.start();
}

发生的事情是坐标甚至不靠近按钮。实际上,动画从对话框的中心开始。

知道我做错了什么?

谢谢,

1 个答案:

答案 0 :(得分:1)

这应该这样做:

public void startCircularReveal() {
    final View view = findViewById(R.id.revealView);
    final View startView = findViewById(R.id. button);
    int cx = (startView.getLeft() + startView.getRight()) / 2;
    int cy = (startView.getTop() + startView.getBottom()) / 2;
    view.setBackgroundColor(Color.parseColor("#6FA6FF"));
    int finalRadius = Math.max(cy , view.getHeight() - cy);
    Animator anim = ViewAnimationUtils.createCircularReveal(view, cx, cy, 0, finalRadius);
    anim.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {

        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });
    anim.setDuration(200);
    view.setVisibility(View.VISIBLE);
    anim.start();
}

将视图startView更改为您的按钮,onAnimationEnd在显示完成后执行任何操作。

现在将其添加到xml的底部:

<FrameLayout
     android:id="@+id/revealView"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:background="@color/custom_theme_color"
     android:orientation="vertical"
     android:visibility="invisible">
</FrameLayout>

这个视图是显示在(start at startCircularReveal()方法的命名视图)的顶部,希望它有所帮助。