动画结束视图坐标与开始时相同
On animation end view coordinates are the same as on start
我有以下布局,其中包含同一父项中的两个 EditText
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent">
<EditText
android:id="@+id/et_profession"
android:hint="Profession"
android:imeOptions="actionNext"
android:layout_marginLeft="32dp" android:layout_marginRight="32dp"
android:layout_width="match_parent" android:layout_height="48dp" android:inputType="text"
tools:ignore="LabelFor" />
<EditText
android:id="@+id/et_company"
android:hint="Company"
android:visibility="invisible"
android:layout_marginLeft="32dp" android:layout_marginRight="32dp"
android:layout_width="match_parent" android:layout_height="48dp" android:inputType="text"
tools:ignore="LabelFor" />
</LinearLayout>
我想要的是当用户按下键盘上的下一个操作按钮时第一个 editText 将第二个 editText 动画到第一个的位置。
这是我的代码:
private void startCompanyAnim() {
etCompany.animate()
.setDuration(2000L)
.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
etCompany.setVisibility(VISIBLE);
Log.i("ANIM", "START : prof.TOP == " + etProfession.getTop() + " and comp.TOP == " + etCompany.getTop());
}
@Override
public void onAnimationEnd(Animator animation) {
Log.i("ANIM", "END : prof.TOP == " + etProfession.getTop() + " and comp.TOP == " + etCompany.getTop());
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
})
.translationY(etProfession.getTop() - etCompany.getTop())
.start();
}
虽然这似乎可行并且第二个视图位于第一个视图之上,但问题是当我记录时,第二个 editText 的 getTop()
值与动画之前的值相同.
例如,这是我在 logcat 上得到的:
01-20 09:08:25.646 2014-2014/com.anim.androidanimations I/ANIM: START : prof.TOP == 0 and comp.TOP == 72
01-20 09:08:27.655 2014-2014/com.anim.androidanimations I/ANIM: END : prof.TOP == 0 and comp.TOP == 72
第二个视图 onAnimationEnd()
的记录 getTop()
不应该与第一个视图具有相同的值吗?此外,因为我可以在我的设备屏幕上看到它确实位于第一个屏幕之上,为什么它给我的价值与动画之前相同?
尝试修改如下:
private void startCompanyAnim() {
etCompany.animate()
// ...
.yBy(etProfession.getTop() - etCompany.getTop())
.start();
}
这将直接影响 Y 属性。
对于您的情况,这是实现此目的的简单方法。
@Override
public void onAnimationEnd(Animator animation) {
etCompany.layout(etProfession.getLeft(),etProfession.getTop(),etProfession.getRight(),etProfession.getBottom());
Log.i("ANIM", "END : prof.TOP == " + etProfession.getTop() + " and comp.TOP == " + etCompany.getTop());
}
我有以下布局,其中包含同一父项中的两个 EditText
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent">
<EditText
android:id="@+id/et_profession"
android:hint="Profession"
android:imeOptions="actionNext"
android:layout_marginLeft="32dp" android:layout_marginRight="32dp"
android:layout_width="match_parent" android:layout_height="48dp" android:inputType="text"
tools:ignore="LabelFor" />
<EditText
android:id="@+id/et_company"
android:hint="Company"
android:visibility="invisible"
android:layout_marginLeft="32dp" android:layout_marginRight="32dp"
android:layout_width="match_parent" android:layout_height="48dp" android:inputType="text"
tools:ignore="LabelFor" />
</LinearLayout>
我想要的是当用户按下键盘上的下一个操作按钮时第一个 editText 将第二个 editText 动画到第一个的位置。
这是我的代码:
private void startCompanyAnim() {
etCompany.animate()
.setDuration(2000L)
.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
etCompany.setVisibility(VISIBLE);
Log.i("ANIM", "START : prof.TOP == " + etProfession.getTop() + " and comp.TOP == " + etCompany.getTop());
}
@Override
public void onAnimationEnd(Animator animation) {
Log.i("ANIM", "END : prof.TOP == " + etProfession.getTop() + " and comp.TOP == " + etCompany.getTop());
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
})
.translationY(etProfession.getTop() - etCompany.getTop())
.start();
}
虽然这似乎可行并且第二个视图位于第一个视图之上,但问题是当我记录时,第二个 editText 的 getTop()
值与动画之前的值相同.
例如,这是我在 logcat 上得到的:
01-20 09:08:25.646 2014-2014/com.anim.androidanimations I/ANIM: START : prof.TOP == 0 and comp.TOP == 72
01-20 09:08:27.655 2014-2014/com.anim.androidanimations I/ANIM: END : prof.TOP == 0 and comp.TOP == 72
第二个视图 onAnimationEnd()
的记录 getTop()
不应该与第一个视图具有相同的值吗?此外,因为我可以在我的设备屏幕上看到它确实位于第一个屏幕之上,为什么它给我的价值与动画之前相同?
尝试修改如下:
private void startCompanyAnim() {
etCompany.animate()
// ...
.yBy(etProfession.getTop() - etCompany.getTop())
.start();
}
这将直接影响 Y 属性。
对于您的情况,这是实现此目的的简单方法。
@Override
public void onAnimationEnd(Animator animation) {
etCompany.layout(etProfession.getLeft(),etProfession.getTop(),etProfession.getRight(),etProfession.getBottom());
Log.i("ANIM", "END : prof.TOP == " + etProfession.getTop() + " and comp.TOP == " + etCompany.getTop());
}