【问题标题】:Frame Animations only start one time帧动画只开始一次
【发布时间】:2017-02-03 01:57:21
【问题描述】:

我通过下面的代码创建了帧动画。我希望每次单击按钮时动画都会开始,但它仅在第一次工作

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/frame"
        />

    <Button
        android:layout_centerInParent="true"
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Animation"
        />

</RelativeLayout>

frame.xml

<animation-list
    android:oneshot="true"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <item android:drawable="@drawable/ic_heart_0" android:duration="300" />
    <item android:drawable="@drawable/ic_heart_50" android:duration="300" />
    <item android:drawable="@drawable/ic_heart_75" android:duration="300" />
    <item android:drawable="@drawable/ic_heart_100" android:duration="300" />
    <item android:drawable="@drawable/ic_heart_0" android:duration="300" />
</animation-list>

活动

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startAnimation();
            }
        });
    }

    public void startAnimation(){
         ImageView img = (ImageView) findViewById(R.id.image);
        ((AnimationDrawable) img.getBackground()).start();
    }
}

如何让动画在我点击按钮时开始播放? 任何帮助或建议将不胜感激。

【问题讨论】:

    标签: android animationdrawable


    【解决方案1】:

    您在MainActivity 中的 300 持续时间后尝试stop() 动画。我认为它无法识别您是否停止。 300以后就停了吧?但我认为也许应用程序识别它仍然启动。

    【讨论】:

    • 谢谢,我认为手动停止动画可以让动画重新开始
    【解决方案2】:

    这是我找到的一种解决方案。如果动画正在运行,我会尝试停止动画,然后再重新启动它。我认为动画即使经过所有帧也不会停止

    if(((AnimationDrawable) imageView.getBackground()).isRunning()){
          ((AnimationDrawable) imageView.getBackground()).stop();
    }
    ((AnimationDrawable) imageView.getBackground()).start();
    

    【讨论】:

      【解决方案3】:

      为了再次运行“一次性动画”:

      private void runAnimation(ImageView imageView) {
          AnimationDrawable animation = (AnimationDrawable) imageView.getBackground();
          animation.setVisible(true, true);
          animation.start();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-11
        • 2018-05-14
        • 2021-05-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-10
        • 1970-01-01
        相关资源
        最近更新 更多