Animation Not Repeating on Click of Image

2019-02-20 01:37发布

问题:

On Click of a Textview i am doing frame animation using following piece of code

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

and on Click of the TextView i am starting the animation using ribinclickanimation.start();

clickframeanimation is the anim file. and ribinclickanimation is the object of AnimationDrawable It works properly for the first time when i click but when i click for the second time onwards nothing happens can anyone suggest me any help.

*CODE: *

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

        }
    }
}

and my clickframeanimation.xml is:

<?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>

回答1:

Here is the update code

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

        }
    }

}