android 设置坐标,android – 将View从一个坐标设置为另一个坐标的正确方法是什么?...

所以,事实证明这比我想象的要简单得多.

我创建了一个全屏的RelativeLayout,我只在动画发生时显示.

我得到了这个埋头按钮的起始位置(在Java中看到这些C风格的编码机制很有趣,这些日子非常罕见:

int fromLoc[] = new int[2];

v.getLocationOnScreen(fromLoc);

float startX = fromLoc[0];

float startY = fromLoc[1];

所以,现在我有了我的起点.

我的终点是屏幕上的绝对坐标,您可以根据需要分配

然后我做了一个小动画帮助类,让我传递所有坐标,回调和动画的持续时间

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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值