您好我想链接一些Android动画。 我添加了一个监听器,在动画结束后调用一个新动画。
我不知道这是否是我的手机导致的缓慢。
这是我的代码:
点击开始,等待组合 单击代表组合的按钮 重新开始两个新的组合 点击两个按钮......然后3,4,5 ......
private Resources res;
AnimationDrawable frameAnimation;
private TextView result;
private TextView punchSize;
private int correctColorPunches = 0;
private List<Integer> combination = new ArrayList<Integer>();
private List<Integer> enteredCombination = new ArrayList<Integer>();;
Random randomGenerator = new Random();
Animation animation1;
Animation animation2;
Animation animation3;
Animation animation4;
Animation animation5;
int animationCounter = 0;
private boolean animationReady = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String deviceId = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
res = getResources();
button1 = (ImageView) findViewById(R.id.button1);
button2 = (ImageView) findViewById(R.id.button2);
button3 = (ImageView) findViewById(R.id.button3);
button4 = (ImageView) findViewById(R.id.button4);
button5 = (ImageView) findViewById(R.id.button5);
animation1 = AnimationUtils.loadAnimation(this, R.anim.animation);
animation2 = AnimationUtils.loadAnimation(this, R.anim.animation);
animation3 = AnimationUtils.loadAnimation(this, R.anim.animation);
animation4 = AnimationUtils.loadAnimation(this, R.anim.animation);
animation5 = AnimationUtils.loadAnimation(this, R.anim.animation);
animation1.setAnimationListener(this);
animation2.setAnimationListener(this);
animation3.setAnimationListener(this);
animation4.setAnimationListener(this);
animation5.setAnimationListener(this);
result = (TextView) findViewById(R.id.result);
punchSize = (TextView) findViewById(R.id.punchSize);
startGameButton = (Button) findViewById(R.id.startGameButton);
result.setText("0");
startGameButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
punchSize.setText("1");
startNewCombinationRound();
enableOnClickListener();
msg("Spiel gestartet...");
}
});
}
private Animation getAnimation(int animationId) {
switch (animationId){
case 0 : return animation1;
case 1 : return animation2;
case 2 : return animation3;
case 3 : return animation4;
case 4 : return animation5;
}
return null;
}
private void playAnimation() {
while (!animationReady) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
if(animationCounter < combination.size()) {
animationReady = false;
findViewById(getButton(combination.get(animationCounter))).startAnimation(getAnimation(combination.get(animationCounter)));
return;
}
}
private int generateRandomButtonId() {
return randomGenerator.nextInt(4)+1;
}
private void startNewCombinationRound() {
enteredCombination.clear();
this.combination.add(generateRandomButtonId());
animationCounter = 0;
playAnimation();
}
private int getButton(int buttonId) {
switch (buttonId){
case 1 : return R.id.button1;
case 2 : return R.id.button2;
case 3 : return R.id.button3;
case 4 : return R.id.button4;
case 5 : return R.id.button5;
}
return R.id.button1;
}
private void enableOnClickListener() {
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setValuesAndValidate(1);
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setValuesAndValidate(2);
}
});
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setValuesAndValidate(3);
}
});
button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setValuesAndValidate(4);
}
});
button5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setValuesAndValidate(5);
}
});
}
private void disableOnClickListener() {
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tooFastMsg();
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tooFastMsg();
}
});
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tooFastMsg();
}
});
button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tooFastMsg();
}
});
button5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tooFastMsg();
}
});
}
private void tooFastMsg() {
msg("Du bist zu schnell!");
}
private void setValuesAndValidate(int buttonId) {
enteredCombination.add(buttonId);
validateCombination();
setResult();
}
private void setResult() {
correctColorPunches = correctColorPunches + 1;
result.setText(String.valueOf(correctColorPunches));
}
private void validateCombination() {
int numberOfEnteredValues = enteredCombination.size();
if(combination.size()==0) {
msg("Noch nicht gestartet...");
} else {
if(numberOfEnteredValues < combination.size()) {
for (int i = 0; i < numberOfEnteredValues; i++) {
if(!combination.get(i).equals(enteredCombination.get(i))) {
msg("GameOver!");
Intent intent = getIntent();
finish();
startActivity(intent);
return;
}
}
return;
}
for (int i = 0; i < numberOfEnteredValues; i++) {
if(!combination.get(i).equals(enteredCombination.get(i))) {
msg("GameOver!");
Intent intent = getIntent();
finish();
startActivity(intent);
return;
}
}
punchSize.setText(String.valueOf(combination.size()+1));
msg("Richtig!");
startNewCombinationRound();
}
}
public void onAnimationStart(Animation animation) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
Log.d("blub", "#######################################################START"+dateFormat.format(date) + " counter:"+animationCounter);
animationCounter++;
//To change body of implemented methods use File | Settings | File Templates.
}
public void onAnimationEnd(Animation animation) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
Log.d("blub", "#######################################################ENDE"+dateFormat.format(date) + " counter:"+animationCounter);
animationReady = true;
playAnimation();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
public void onAnimationRepeat(Animation animation) {
}
}