我正在开发一个五子棋游戏,我用canvas.drawBitmap来画棋盘和棋子。我想使用动画将这些片段从一个位置移动到另一个位置。我该怎么做呢?我想我应该做一些翻译动画。我能用位图做到这一点吗?谢谢。
发布于 2014-09-08 15:28:46
检查此代码以设置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);发布于 2014-09-08 15:46:53
为您的棋子创建全局位置坐标并使用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);https://stackoverflow.com/questions/25719196
复制相似问题