android - 在每个 onClick 调用上运行相同的帧动画

标签 android imageview android-animation frames

我试图在单击ImageView时使用AnimationDrawable显示帧到帧动画。但当我单击 ImageView 时,我的动画仅发生一次。我使用 xml 来制作动画。

动画 XML:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true">
  <item android:drawable="@drawable/scorepopup1" android:duration="100" />
  <item android:drawable="@drawable/scorepopup2" android:duration="100" />
  <item android:drawable="@drawable/scorepopup3" android:duration="100" />
  <item android:drawable="@drawable/scorepopup4" android:duration="100" />
  <item android:drawable="@drawable/invipopup" android:duration="100" />

</animation-list>

这是我的按钮和动画图像容器的 xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="70dp"
    android:layout_height="50dp"
    android:layout_alignParentRight="true" 
    android:layout_margin="5dp">

    <ImageView
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:background="@drawable/bd"
    android:layout_margin="10dp"
    android:onClick="checkGem" 
    android:contentDescription="bd"/>


    <ImageView
        android:id="@+id/scorePopup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp" />
</LinearLayout>

检查 gem 方法:

ImageView popup1 =  (ImageView) findViewById(R.id.scorePopup);

popup1.setBackgroundResource(R.drawable.popup1_animation);
AnimationDrawable frameAnimation2 = (AnimationDrawable) 

popup1.getBackground();
frameAnimation2.start();

我的问题是动画仅显示一次,但我希望每次单击时它都会显示动画。有什么想法吗?

最佳答案

start() 之前的 ImageViewAnimationDrawable 背景上调用 stop()调用:

//...
AnimationDrawable frameAnimation2 = (AnimationDrawable) popup1.getBackground();
frameAnimation2.stop();
frameAnimation2.start();

当您第一次单击ImageView时,动画将会运行,但最后它会发现自己处于它认为仍在运行的状态。在这种状态下,调用 start() 将被忽略。 stop() 方法“重置”,表明 AnimationDrawable 将再次从头开始运行。

关于android - 在每个 onClick 调用上运行相同的帧动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14627677/

相关文章:

android - $http 请求状态 0 和 https

android - android View 的高度动画

android - 带有 setStartOffset 的 AnimationSet 不起作用

android - 在正在进行的 FragmentTransaction 期间更改 Fragments 的 z 顺序

java - 集成 Localytics - FragmentActivity

java - 使用 ResideMenu lib 时删除菜单项

安卓磨损 : Starting a service on Handheld

android - 图像未从 ListView 上的 URL 加载

android - 使用 XML 布局时如何覆盖 ImageView 的 OnDraw?

android - 将 ImageView 背景设置为位图