【发布时间】:2015-08-10 00:17:34
【问题描述】:
我有一张图片。单击按钮时,图像上会发生翻译动画,因此它会向左滑动,您最终只能看到一定比例的图像。
有没有办法在翻译动画发生后获取图像的位置?我意识到我可以使用动画侦听器并在 OnAnimationEnd 方法中执行某些操作。但是,我不确定在这里做什么......
如何获取图像移动到的位置并将其设置为图像的布局参数?
我真的可以在这里使用您的帮助。
【问题讨论】:
我有一张图片。单击按钮时,图像上会发生翻译动画,因此它会向左滑动,您最终只能看到一定比例的图像。
有没有办法在翻译动画发生后获取图像的位置?我意识到我可以使用动画侦听器并在 OnAnimationEnd 方法中执行某些操作。但是,我不确定在这里做什么......
如何获取图像移动到的位置并将其设置为图像的布局参数?
我真的可以在这里使用您的帮助。
【问题讨论】:
假设您当前的图像位置是 x, y。你在 x 方向 100,y 方向 50 移动。所以你当前的位置是 newx =x+100,newy=y+50。您将在 onAnimationEnd 方法中使用您的布局放置图像。
TranslateAnimation TAnimation=new TranslateAnimation(0, 100, 0, 50)
TAnimation.setDuration(2000);
TAnimation.setFillAfter(true);
Image.startAnimation(TAnimation);
TAnimation.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
RelativeLayout RL=(RelativeLayout)findViewById(R.id.rl);
param=new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
param.setMargins(x+100, y+50, 0, 0);
Image.setLayoutParams(param);
}
});
【讨论】:
我知道这个问题很老,但它可能会对某人有所帮助。 您可以添加:
animation.setFillAfter(true);
或在 Xml 集中:
fillAfter = "true"
就是这样。它将强制视图处于新位置
【讨论】: