多个 ViewPropertyAnimators
Multiple ViewPropertyAnimators
希望我没有在这里重复问题;我找不到关于多个 ViewPropertyAnimators
的信息。目标是让视图在 8 秒内从 y1 动画到 y2。第一秒淡出,最后一秒淡出
这是我在 Activity 的 onCreate()
中尝试过的:
final View animatingView = findViewById(R.id.animateMe);
animatingView.post(new Runnable() {
@Override
public void run() {
//Translation
animatingView.setY(0);
animatingView.animate().translationY(800).setDuration(8000);
//Fading view in
animatingView.setAlpha(0f);
animatingView.animate().alpha(1f).setDuration(1000);
//Waiting 6 seconds and then fading the view back out
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
animatingView.animate().alpha(0f).setDuration(1000);
}
}, 6000);
}
});
但是,结果是从 0 到 800 的转换,以及从 0 到 1 的 alpha 在一秒钟内完成的转换。然后 6 秒后视图淡出。它看起来每次我调用 View.animate() 它 returns 相同的 ViewPropertyAnimator。有没有办法让我拥有多个?我正在考虑为视图的 alpha 设置动画,将视图嵌套在相对布局中,然后为相对布局转换设置动画。如果没有必要,我宁愿不走那条路。有人知道更好的解决方案吗?
您可以通过直接使用 ObjectAnimator
实例来解决此问题,而不是使用 .animate()
抽象。
ObjectAnimator translationY = ObjectAnimator.ofFloat(animatingView, "translationY", 0f, 800f);
translationY.setDuration(8000);
ObjectAnimator alpha1 = ObjectAnimator.ofFloat(animatingView, "alpha", 0f, 1f);
alpha1.setDuration(1000);
ObjectAnimator alpha2 = ObjectAnimator.ofFloat(animatingView, "alpha", 1f, 0f);
alpha2.setDuration(1000);
alpha2.setStartDelay(7000);
AnimatorSet set = new AnimatorSet();
set.playTogether(translationY, alpha1, alpha2);
set.start();
希望我没有在这里重复问题;我找不到关于多个 ViewPropertyAnimators
的信息。目标是让视图在 8 秒内从 y1 动画到 y2。第一秒淡出,最后一秒淡出
这是我在 Activity 的 onCreate()
中尝试过的:
final View animatingView = findViewById(R.id.animateMe);
animatingView.post(new Runnable() {
@Override
public void run() {
//Translation
animatingView.setY(0);
animatingView.animate().translationY(800).setDuration(8000);
//Fading view in
animatingView.setAlpha(0f);
animatingView.animate().alpha(1f).setDuration(1000);
//Waiting 6 seconds and then fading the view back out
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
animatingView.animate().alpha(0f).setDuration(1000);
}
}, 6000);
}
});
但是,结果是从 0 到 800 的转换,以及从 0 到 1 的 alpha 在一秒钟内完成的转换。然后 6 秒后视图淡出。它看起来每次我调用 View.animate() 它 returns 相同的 ViewPropertyAnimator。有没有办法让我拥有多个?我正在考虑为视图的 alpha 设置动画,将视图嵌套在相对布局中,然后为相对布局转换设置动画。如果没有必要,我宁愿不走那条路。有人知道更好的解决方案吗?
您可以通过直接使用 ObjectAnimator
实例来解决此问题,而不是使用 .animate()
抽象。
ObjectAnimator translationY = ObjectAnimator.ofFloat(animatingView, "translationY", 0f, 800f);
translationY.setDuration(8000);
ObjectAnimator alpha1 = ObjectAnimator.ofFloat(animatingView, "alpha", 0f, 1f);
alpha1.setDuration(1000);
ObjectAnimator alpha2 = ObjectAnimator.ofFloat(animatingView, "alpha", 1f, 0f);
alpha2.setDuration(1000);
alpha2.setStartDelay(7000);
AnimatorSet set = new AnimatorSet();
set.playTogether(translationY, alpha1, alpha2);
set.start();