Java 让一个按钮从白色闪烁到红色,然后给出它原来的圆形形状

Java 让一个按钮从白色闪烁到红色,然后给出它原来的圆形形状,java,android,android-studio,Java,Android,Android Studio,我已经搜索了堆栈溢出,但我就是找不到适合我的案例的正确答案 我目前正在Android Studio中编写一个测验应用程序,无论答案是否正确,我都会被闪烁的按钮卡住。 如果答案正确,它应该闪烁绿色,如果答案不正确,它应该闪烁红色 这是我的一个回答按钮: <Button android:id="@+id/choice1" android:layout_width="match_parent" android:layout_height="50dp" an

我已经搜索了堆栈溢出,但我就是找不到适合我的案例的正确答案

我目前正在Android Studio中编写一个测验应用程序,无论答案是否正确,我都会被闪烁的按钮卡住。 如果答案正确,它应该闪烁绿色,如果答案不正确,它应该闪烁红色

这是我的一个回答按钮:

    <Button
    android:id="@+id/choice1"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginBottom="24dp"
    android:fontFamily="@font/aldrich"
    android:padding="8dp"
    android:textColor="#fff"
    android:background="@drawable/rounded_button"/>
我尝试使用mButtonChoice1.setBackgroundResource(R.drawable.rounded_按钮);再次将按钮塑造成圆形,因为我的blink方法在闪烁时将其塑造成矩形

我的blinkEffect方法如下所示:

private void blinkEffectRed1(){
    ObjectAnimator animator = ObjectAnimator.ofInt(mButtonChoice1, 
    "backgroundColor", Color.RED , Color.parseColor(("#ff669900")));
    animator.setDuration(250);
    animator.setEvaluator(new ArgbEvaluator());
    animator.setRepeatCount(2);
    animator.setRepeatMode(ValueAnimator.REVERSE);
    animator.start();
}
现在的问题是,应用程序不再将背景设置为圆角按钮,并且按钮保持为矩形

为什么会发生这种情况?如何使圆形按钮闪烁并使其再次圆形

如果已经有这样一个问题的答案,我很抱歉,请提供给我的链接,然后


谢谢大家!

您正在为视图的背景色设置动画,它将为矩形/正方形的背景设置动画

相反,您必须实现alphaanimation

例如:

ObjectAnimator objAnimator = ObjectAnimator.ofFloat(mCircle, "alpha",0f,1f);
objAnimator.setDuration(1000);
objAnimator.setRepeatMode(Animation.REVERSE);
objAnimator.setRepeatCount(Animation.INFINITE);
objAnimator.start();

当答案为真时,设置按钮动画

if(answerTrue){

    // Set the color of the button to GREEN once.

    // Next, animate its visibility with the set color - which is GREEN as follows:

    Animation anim = new AlphaAnimation(0.0f, 1.0f);
    anim.setDuration(50); //You can manage the blinking time with this parameter
    anim.setStartOffset(20);
    anim.setRepeatMode(Animation.REVERSE);
    anim.setRepeatCount(Animation.INFINITE);
    button.startAnimation(anim);
}
在使用动画时,(有时)您希望与UI相关的操作是同步的。要实现这一点,需要向
ObjectAnimator
添加动画侦听器,并在动画结束时调用
setBackgroundResource()
方法

下面是一个示例,我编辑了您的
blinkEffectRed1
方法:

private void blinkEffectRed1(){
  ObjectAnimator animator = ObjectAnimator.ofInt(mButtonChoice1, "backgroundColor", Color.RED , Color.parseColor(("#ff669900")));
  animator.setDuration(250);
  animator.setEvaluator(new ArgbEvaluator());
  animator.setRepeatCount(2);
  animator.setRepeatMode(ValueAnimator.REVERSE);

  // Adding the listener
  animator.addListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation) {
        // Update the button when animation ends...
        mButtonChoice1.setBackgroundResource(R.drawable.rounded_button);
        updateQuestion();
    }
  });

  animator.start();
}

我希望能解决这个问题:)

我想你必须使用动画监听器(
animator.addListener(…)
),只有在调用了
onAnimationEnd
时,你才需要调用
setBackgroundResource
方法。。。你需要一个示例代码吗?因为我是安卓工作室的noob,你能举个例子吗?非常感谢。
if(answerTrue){

    // Set the color of the button to GREEN once.

    // Next, animate its visibility with the set color - which is GREEN as follows:

    Animation anim = new AlphaAnimation(0.0f, 1.0f);
    anim.setDuration(50); //You can manage the blinking time with this parameter
    anim.setStartOffset(20);
    anim.setRepeatMode(Animation.REVERSE);
    anim.setRepeatCount(Animation.INFINITE);
    button.startAnimation(anim);
}
private void blinkEffectRed1(){
  ObjectAnimator animator = ObjectAnimator.ofInt(mButtonChoice1, "backgroundColor", Color.RED , Color.parseColor(("#ff669900")));
  animator.setDuration(250);
  animator.setEvaluator(new ArgbEvaluator());
  animator.setRepeatCount(2);
  animator.setRepeatMode(ValueAnimator.REVERSE);

  // Adding the listener
  animator.addListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation) {
        // Update the button when animation ends...
        mButtonChoice1.setBackgroundResource(R.drawable.rounded_button);
        updateQuestion();
    }
  });

  animator.start();
}