2013-11-26 114 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翻译。

这样,您可以使用绝对位置进行翻译。尽管你可以使用顶部坐标来代替视图。