繁体   English   中英

android位图翻译动画

[英]android bitmap translation animation

我正在开发西洋双陆棋游戏。我使用canvas.drawBitmap绘制棋盘和棋子。 我想使用动画将片段从一个位置移动到另一个位置。 我怎样才能做到这一点? 我想我应该做某种翻译动画。 我可以使用位图吗? 谢谢。

检查以下代码以使ImageView动画化:

final ImageView btnTranslate1 = (ImageView) findViewById(R.id.translate1);
Animation translateAnimation1 = new TranslateAnimation(0f, x, 0f, y);
translateAnimation1.setDuration(500);
translateAnimation1.setInterpolator(new CircInterpolator(Type.INOUT));

// start your animation with this line
btnTranslate1.startAnimation(translate1);

为作品创建全局位置坐标,并使用Animation更新它们。

Point positionOfPiece;

private static final int FINAL_X = 5;
private static final int FINAL_Y = 5;

private static final int INITIAL_X = 5;
private static final int INITIAL_Y = 5;

private class CustomAnimation extends Animation {

        @Override
        protected void applyTransformation(float interpolatedTime,
                Transformation t) {
            createLog("Updating");

            if (interpolatedTime == 0) {
                    positionOfPiece.set(INITIAL_X, INITIAL_Y);
            } else if (interpolatedTime == 1) {
                    positionOfPiece.set(FINAL_X, FINAL_Y); 
            } else {
                    positionOfPiece.set((1-interpolatedTime)*INITIAL_X + interpolatedTime*FINAL_X, (1-interpolatedTime)*INITIAL_Y + interpolatedTime*FINAL_Y);
            }
            postInvalidate();
            super.applyTransformation(interpolatedTime, t);
        }

    }

此处的内插时间将在0到1之间变化(*不完全取决于内插器)。

根据您的持续时间和动画插值器。

mAnimation = new CustomAnimation();
mAnimation.setDuration(mDuration);
mAnimation.setInterpolator(mInterpolator);
startAnimation(mAnimation);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM