Android 关于琐事应用程序中的动画,updateQuestion背后的逻辑是什么?

Android 关于琐事应用程序中的动画,updateQuestion背后的逻辑是什么?,android,animation,android-animation,Android,Animation,Android Animation,我正在Android Studio上创建一个琐事应用程序。对于每个语句,用户必须选择true或false。根据输入,如果用户选择的答案正确,则语句文本将以绿色淡入,而如果用户选择的答案错误,则语句文本将以红色抖动。这是我目前的代码: List<Question> questionList; private ActivityMainBinding binding; private int currentQuestionIndex = 0; @Override

我正在Android Studio上创建一个琐事应用程序。对于每个语句,用户必须选择true或false。根据输入,如果用户选择的答案正确,则语句文本将以绿色淡入,而如果用户选择的答案错误,则语句文本将以红色抖动。这是我目前的代码:

List<Question> questionList;
    private ActivityMainBinding binding;
    private int currentQuestionIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        questionList = new Repository().getQuestions(questionArrayList -> {
                    binding.questionTextview.setText(questionArrayList.get(currentQuestionIndex)
                            .getAnswer());

                    updateCounter(questionArrayList);
                }

        );


        binding.buttonNext.setOnClickListener(view -> {

            currentQuestionIndex = (currentQuestionIndex + 1) % questionList.size();
            updateQuestion();

        });
        binding.buttonTrue.setOnClickListener(view -> {
            checkAnswer(true);
            //updateQuestion();

        });
        binding.buttonFalse.setOnClickListener(view -> {
            checkAnswer(false);
            //updateQuestion();

        });


    }
//checking if the answer is correct, or not.
    private void checkAnswer(boolean userChoseCorrect) {
        boolean answer = questionList.get(currentQuestionIndex).isAnswerTrue();
        int snackMessageId = 0;
        if (userChoseCorrect == answer) {
            snackMessageId = R.string.correct_answer;
            fadeAnimation();
        } else {
            snackMessageId = R.string.incorrect;
            shakeAnimation();
        }
        Snackbar.make(binding.cardView, snackMessageId, Snackbar.LENGTH_SHORT)
                .show();

    }
//updating the counter. The counter will basically display the question # / total questions
    private void updateCounter(ArrayList<Question> questionArrayList) {
        binding.textViewOutOf.setText(String.format(getString(R.string.text_formatted),
                currentQuestionIndex, questionArrayList.size()));
    }

    private void fadeAnimation() {
        AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
        alphaAnimation.setDuration(300);
        alphaAnimation.setRepeatCount(1);
        alphaAnimation.setRepeatMode(Animation.REVERSE);

        binding.cardView.setAnimation(alphaAnimation);

        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                binding.questionTextview.setTextColor(Color.GREEN);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                binding.questionTextview.setTextColor(Color.WHITE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });


    }

    private void updateQuestion() {
        String question = questionList.get(currentQuestionIndex).getAnswer();
        binding.questionTextview.setText(question);
        updateCounter((ArrayList<Question>) questionList);
    }

    private void shakeAnimation() {
        Animation shake = AnimationUtils.loadAnimation(MainActivity.this,
                R.anim.shake_animation);
        binding.cardView.setAnimation(shake);

        shake.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                binding.questionTextview.setTextColor(Color.RED);

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                binding.questionTextview.setTextColor(Color.WHITE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });


    }
列表问题列表;
私人活动主要约束;
private int currentQuestionIndex=0;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
binding=DataBindingUtil.setContentView(this,R.layout.activity_main);
questionList=新存储库().getQuestions(questionArrayList->{
binding.questionTextview.setText(questionArrayList.get(currentQuestionIndex)
.getAnswer());
更新计数器(问题列表);
}
);
binding.buttonNext.setOnClickListener(视图->{
currentQuestionIndex=(currentQuestionIndex+1)%questionList.size();
updateQuestion();
});
binding.buttonTrue.setOnClickListener(视图->{
核对答案(正确);
//updateQuestion();
});
binding.buttonFalse.setOnClickListener(视图->{
核对答案(假);
//updateQuestion();
});
}
//检查答案是否正确。
私有void checkAnswer(布尔userChoseCorrect){
布尔答案=questionList.get(currentQuestionIndex.isAnswerTrue();
int snackMessageId=0;
if(userChoseCorrect==答案){
snackMessageId=R.string.correct\u答案;
fadeAnimation();
}否则{
snackMessageId=R.string.error;
shakeAnimation();
}
Snackbar.make(binding.cardwiew、snackMessageId、Snackbar.LENGTH\u SHORT)
.show();
}
//更新计数器。计数器基本上会显示问题#/全部问题
私有void更新计数器(ArrayList questionArrayList){
binding.textViewOutOf.setText(String.format(getString)(R.String.text\u格式化),
currentQuestionIndex,questionArrayList.size());
}
私有void fadeAnimation(){
AlphaAnimation AlphaAnimation=新的AlphaAnimation(1.0f,0.0f);
alphaAnimation.setDuration(300);
alphaAnimation.setRepeatCount(1);
alphaAnimation.setRepeatMode(Animation.REVERSE);
binding.cardwiew.setAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(新的Animation.AnimationListener(){
@凌驾
onAnimationStart上的公共无效(动画){
binding.questionTextview.setTextColor(Color.GREEN);
}
@凌驾
onAnimationEnd上的公共无效(动画){
binding.questionTextview.setTextColor(Color.WHITE);
}
@凌驾
onAnimationRepeat上的公共无效(动画){
}
});
}
私有void updateQuestion(){
String question=questionList.get(currentQuestionIndex.getAnswer();
binding.questionTextview.setText(问题);
更新计数器((ArrayList)问题列表);
}
私有动画(){
Animation shake=AnimationUtils.loadAnimation(MainActivity.this,
R.anim.shake_动画);
binding.cardwiew.setAnimation(抖动);
shake.setAnimationListener(新建Animation.AnimationListener(){
@凌驾
onAnimationStart上的公共无效(动画){
binding.questionTextview.setTextColor(Color.RED);
}
@凌驾
onAnimationEnd上的公共无效(动画){
binding.questionTextview.setTextColor(Color.WHITE);
}
@凌驾
onAnimationRepeat上的公共无效(动画){
}
});
}
这里的代码工作得非常好。但是,在我调用buttonTrue和buttonFalse的
onClick
方法中的
updateQuestion()
(作为测试,这是偶然的)之前,动画仅在我单击next按钮之后才出现为什么需要调用updateQuestion()方法来调用动画?为什么我不能调用checkAnswer(),它将调用动画方法?

谢谢