2013-11-26 67 views
1

我想通過翻譯動畫將圖像按鈕翻譯到另一個位置(位置到另一個圖像按鈕)。我有他們的兩個職位。我擁有的動畫方法都是使用相對位置變化,但我對絕對翻譯感興趣。在Android中有沒有辦法做到這一點?動畫無法正常工作?

public void startAnimationFindChant(View view) { 
      int[] locationSource = new int[2]; 
      int[] locationDestination = new int[2]; 
      view.geLocationOnScreen(locationSource); 
      Log.d("locationSource",locationSource[0]+" "+locationSource[1]); 
      ImageButton findachant1 = (ImageButton) findViewById(R.id.findachanticon1); 

      findachant1.getLocationOnScreen(locationDestination); 
      Log.d("locationDestination",locationDestination[0]+" "+locationDestination[1]); 

      TranslateAnimation animation = new TranslateAnimation(
        Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, 
        Animation.ABSOLUTE, locationSource[1], Animation.ABSOLUTE, 
        locationDestination[1]); 
      animation.setFillAfter(true); 
      view.startAnimation(animation); 
     } 

回答

0
public void doSlideUp(View view) { 
    LinearLayout myView = (LinearLayout) findViewById(R.id.content_area); 
    Animation slideUp = setLayoutAnim_slideup(); 
    myView.startAnimation(slideUp); 

} 

public void doSlideDown(View view) { 
    RelativeLayout myView = (RelativeLayout) findViewById(R.id.content_area1); 
    Animation slideDown = setLayoutAnim_slidedown(); 
    myView.startAnimation(slideDown); 
} 

public Animation setLayoutAnim_slidedown() { 

    AnimationSet set = new AnimationSet(true); 

    Animation animation = new TranslateAnimation(
      Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 
      0.0f, Animation.RELATIVE_TO_SELF, -1.0f, 
      Animation.RELATIVE_TO_SELF, 0.0f); 
    animation.setDuration(2000); 
    animation.setAnimationListener(new AnimationListener() { 

     @Override 
     public void onAnimationStart(Animation animation) { 
     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 
     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 

     } 
    }); 
    set.addAnimation(animation); 

    LayoutAnimationController controller = new LayoutAnimationController(
      set, 0.25f); 

    return animation; 
} 

public Animation setLayoutAnim_slideup() { 

    AnimationSet set = new AnimationSet(true); 

    Animation animation = new TranslateAnimation(
      Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 
      0.0f, Animation.RELATIVE_TO_SELF, 0.0f, 
      Animation.RELATIVE_TO_SELF, -1.0f); 
    animation.setDuration(2000); 
    animation.setAnimationListener(new AnimationListener() { 
     @Override 
     public void onAnimationStart(Animation animation) { 

     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 

     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 




     } 
    }); 
    set.addAnimation(animation); 
    LayoutAnimationController controller = new LayoutAnimationController(
      set, 0.25f); 
    return animation; 
} 

試試這個,因爲你需要的動畫X ASIX值更改爲Y軸。

+0

我想OP詢問了兩個職位之間的絕對翻譯。 – noob

+0

是的,我想要兩個職位之間的絕對翻譯。幫我。 –

+0

對我來說它不起作用 –

0

我已經使用ObjectAnimator製成用於TranslateAnimation一個簡單的類之間的兩個位置 -

private void translateAnimateView(View source, View target, 
     AnimatorListener listener) { 
    ObjectAnimator animator = ObjectAnimator.ofFloat(source, 
      "translationY", 0, target.getTop() - source.getTop()); 
    animator.addListener(listener); 

    animator.setDuration(1000); 
    animator.start(); 
} 

該方法接受threee參數,源視圖,目標視圖和監聽器(可以爲空)。源將被動畫以達到目標視圖。我只添加了Y翻譯,但實際上也可以包含X翻譯。

這樣,您可以使用絕對位置進行翻譯。儘管你可以使用頂部座標來代替視圖。