开发者

Problem on moving an image using translate animation on android

I want to move an image from 0,0 to 100,100 on 开发者_如何学Goandroid. I'm using translate animation to do so:

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;
}

The problem is when the animation finishes at 100,100, the image will move to 200,200 for a short time and back to 100,100 in the end. Is there any problem in my code? How to let the image stop at 100,100 correctly?


I think you need to use

animation.setFillAfter(true); //to retain the properties after the animation finishes.

There is no need for the onAnimationEnd event. Not sure why it's moving to 200, 200 though.


I've the same Problem with it and I know that calling animation.setFillAfter(true) isn't the right way because it only draw a picture of the view on the last position of the animation, but the real view is on the position like before. Maybe there are no problems with an image, but you'll realize it if you want to try it with a button or something similar.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜