android - 单击图像时动画不重复

标签 android android-animation

在点击 Textview 时,我正在使用以下代码制作帧动画

imgclickanimation.setBackgroundResource(R.anim.clickframeanimation);
ribinclickanimation= (AnimationDrawable) imgclickanimation.getBackground();

然后单击 TextView 我开始使用动画 ribinclickanimation.start();

clickframeanimation 是动画文件。 而ribinclickanimationAnimationDrawable的对象 当我第一次点击时它工作正常但是当我第二次点击时没有任何反应任何人都可以建议我任何帮助。

*代码:*

package com.example.tryfinal;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener 
{
    TextView imgclickanimation;
    AnimationDrawable ribinclickanimation;//,ribinanimation;
    int duration=200;
    ScrollView scroll;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imgclickanimation= (TextView) findViewById(R.id.imgclickanimation);



        imgclickanimation.setOnClickListener(this);
        imgclickanimation.setBackgroundResource(R.anim.clickframeanimation);
        ribinclickanimation= (AnimationDrawable) imgclickanimation.getBackground();
    }
    public void onClick(View arg0) 
    {
        if(arg0.getId()==imgclickanimation.getId())
        {
            imgclickanimation.setBackgroundResource(R.anim.clickframeanimation);
            ribinclickanimation.start();

        }
    }
}

我的 clickframeanimation.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/ribin3" android:duration="200" />
    <item android:drawable="@drawable/ribin4" android:duration="200" />
    <item android:drawable="@drawable/ribin3" android:duration="200" />
</animation-list>

最佳答案

这是更新代码

package com.example.tryfinal;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ScrollView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

 TextView imgclickanimation;
    AnimationDrawable ribinclickanimation;//,ribinanimation;
    int duration=200;
    ScrollView scroll;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imgclickanimation= (TextView) findViewById(R.id.imgclickanimation);
        imgclickanimation.setOnClickListener(this);
        imgclickanimation.setBackgroundResource(R.anim.clickframeanimation);
        ribinclickanimation= (AnimationDrawable) imgclickanimation.getBackground();
    }
    public void onClick(View arg0) 
    {
        ribinclickanimation.stop();
        if(arg0.getId()==imgclickanimation.getId())
        {
            imgclickanimation.setBackgroundResource(R.anim.clickframeanimation);//It will still work without this line. There is no need to set the resource again.

            ribinclickanimation.start();

        }
    }

}

关于android - 单击图像时动画不重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13006157/

相关文章:

android - Google Play 测试版应用不在主屏幕上

android - react-native-keyboard-aware-scroll-view 无法正常工作

android - Android 中启动前序列动画卡住

android - 哪个 LayoutManager 用于 2048 游戏的动画?

android-animation - 如何从上到下转换事件,反之亦然?

javascript - Android从asset文件夹加载加密的HTML+JS到webview

android - PhoneGap/Ionic 如何处理来自 Parse 的推送通知?

android - 为什么不在Android中直接实例化服务呢?

android - 从左向右移动图像然后旋转动画

android - 在 ViewPager 和 setFillAfter 上使用动画