代码之家  ›  专栏  ›  技术社区  ›  Chilly Zhong

在android上使用translate动画移动图像的问题

  •  2
  • Chilly Zhong  · 技术社区  · 15 年前

    我想在android上将图像从0,0移动到100100。我正在使用翻译动画来执行此操作:

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

    问题是当动画在100100结束时,图像将在短时间内移动到200200,最后返回到100100。我的代码有问题吗?如何让图像正确地停在100100?

    2 回复  |  直到 15 年前
        1
  •  4
  •   Deepak Swami    13 年前

    我想你需要用

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

    onAnimationEnd 事件。 200, 200 不过。

        2
  •  2
  •   CoolBeans Jake    14 年前

    我也有同样的问题 animation.setFillAfter(true)