【问题标题】:Problem on moving an image using translate animation on android在android上使用翻译动画移动图像的问题
【发布时间】:2023-03-20 06:09:01
【问题描述】:

我想在 android 上将图像从 0,0 移动到 100,100。我正在使用翻译动画来做到这一点:

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。我的代码有问题吗?如何让图片正确停在100100?

【问题讨论】:

    标签: android animation


    【解决方案1】:

    我认为你需要使用

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

    不需要onAnimationEnd 事件。 不知道为什么它会转移到200, 200

    【讨论】:

      【解决方案2】:

      我也有同样的问题,我知道调用animation.setFillAfter(true) 不是正确的方法,因为它只在动画的最后一个位置绘制视图的图片,但真正的视图在该位置就像之前一样。也许图像没有问题,但如果你想用按钮或类似的东西尝试一下,你就会意识到这一点。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-03
        • 1970-01-01
        • 2015-04-19
        • 1970-01-01
        • 2014-07-31
        • 1970-01-01
        • 2013-03-07
        • 2012-04-13
        相关资源
        最近更新 更多