无法在对话框中获取视图中心
Cannot get center of view inside dialog
本质上,我在一个对话框中有两个视图。一个 button
和一个作为背景的 LinearLayout
。我正在尝试使用按钮中心的 CircularReveal
为这个 LinearLayout
设置动画,以便它用一种颜色覆盖整个对话框。
这是我用作对话框的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();
}
发生的事情是坐标甚至离按钮都不近。事实上,动画是从对话框的中心开始的。
知道我做错了什么吗?
谢谢,
应该这样做:
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>
这个视图是显示将位于(启动 startCircularReveal() 方法时命名的视图)之上的位置,希望对您有所帮助。
本质上,我在一个对话框中有两个视图。一个 button
和一个作为背景的 LinearLayout
。我正在尝试使用按钮中心的 CircularReveal
为这个 LinearLayout
设置动画,以便它用一种颜色覆盖整个对话框。
这是我用作对话框的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();
}
发生的事情是坐标甚至离按钮都不近。事实上,动画是从对话框的中心开始的。
知道我做错了什么吗?
谢谢,
应该这样做:
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>
这个视图是显示将位于(启动 startCircularReveal() 方法时命名的视图)之上的位置,希望对您有所帮助。