动画不重复的点击图片(Animation Not Repeating on Click of Ima

2019-07-01 16:22发布

在点击一个的Textview我在做什么使用下面的代码段帧动画

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

和点击的TextView ,我使用启动动画ribinclickanimation.start();

clickframeanimation是动画文件。 和ribinclickanimation是对象AnimationDrawable它可以正常工作,第一次当我点击,但是当我点击第二次起什么也没有发生任何人都可以建议我的任何帮助。

* 验证码:*

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>

Answer 1:

这里是更新代码

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();

        }
    }

}


文章来源: Animation Not Repeating on Click of Image