I want to ignore the animation instructions when I give the same instructions.

Asked 2 years ago, Updated 2 years ago, 69 views

The following is an animation that changes the color of the imageview when you press the button, but if you press this id=start button continuously, the animation will start again from the beginning (although it's only natural).

For example, when the same button is pressed continuously, the animation does not start from the beginning when the same button is used.In other words, we would like to give instructions such as ignoring the same signal.
The code is dirty and I'm sorry I didn't know, but if you know how to do it, I'd appreciate it if you could let me know.
Thank you for your cooperation.

setContentView(R.layout.activity_main);
    image=(ImageView) findViewById (R.id.image);

    btnStart=(Button) findViewById (R.id.start);
    btnStart.setOnClickListener(newView.OnClickListener(){

        @ Override
        public void onClick (View v) {
            startColorAnimation (image);
        }});

    btnStart=(Button) findViewById (R.id.start1);
    btnStart.setOnClickListener(newView.OnClickListener(){

        @ Override
        public void onClick (View v) {
            startColorAnimation1(image);
        }});

or

public void startColorAnimation(View view){
    int colorStart = 0xFFffff;
    int colorEnd = 0xFF000000;

    ValueAnimator colorAnim=ObjectAnimator.ofInt(
            view, "backgroundColor", colorStart, colorEnd);
    colorAnim.setDuration(2000);
    colorAnim.setEvaluator(newArgbEvaluator());
    colorAnim.setRepeatCount(100);
    colorAnim.setRepeatMode(ValueAnimator.REVERSE);
    colorAnim.start();
}
public void startColorAnimation1 (View view) {
    int colorStart = 0xfff53b6d;
    // int colorStart = view.getSolidColor();
    int colorEnd = 0xFF000000;

    ValueAnimator colorAnim=ObjectAnimator.ofInt(
            view, "backgroundColor", colorStart, colorEnd);
    colorAnim.setDuration(2000);
    colorAnim.setEvaluator(newArgbEvaluator());
    colorAnim.setRepeatCount(100);
    colorAnim.setRepeatMode(ValueAnimator.REVERSE);
    colorAnim.start();
}

android android-animation

2022-09-30 20:47

1 Answers

Why don't you have ValueAnimator as a class field instead of as local to the method, and do nothing to get out of the method if the ValueAnimator object already exists?

public class MyActivity extensions Activity {

  ValueAnimator anim;

  protected void onCreate() {
    super.onCreate()
    setContentView(R.layout.activity_main);

    Button btnStart=(Button) findViewById (R.id.start);
    btnStart.setOnClickListener(newView.OnClickListener(){

      @ Override
      public void onClick (View v) {
        startColorAnimation (image);
      }
    });
  }

  public void startColorAnimation (View view) {

    // If the animation is already running, do nothing.
    if(anim!=null)return;

    int colorStart = 0xFFffff;
    int colorEnd = 0xFF000000;

    anim=ObjectAnimator.ofInt(view, "backgroundColor", colorStart, colorEnd);
    anim.setDuration (2000);
    anim.setEvaluator(newArgbEvaluator());
    anim.setRepeatCount(100);
    anim.setRepeatMode(ValueAnimator.REVERSE);
    anim.start();
  }
}

*Partial omitted

In addition, you can only run animation once as above, so you must do anim=null somewhere to be able to run animation again.


2022-09-30 20:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.