2012-10-16 47 views
5

需要做一個動畫(在Android 2.2及以上):從上到下 - 翻譯動畫

從上到下1.Moving按鈕(點擊之後他),

從2.Moving回自下而上(再次點擊他之後)。

第一個動畫工作正常,但第二個不是,btn從底部到頂部「跳」,而不是動畫。

代碼:

public class MainActivity extends Activity { 

static RelativeLayout relativeLayout; 
static Button btn; 
static Boolean isUp = true; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    btn = (Button) findViewById(R.id.button1); 
    relativeLayout = (RelativeLayout) findViewById(R.id.relative_layout); 

    btn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      if(isUp){ 
       isUp = false; 
       v.startAnimation(MainActivity.getVerticalSlideAnimation(0,relativeLayout.getBottom() - v.getHeight(),500,0)); 
      }else{ 
       isUp = true; 
       v.startAnimation(MainActivity.getVerticalSlideAnimation(relativeLayout.getBottom() - v.getHeight(),0,500,0)); 
      } 
     } 
    }); 
} 


public static Animation getVerticalSlideAnimation(int fromYPosition, final int toYPosition, int duration, int startOffset) 
{ 
    TranslateAnimation translateAnimation = new TranslateAnimation(1, 0.0F, 1, 0.0F, 0, fromYPosition, 0, toYPosition); 
    translateAnimation.setDuration(duration); 
    translateAnimation.setInterpolator(new AccelerateInterpolator()); 
    translateAnimation.setStartOffset(startOffset); 

    //Stop animation after finishing. 
    //translateAnimation.setFillAfter(true); 

    translateAnimation.setAnimationListener(new AnimationListener() 
    { 
    public void onAnimationStart(Animation animation) { } 
    public void onAnimationRepeat(Animation animation) { } 
    public void onAnimationEnd(Animation animation) { 
     btn.setY(toYPosition);   
    } 
    }); 

    return translateAnimation; 
    } 
} 

佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/relative_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:text="Button" /> 

</RelativeLayout> 

回答

10

好吧,我解決了這個問題。

有你應該知道的一些動畫issuses:

  1. 動畫paremeters都不是簡單的「從(固定位置)」 - >「將(固定位置)」你應該想到。有更多像「從(當前位置/ 0)」 - >「要做多少步驟和在哪個方向(正/負負脈衝)」

  2. 動畫不會改變真實位置屏幕上的看法,因此,如果你想停在終點位置的動畫,你應該使用:

    animation.setFillAfter(true); 
    
  3. 如果你想改變視圖的實際位置,你應該更新視圖參數onAnimationEnd(如下面的代碼),或者手動計算位置並設置Y/X位置(同樣在onAnimationEnd上),如:

    animatedView.setY(stopPosition); 
    

驗證碼:

public class AnimationActivity extends Activity { 

    private boolean isUp; 

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

    ((Button) findViewById(R.id.button1)) 
      .setOnClickListener(new OnClickListener() { 

       public void onClick(final View v) { 

        final float direction = (isUp) ? -1 : 1; 
        final float yDelta = getScreenHeight() - (2 * v.getHeight()); 
        final int layoutTopOrBottomRule = (isUp) ? RelativeLayout.ALIGN_PARENT_TOP : RelativeLayout.ALIGN_PARENT_BOTTOM; 

        final Animation animation = new TranslateAnimation(0,0,0, yDelta * direction); 

        animation.setDuration(500); 

        animation.setAnimationListener(new AnimationListener() { 

         public void onAnimationStart(Animation animation) { 
         } 

         public void onAnimationRepeat(Animation animation) { 
         } 

         public void onAnimationEnd(Animation animation) { 

          // fix flicking 
          // Source : http://stackoverflow.com/questions/9387711/android-animation-flicker 
          TranslateAnimation anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, 0.0f); 
          anim.setDuration(1); 
          v.startAnimation(anim); 


          //set new params 
          LayoutParams params = new LayoutParams(v.getLayoutParams()); 
          params.addRule(RelativeLayout.CENTER_HORIZONTAL); 
          params.addRule(layoutTopOrBottomRule); 
          v.setLayoutParams(params); 
         } 
        }); 

        v.startAnimation(animation); 

        //reverse direction 
        isUp = !isUp; 
       } 
      }); 
} 

private float getScreenHeight() { 

    DisplayMetrics displaymetrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
    return (float) displaymetrics.heightPixels; 

} 

}

+0

它是真正有用的。我想知道,如何從頂部連續每次獲取其位置移動,以便底部它從每一步移到下一步。如何使用動畫來實現它?他們有其他方法嗎?我的最終目的是檢測移動視圖和此視圖之間的碰撞?感謝您的幫助 – Ajay

-1
public class AnimationActivity extends Activity { 

    private boolean isUp; 

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

    ((Button) findViewById(R.id.button1)) 
      .setOnClickListener(new OnClickListener() { 

       public void onClick(final View v) { 

        final float direction = (isUp) ? -1 : 1; 
        final float yDelta = getScreenHeight() - (2 * v.getHeight()); 
        final int layoutTopOrBottomRule = (isUp) ? RelativeLayout.ALIGN_PARENT_TOP : RelativeLayout.ALIGN_PARENT_BOTTOM; 

        final Animation animation = new TranslateAnimation(0,0,0, yDelta * direction); 

        animation.setDuration(500); 

        animation.setAnimationListener(new AnimationListener() { 

         public void onAnimationStart(Animation animation) { 
         } 

         public void onAnimationRepeat(Animation animation) { 
         } 

         public void onAnimationEnd(Animation animation) { 

          // fix flicking 
          // Source : http://stackoverflow.com/questions/9387711/android-animation-flicker 
          TranslateAnimation anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, 0.0f); 
          anim.setDuration(1); 
          v.startAnimation(anim); 


          //set new params 
          LayoutParams params = new LayoutParams(v.getLayoutParams()); 
          params.addRule(RelativeLayout.CENTER_HORIZONTAL); 
          params.addRule(layoutTopOrBottomRule); 
          v.setLayoutParams(params); 
         } 
        }); 

        v.startAnimation(animation); 

        //reverse direction 
        isUp = !isUp; 
       } 
      }); 
} 

private float getScreenHeight() { 

    DisplayMetrics displaymetrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
    return (float) displaymetrics.heightPixels; 

} 
+0

你可以簡單地設置onClickListener而不是強制轉換到按鈕 –

+0

你剛剛從上面複製了代碼並粘貼了它.... – Denny