我想通过翻译动画将图像按钮翻译到另一个位置(位置到另一个图像按钮)。我有两个职位。我所拥有的动画方法都使用相对位置变化,但我对绝对翻译感兴趣。在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 :(得分: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轴的值更改为y轴。
答案 1 :(得分: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();
}
此方法接受三个参数,源视图,目标视图和侦听器(可以为null)。源将被动画化以达到目标视图。我刚刚添加了Y翻译,但你实际上也可以包含x翻译。
这样您就可以使用绝对位置进行翻译。你可以使用顶部坐标而不是视图。