簡體   English   中英

如何在Android中多次播放可繪制的動畫?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM