Android Animation 幀動畫監聽結束的方法 – Android移動開發技術文章_手機開發 Android移動開發教學課程

 

這個問題用中文搜瞭很久一直沒有解決,今天嘗試用英文在Google上搜瞭一下,打開第一個網頁就把困擾的很久的問題解決瞭

 

 

問題描述:幀動畫連續播放–android 幀動畫沒有提供結束動作的監聽方法,怎麼實現播放完一個動畫之後接著播放第二個動畫呢?

 

 

正確方法1:

 

 

public void mmswoon(){ 

        imageView.setBackgroundResource(R.anim.mmswoon); 

        AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground(); 

        animationDrawable.start(); 

         

        int duration = 0; 

        for(int i=0;i<animationDrawable.getNumberOfFrames();i++){ 

            duration += animationDrawable.getDuration(i); 

        } 

        Handler handler = new Handler(); 

        handler.postDelayed(new Runnable() { 

            public void run() { 

               //此處調用第二個動畫播放方法   

            } 

        }, duration); 

         

    } 

duration是第一個動畫播放的總時間

 

PS :自己試過瞭,可以實現效果

正確方法2:

 

 

this.q_pic_view.setImageResource(0); 

    this.q_pic_view.setBackgroundResource(R.drawable.animation_test); 

    AnimationDrawable correct_animation = (AnimationDrawable) this.q_pic_view.getBackground(); 

    correct_animation.start(); 

 

    //here I tried to implement option C but it didn't work 

    while(correct_animation.isRunning()){ 

        try { 

           Thread.sleep(20); 

        } catch (InterruptedException e) { 

           // TODO Auto-generated catch block 

           e.printStackTrace(); 

        } 

    } 

這個方法沒有試過

 

摘自 wangxin的專欄

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *