Android 现有ImageView上的AnimationDrawable将不会设置动画

Android 现有ImageView上的AnimationDrawable将不会设置动画,android,android-layout,animationdrawable,Android,Android Layout,Animationdrawable,我有以下代码,用于在应用另一个动画后将动画添加到ImageView 动画从未开始。以下是相关代码: // I'm Using Jake Wharton's ButterKnife,a view "injection" library. @InjectView(R.id.record_button) public ImageView recordButtonView; protected void onCreate(Bundle savedInstance) { ... But

我有以下代码,用于在应用另一个动画后将动画添加到ImageView

动画从未开始。以下是相关代码:

// 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
    ...
}
这是record_button_animation.xml(保存在res/drawable上)


下面是相应的布局文件

<?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>

希望这有助于:

@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
    ...
}
试试这个

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();
          }
    });

不幸的是,这没有帮助。初始动画(脉动)只是再次开始,但不是R.drawable.record_按钮_动画中定义的动画)
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();
          }
    });