AnimationDrawable on existing ImageView will not animate

I have the following code for adding an animation to an ImageView after applying another animation.

The animation never starts. Here is the related code:

// I'm Using Jake Wharton's ButterKnife,a view "injection" library.
@InjectView(R.id.record_button)
public ImageView recordButtonView;

protected void onCreate(Bundle savedInstance) {
    ... 
    ButterKnife.inject(this);
    pulsate = AnimationUtils.loadAnimation(this.parent, R.anim.pulsate);
    recordButtonView.startAnimation(pulsate)
    ...
}

@OnClick(R.id.record_button)   
protected void onButtonClick( ) {
    ... 
    // Clear the old animation
    recordButtonView.clearAnimation();

    // Start the new animation
    recordButtonView.setBackgroundResource(R.drawable.record_button_animation);
    AnimationDrawable recordButtonAnimation = (AnimationDrawable) recordButtonView.getBackground();
    recordButtonAnimation.setCallback(recordButtonView);
    recordButtonAnimation.setVisible(true, true);
    recordButtonAnimation.start();

    // At this point the animation should be looping.  Nothing happens
    ...
}

Here is record_button_animation.xml (saved on res/drawable)

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
    <item android:drawable="@drawable/sing_btn_record" android:duration="500"/>
    <item android:drawable="@drawable/sing_btn_record_activated" android:duration="500"/>
</animation-list>

Here is the corresponding layout file

<?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <!-- all sibling elements are removed for clarity -->

  <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_above="@+id/stem">

      <!-- all sibling elements are removed for clarity -->

      <ImageView
          android:id="@+id/record_button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/sing_btn_record"
          android:scaleType="center"
          android:layout_centerInParent="true"/>
  </RelativeLayout>
</RelativeLayout>

Answers


Hope this helps:

@OnClick(R.id.record_button)   
protected void onButtonClick( ) {
    ... 
    // Clear the old animation
    recordButtonView.clearAnimation();

    // Start the new animation
    recordButtonView.setBackgroundResource(R.drawable.record_button_animation);
   // Following line worked for me
    recordButtonView.setAnimation(pulsate);   

    // At this point the animation should be looping.  Nothing happens
    ...
}

Try this

ImageView recordButton = (ImageView) findViewById(R.id.imageViewPulse);
    recordButton.setImageBitmap(null);
    recordButton.setBackgroundResource( R.anim.pulse );
    final AnimationDrawable mailAnimation = (AnimationDrawable) recordButton.getBackground();
    recordButton.post(new Runnable() {
        public void run() {
            if ( mailAnimation != null ) mailAnimation.start();
          }
    });

Need Your Help

How can I provide a script for PHP CLI via composer (as standalone, and as dependency)

php composer-php command-line-interface

I am attempting to write a PHP script that I would like to run from the command line. I want to use composer to manage its dependencies, as well as to make it available to be installed as a depende...

Why is my CoreData info erased when changing the attributes of an entity?

ios objective-c cocoa-touch core-data

Every time I add an attribute to one of my CoreData entities and run the app, I get an exception and everything in CoreData (all objects I created) is deleted.