Я делаю приложение пустяков. У меня есть список вопросов, и на каждый вопрос есть 4 ответа, но только один ответ правильный. Когда я нажимаю кнопку (каждый ответ является кнопкой), если это правильный ответ, приложение должно отображать анимацию исчезновения, а если ответ неправильный, я создаю анимацию дрожания, которая должна появиться. При первом и третьем вопросе анимации выглядят случайными.
Я также должен указать, что у меня возникает ошибка при отладке: исходный код не соответствует байт-коду
public void onClick(View v) {
if (index < totalQuestion) {
Button clickedButton = (Button) v;
if (clickedButton.getText().equals(Common.questionList.get(index).getCorrectAnswer())) {
fadeView();
score += 10;
correctAnswer++;
showQuestion(++index);
} else {
shakeAnimation();
if (score > 0) {
score -= 10;
}
showQuestion(++index);
}
if (Common.questionList.isEmpty()) {
Intent intent = new Intent(this, Done.class);
Bundle dataSend = new Bundle();
dataSend.putInt("SCORE", score);
dataSend.putInt("TOTAL", totalQuestion);
dataSend.putInt("CORRECT", correctAnswer);
intent.putExtras(dataSend);
startActivity(intent);
finish();
}
txtscore.setText(String.format("%d", score));
}
}
Анимации:
private void fadeView() {
final CardView cardView = findViewById(R.id.cardview);
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
alphaAnimation.setDuration(350);
alphaAnimation.setRepeatCount(1);
alphaAnimation.setRepeatMode(Animation.REVERSE);
cardView.setAnimation(alphaAnimation);
txtscore.setAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
cardView.setCardBackgroundColor(Color.GREEN);
txtscore.setTextColor(Color.GREEN);
}
@Override
public void onAnimationEnd(Animation animation) {
cardView.setCardBackgroundColor(Color.WHITE);
txtscore.setTextColor(Color.WHITE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
private void shakeAnimation() {
Animation shake = AnimationUtils.loadAnimation(Playing.this, R.anim.shake_animation);
final CardView cardView = findViewById(R.id.cardview);
cardView.setAnimation(shake);
txtscore.setAnimation(shake);
shake.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
cardView.setCardBackgroundColor(Color.RED);
txtscore.setTextColor(Color.RED);
}
@Override
public void onAnimationEnd(Animation animation) {
cardView.setCardBackgroundColor(Color.WHITE);
txtscore.setTextColor(Color.WHITE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}