完成第一个后启动DrawableAnimation

时间:2014-08-13 01:04:06

标签: android animation imageview drawable

如何在第一次完成后启动drawableAnimation?据我所知,没有DrawableAnimation的监听器,所以我知道我必须使用最后一帧来启动第二个DrawableAnimation。但我不明白该怎么做。

public class MainActivity extends ActionBarActivity {

Button button;
private AnimationDrawable frameAnimation;
private ImageView view;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    addListenerOnButton();
        // Type casting the Image View
        view = (ImageView) findViewById(R.id.imagen);
        // Setting animation_list.xml as the background of the image view
        view.setBackgroundResource(R.drawable.entrada01);
        // Type casting the Animation drawable
        frameAnimation = (AnimationDrawable) view.getBackground();
            }

2 个答案:

答案 0 :(得分:0)

试试吧。 它可能适合你。

public class AndroidAnimationActivity extends Activity {

 AnimationDrawable myAnimationDrawable;

 Timer timer;
 MyTimerTask myTimerTask;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageView myAnimation = (ImageView)findViewById(R.id.myanimation);
        myAnimationDrawable 
         = (AnimationDrawable)myAnimation.getDrawable();

        myAnimation.post(
          new Runnable(){

     @Override
     public void run() {
      myAnimationDrawable.start();
     }
          });

        //Calculate the total duration
        int duration = 0;
        for(int i = 0; i < myAnimationDrawable.getNumberOfFrames(); i++){
         duration += myAnimationDrawable.getDuration(i);
        }

        timer = new Timer();
        myTimerTask = new MyTimerTask();
        timer.schedule(myTimerTask, duration);
    }

    class MyTimerTask extends TimerTask {

  @Override
  public void run() {

   timer.cancel();
   runOnUiThread(new Runnable(){
    @Override
    public void run() {
     //Start Animation here
     Toast.makeText(getApplicationContext(), 
       "Animation Stopped", 
       Toast.LENGTH_SHORT).show(); 
    }});
  }  
 }
}

您还可以使用xml文件

应用多个drawableAnimation

就好像你有四个drawableAnimation文件 anim1.xml,anim2.xml,anim3.xml和anim4.xml

然后在使用xml文件

完成动画后开始下一个动画

注意:所有动画 anim1.xml,anim2.xml,anim3.xml和anim4.xml 都是android:oneshot="true" ,否则它可能无效

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true" >

    <item
        android:drawable="@anim/anim1"
        android:duration="500"/>
    <item
        android:drawable="@anim/anim2"
        android:duration="50"/>
    <item
        android:drawable="@anim/anim3"
        android:duration="1050"/>
    <item
        android:drawable="@anim/anim4"
        android:duration="50"/>


</animation-list>

答案 1 :(得分:0)

我认为在完成第一个DrawableAnimation后我找到了运行DrawableAnimation的方法。我知道动画有多长,所以我花时间开始第二个动画。我想我可以计算这个时间,但是这样做很麻烦。

// Start Animation
Handler nHandler = new Handler(); 
nHandler.postDelayed(new Runnable() {
public void run() {
runOnUiThread(new Runnable(){
@Override
public void run() {             
view = (ImageView) findViewById(R.id.imagen);
view.setBackgroundResource(R.drawable.entrada03);
frameAnimation = (AnimationDrawable) view.getBackground(); 
frameAnimation.start();
    // Start Animation 
    Handler nHandler = new Handler(); 
    nHandler.postDelayed(new Runnable() {
    public void run() {
    runOnUiThread(new Runnable(){
    @Override
    public void run() {             
    view = (ImageView) findViewById(R.id.imagen);
    view.setBackgroundResource(R.drawable.entrada04);
    frameAnimation = (AnimationDrawable) view.getBackground(); 
    frameAnimation.start();
    }});}}, 600);
}});}}, 600);

这段代码对我有用,但我发现了其他一些问题。如果在运行第一个动画时按下按钮,按钮会执行操作,但会被第二个动画中断。所以,我一直在努力改进这段代码。

感谢您的帮助@ prakash-ubhadiya