在Android上使用翻译动画移动图像的问题
问题描述:
我想在android上将图像从0,0移动到100,100。我使用翻译动画这样做:在Android上使用翻译动画移动图像的问题
public void moveImage() {
// move image from 0,0 to 100,100
mAnimationTranslate = new TranslateAnimation(0, 100, 0, 100);
mAnimationTranslate.setDuration(1000);
mAnimationTranslate.setAnimationListener(this);
this.startAnimation(mAnimationTranslate);
}
public void onDraw (Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(bmp, x, y, null);
}
public void onAnimationEnd(Animation animation) {
// stop animation and draw the image at 100,100
x = 100;
y = 100;
}
问题是,当动画完成在100,100,图像将移动到200,200短的时间和回100,100到底。我的代码有问题吗?如何让图像正确停止在100,100?
答
我认为你需要使用
animation.setFillAfter(true); //to retain the properties after the animation finishes.
没有必要为onAnimationEnd
事件。 不知道为什么它移动到200, 200
虽然。
答
我和它有同样的问题,我知道调用animation.setFillAfter(true)
不是正确的方法,因为它只绘制动画的最后位置上的视图的图片,但真实的视图位于像之前。也许图像没有问题,但如果你想用按钮或类似的东西来尝试它,你会意识到它。