![](/img/trans.png)
[英]NavigationView widget, complete animation before changes between Activities
[英]Animation code starts before first animation is complete
我尝试为抛硬币活动设置简单的抛硬币动画。 因此,我创建了两个可绘制对象(coin_heads 和 coin_tails)并设置了这段代码。 我正在创建一个随机值,然后我有一个简单的 if 语句来决定是否应该出现正面或反面。 取决于此,drawable 将被替换。 我已经知道每当你用yourobject.setImageDrawable(ContextCompat.getDrawable(this,drawable));
替换 drawable 时yourobject.setImageDrawable(ContextCompat.getDrawable(this,drawable));
它的旋转、缩放等正在被重置(这就是为什么我想将旋转和 alpha 设置为我之前为其设置动画的值)。
我认为使用 animate() 方法而不是 XML 参数会更容易,这就是我现在遇到问题的原因。 每当我尝试运行它时,它都会跳过我的第一行
cimg.setAlpha(1f);
cimg.animate().alphaBy(-1f).setDuration(800);
因此,为了描述会发生什么:只要我按下按钮翻转硬币,图像的 alpha 就会立即设置为 0,然后在 800 毫秒的时间内将动画向后设置为 1。 但我希望它首先在 800 毫秒内将其动画化为 Alpha=0,然后再在 800 毫秒内将其动画化为 1。
这里是flip方法的代码(我在调试的时候注释掉了很多):
public void flip(View view) {
TextView coin = (TextView) findViewById(R.id.coinField);
ImageView cimg = (ImageView) findViewById(R.id.coinimage);
double flip1 = Math.random();
//start of animation
//cimg.animate().scaleXBy(4f).setDuration(1000);
//cimg.animate().scaleYBy(4f).setDuration(1000);
//cimg.animate().rotationBy(180f).setDuration(1000);
//first animation
cimg.setAlpha(1f);
cimg.animate().alphaBy(-1f).setDuration(800);
//final animation
if (flip1>=0.5) {
coin.setText(R.string.heads);
cimg.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.coin_heads));
//cimg.setRotation(180f);
cimg.setAlpha(0f);
cimg.animate().alpha(1f).setDuration(800);
//cimg.animate().rotationBy(180f).setDuration(1000);
//cimg.animate().scaleXBy(0.25f).setDuration(1000);
//cimg.animate().scaleYBy(0.25f).setDuration(1000);
}
else {
coin.setText(R.string.tails);
cimg.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.coin_tails));
//cimg.setRotation(180f);
cimg.setAlpha(0f);
cimg.animate().alpha(1f).setDuration(800);
//cimg.animate().rotationBy(180f).setDuration(1000);
//cimg.animate().scaleXBy(0.25f).setDuration(1000);
//cimg.animate().scaleYBy(0.25f).setDuration(1000);
}
}
如果有人对此有解决方案,我会非常高兴,因为我已经为此苦苦挣扎了几个小时。 我也尝试使用Thread.sleep(1000)
来确保动画完成,但是这样做时,它仍然跳过第一个动画,等待 1 秒,然后照常执行。
编辑:
我是这样设置的。 现在,当我按下按钮时,什么也没有发生:
public void flip(View view) {
final TextView coin = (TextView) findViewById(R.id.coinField);
final ImageView cimg = (ImageView) findViewById(R.id.coinimage);
final double flip1 = Math.random();
Animation anim = new Animation() {
@Override
protected Animation clone() throws CloneNotSupportedException {
cimg.setAlpha(1f);
cimg.animate().alpha(0f).setDuration(800);
return super.clone();
}
};
final Animation anim2 = new Animation() {
@Override
protected Animation clone() throws CloneNotSupportedException {
if (flip1>=0.5) {
coin.setText(R.string.heads);
cimg.setImageDrawable(getDrawable(R.drawable.coin_heads));
cimg.setAlpha(0f);
cimg.animate().alpha(1f).setDuration(800);
}
else {
coin.setText(R.string.tails);
cimg.setImageDrawable(getDrawable(R.drawable.coin_tails));
//cimg.setRotation(180f);
cimg.setAlpha(0f);
cimg.animate().alpha(1f).setDuration(800);
}
return super.clone();
}
};
anim2.setAnimationListener(null);
Animation.AnimationListener list = new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
anim2.start();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
};
anim.setAnimationListener(list);
}
使用动画侦听器在第一个动画结束后开始第二个动画:
final Animation anim = new SomeTypeOfAnimation();
anim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
//Start your other animation here...
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.