所以,事实证明这比我想象的要容易得多。
我创建了一个全屏的RelativeLayout,在动画发生时只显示它。
我通过以下方式获取我的隐藏按钮的起始位置(在Java中看到这些C风格的编码机制很有趣,这些机制如今已经相当少见):
int fromLoc[] = new int[2];
v.getLocationOnScreen(fromLoc);
float startX = fromLoc[0];
float startY = fromLoc[1];
现在,我有了起点。
我的终点是屏幕上的一个绝对坐标,您可以按任意方式分配它
然后,我创建了一个小的Animations帮助类,让我可以传入所有坐标、回调和动画的持续时间
public class Animations {
public Animation fromAtoB(float fromX, float fromY, float toX, float toY, AnimationListener l, int speed){
Animation fromAtoB = new TranslateAnimation(
Animation.ABSOLUTE,
fromX,
Animation.ABSOLUTE,
toX,
Animation.ABSOLUTE,
fromY,
Animation.ABSOLUTE,
toY
);
fromAtoB.setDuration(speed);
fromAtoB.setInterpolator(new AnticipateOvershootInterpolator(1.0f));
if(l != null)
fromAtoB.setAnimationListener(l);
return fromAtoB;
}
}
我们需要一个监听器来告诉我们动画何时完成以清除它
AnimationListener animL = new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
clearAnimation();
}
};
最后,我们把所有东西放在一起并按下开始按钮。
int fromLoc[] = new int[2];
v.getLocationOnScreen(fromLoc);
float startX = fromLoc[0];
float startY = fromLoc[1];
RelativeLayout rl = ((RelativeLayout)findViewById(R.id.sticker_animation_layout));
ImageView sticker = new ImageView(this);
int stickerId = getStickerIdFromButton(v);
if(stickerId == 0){
stickerAnimationPlaying = false;
return;
}
float destX = 200.0f;
float destY = 200.0f;
sticker.setBackgroundResource(stickerId);
sticker.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
rl.addView(sticker);
Animations anim = new Animations();
Animation a = anim.fromAtoB(startX, startY, destX, destY, animL,750);
sticker.setAnimation(a);
a.startNow();