我是android编程的新手,我正在尝试制作一个简单的游戏。我想要有3个正方形,并且在开始按钮上我想生成一些随机数并旋转其中一个正方形。我在循环中这样做,因为我的随机数不固定。
package com.example.smartpc.memorygame;
import android.graphics.drawable.TransitionDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class MemoryGame extends AppCompatActivity {
ImageView imgRed;
ImageView imgGreen;
ImageView imgBlue;
Button btnStart;
ArrayList<Integer>array=new ArrayList<>();
int brojac=3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_memory_game);
imgRed=(ImageView)findViewById(R.id.imgRed);
imgGreen=(ImageView)findViewById(R.id.imgGreen);
imgBlue=(ImageView)findViewById(R.id.imgBlue);
btnStart=(Button)findViewById(R.id.btnStart);
final RotateAnimation animation=new RotateAnimation(0f,360f, RotateAnimation.RELATIVE_TO_SELF,0.5f,
RotateAnimation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(1000);
imgRed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imgRed.startAnimation(animation);
}
});
imgGreen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imgGreen.startAnimation(animation);
}
});
imgBlue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imgBlue.startAnimation(animation);
}
});
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i=0;i<brojac;i++) {
Random rand = new Random();
int number = rand.nextInt() % 3;
if (number == 1) {
imgRed.startAnimation(animation);
}
else if (number == 2) {
imgGreen.startAnimation(animation);
}
else {
imgBlue.startAnimation(animation);
}
}
}
});
}
}
我需要等到一个动画结束,然后我需要开始另一个循环迭代。有谁知道如何做到这一点?谢谢!
答案 0 :(得分:1)
在btnStart上单击“仅创建随机数”并根据该开始动画
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Random rand = new Random();
int number = rand.nextInt() % 3;
if (number == 1) {
imgRed.startAnimation(animation);
}
else if (number == 2) {
imgGreen.startAnimation(animation);
}
else {
imgBlue.startAnimation(animation);
}
}
});
在setAnimationListener中,当动画结束时,再次创建随机数并依赖于该开始动画
final RotateAnimation animation=new RotateAnimation(0f,360f, RotateAnimation.RELATIVE_TO_SELF,0.5f,
RotateAnimation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(1000);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
Random rand = new Random();
int number = rand.nextInt() % 3;
if (number == 1) {
imgRed.startAnimation(animation);
}
else if (number == 2) {
imgGreen.startAnimation(animation);
}
else {
imgBlue.startAnimation(animation);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});