【问题标题】:AnimationDrawable on existing ImageView will not animate现有 ImageView 上的 AnimationDrawable 不会设置动画
【发布时间】:2014-03-28 17:21:16
【问题描述】:

我有以下代码用于在应用另一个动画后向 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"?>
<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>

这里是对应的布局文件

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

【问题讨论】:

    标签: android android-layout animationdrawable


    【解决方案1】:

    试试这个

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

    【讨论】:

      【解决方案2】:

      希望这会有所帮助:

      @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
          ...
      }
      

      【讨论】:

      • 不幸的是它没有帮助。初始动画(脉动)刚刚重新开始,但不是 R.drawable.record_button_animation 中定义的动画)
      猜你喜欢
      • 2012-01-16
      • 2012-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      相关资源
      最近更新 更多