[英]How to play animation drawable more than once in Android?
我有一个来自png的可绘制动画,而android:oneshot =“ true”是因为我不希望动画不断播放,而只是在我激活它时才会播放。 问题是它只能播放一次,而当我尝试myAnimation.play();
它不会再播放。
我已经尝试过myAnimation.stop();
并再次播放,但这会使动画在动画结束之前停止播放。
当我使用myAnimation.run();
启动动画时, myAnimation.run();
发生相同的事情myAnimation.run();
,尽管我不知道有什么区别。
//in onCreate() method
imageView = findViewById(R.id.imageView);
imageView.setBackgroundResource(R.drawable.animation_drawable);
myAnimation = (AnimationDrawable) imageView.getBackground();
//Triggers in somewhere else in a thread
myAnimation.start();
//animation_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/animation_drawable" android:oneshot="true">
<item android:drawable="@drawable/kapali" android:duration="0"/>
<item android:drawable="@drawable/acik" android:duration="500"/>
<item android:drawable="@drawable/kapali" android:duration="0"/>
</animation-list>
在animation_drawable.xml
您具有android:oneshot="true"
,将其删除或将其更改为false
。
尝试使用
myAnimation.setOneShot(false);
在start()方法之前。
而当您想停止动画使用时
myAnimation.stop();
对于您的情况,在停止动画(或设置oneshot = true)后,要重新启动动画,请使用
myAnimation.setVisible(/*visible=*/true,/*restart=*/true);
您可以在此处查看有关此方法的文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.