2011-10-12 100 views
3

我正在嘗試動畫ImageButton。點擊該按鈕後,我希望它向右滑動。當它再次被點擊時,它應該滑回原始位置。Android幻燈片動畫閃爍

當動畫結束時,它會回到原來的位置,因此我重新定位ImageButton。我有一個小的「閃光燈」,在ImageButton重新定位之前,ImageButton和動畫都處於原始位置。任何人都可以告訴我如何擺脫這個「閃光」?

我寫了下面的代碼來開始動畫,當動畫結束時,移動ImageButton本身。 它是什麼,是一個用於滑動ImageButton的onClickListener和一個用於此的AnimationListener。還有一個onClickListener和一個用於將ImageButton滑回的AnimationListener。

private class SceneIndicatorListenerIn implements ImageButton.OnClickListener { 
    ImageButton imageButton; 
    public SceneIndicatorListenerIn (ImageButton imageButton) { 
     this.imageButton = imageButton; 
    } 

    @Override 
    public void onClick(View view) { 
     // Create animation 
     Animation anim = AnimationUtils.loadAnimation(context, R.anim.sceneindicator_in); 
     anim.setAnimationListener(new SceneIndicatorListenerInDidEnd(imageButton)); 
     view.startAnimation(anim); 
    } 
} 

private class SceneIndicatorListenerInDidEnd implements AnimationListener { 
    ImageButton imageButton; 
    public SceneIndicatorListenerInDidEnd (ImageButton imageButton) { 
     this.imageButton = imageButton; 
    } 

    @Override 
    public void onAnimationEnd(Animation anim) { 
     Log.d(LOG_TAG, "In animation did end"); 

     // This is for density pixels 
     float dp = context.getResources().getDisplayMetrics().density; 

     // Keep position after animation 
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(imageButton.getWidth(), imageButton.getHeight()); 
     params.setMargins((int) (0 * dp), imageButton.getTop(), 0, 0); 
     imageButton.setLayoutParams(params); 

     // Change on click listener 
     imageButton.setOnClickListener(new SceneIndicatorListenerOut(imageButton)); 
    } 

    @Override 
    public void onAnimationRepeat(Animation arg0) {} 

    @Override 
    public void onAnimationStart(Animation arg0) {} 
} 

private class SceneIndicatorListenerOut implements ImageButton.OnClickListener { 
    ImageButton imageButton; 
    public SceneIndicatorListenerOut (ImageButton imageButton) { 
     Log.d(LOG_TAG, "My imageButton was set"); 
     this.imageButton = imageButton; 
    } 

    @Override 
    public void onClick(View view) { 
     Log.d(LOG_TAG, "You clicked me"); 

     // Create animation 
     Animation anim = AnimationUtils.loadAnimation(context, R.anim.sceneindicator_out); 
     anim.setAnimationListener(new SceneIndicatorListenerOutDidEnd(imageButton)); 
     view.startAnimation(anim); 
    } 
} 

private class SceneIndicatorListenerOutDidEnd implements AnimationListener { 
    ImageButton imageButton; 
    public SceneIndicatorListenerOutDidEnd (ImageButton imageButton) { 
     this.imageButton = imageButton; 
    } 

    @Override 
    public void onAnimationEnd(Animation anim) { 
     Log.d(LOG_TAG, "Out animation did end"); 

     // This is for density pixels 
     float dp = context.getResources().getDisplayMetrics().density; 

     // Keep position after animation 
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(imageButton.getWidth(), imageButton.getHeight()); 
     params.setMargins((int) (-199 * dp), imageButton.getTop(), 0, 0); 
     imageButton.setLayoutParams(params); 

     // Change on click listener 
     imageButton.setOnClickListener(new SceneIndicatorListenerIn(imageButton)); 
    } 

    @Override 
    public void onAnimationRepeat(Animation arg0) {} 

    @Override 
    public void onAnimationStart(Animation arg0) {} 
} 

這是我在滑動動畫:

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <translate android:fromXDelta="0%" 
       android:toXDelta="83%" 
       android:duration="750" /> 
</set> 

,這是我的滑回動畫:

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <translate android:fromXDelta="0%" 
       android:toXDelta="-80%" 
       android:duration="750" /> 
</set> 

回答

0

我有同樣的問題,並想後的答案任何遇到此問題的人。無論您使用xml或其他方式設置,都會導致此錯誤。

我得到的方法是在動畫之前手動將X位置設置爲離開屏幕,以便閃光燈會在屏幕上發生,然後將其設置回原來的位置動畫。

image.setX(-5000);//Really any number off of the screen will do 

Animation animation0 = AnimationUtils.loadAnimation(this, 
      R.anim.slide_across_right); 
animation0.setAnimationListener(new AnimationListener() { 

     @Override 
     public void onAnimationStart(Animation animation) { 
      animationHack(); 
     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 
     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 
     } 
}); 



private void animationHack() { 
    (new Handler()).postDelayed(new Runnable() { 
     public void run() { 
      image.setX(0); 
     } 
    }, 20); 

}