Итак, получается, что это намного проще, чем я себе представлял.
Я создал полноэкранный RelativeLayout, который я показываю только во время анимации.
Я получаю начальную позицию моей скрытой кнопки, как это (забавно видеть эти механизмы кодирования в стиле 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, //from xType
fromX,
Animation.ABSOLUTE, //to xType
toX,
Animation.ABSOLUTE, //from yType
fromY,
Animation.ABSOLUTE, //to yType
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) {
//this is just a method to delete the ImageView and hide the animation Layout until we need it again.
clearAnimation();
}
};
И, наконец, мы бросаем все это вместе и нажимаем GO
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;//arbitrary place on screen
float destY = 200.0f;//arbitrary place on screen
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();