java - 如何围绕正方形移动图像

标签 java android android-animation

我有一个方形图像(如一 block 板),每边有 10 个字段。我正在尝试在这个棋盘上移动,步入各个领域(就像棋盘游戏中的 token /典当,一步一归档)。 10步后我必须改变方向(上、左、右、下)。我正在尝试做任何动画,在循环中的每个迭代中更改图像位置,但它只工作一次。

示例:

for (int i = 0; i < range; ++i) {
            asX = token.getX();
            asY = token.getY();
            moveLength = 2.5f;
            if (1 case) {
                token.animate().x(asX - (2.5f * token.getWidth())).y(asY);
            } else if (2 case) {
                token.animate().x(asX - (2.5f * token.getWidth())).y(asY);
            } else if (3 case) {
                token.animate().x(asX - (2.5f * token.getWidth())).y(asY);
            } else if (4 case) {
              token.animate().x(asX - (2.5f * token.getWidth())).y(asY);
            }
            position++;
            token.invalidate();
        }
range is a number of moves,
1 case - move left,
2 case - move up,
3 case - move rigth,
4 case - move down

例如,当 range=6 且 pos=0( token 位于起始字段)时, token 应向左移动 6 个字段(因为 pos < 10 且 range 也 < 10),接下来当 range=8 且 pos 时=6, token 应向左移动 4 个字段,然后向上移动 4 个字段,依此类推。 它仅移动一个字段,而不移动范围。 有什么想法吗?

最佳答案

使用 ObjectAnimator 类具有新的构造函数,使您能够对坐标进行动画处理

更多请阅读this

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  Path path = new Path();
  path.arcTo(0f, 0f, 1000f, 1000f, 270f, -180f, true);
  ObjectAnimator animator = ObjectAnimator.ofFloat(view, View.X, View.Y, path);
  animator.setDuration(2000);
  animator.start();
} else {
  // Create animator without using curved path
}

关于java - 如何围绕正方形移动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56323257/

相关文章:

java - 在 Eclipse RCP 4 中打开一个编辑部分(作为以前 eclipse 版本中的编辑器)

java - 为什么 java 加密填充算法称为 PKCS#5?

android - ionic2 构建 apk @mipmap/icon 错误

android - 共享 Intent Android 中的 Instagram 选项

java - 在 viewpager 中翻转图像

android - 如何顺时针旋转ImageView

java - spring bean实例变量可以保存动态值(每个请求)

java - Angular 6 + Spring 启动 : Error: "from origin ' http://localhost:420 0' has been blocked by CORS policy"

android - 如何忽略 SQLite 查询中的重音(Android)

android - 三星 Galaxy S4,动画屏幕闪烁