Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么赢了';ImageView不能移动吗?_Android - Fatal编程技术网

Android 为什么赢了';ImageView不能移动吗?

Android 为什么赢了';ImageView不能移动吗?,android,Android,我正在制作一个手机游戏,我想让ImageView在按下左右按钮时水平移动 我尝试了setX()和Object Animator,但它仍然无法移动 左右按钮 btnRight.setOnClickListener(new View.OnClickListener() { public void onClick(View right) { x++; ObjectAnimator d = Objec

我正在制作一个手机游戏,我想让ImageView在按下左右按钮时水平移动

我尝试了setX()和Object Animator,但它仍然无法移动

左右按钮

        btnRight.setOnClickListener(new View.OnClickListener() {
            public void onClick(View right) {
                x++;
                ObjectAnimator d = ObjectAnimator.ofFloat(first, "translationX", x);
                d.setDuration(2000);
                d.start();

            }
        });
        btnLeft.setOnClickListener(new View.OnClickListener() {
            public void onClick(View left) {
                x--;
                ObjectAnimator animation = ObjectAnimator.ofFloat(first, "translationX", x);
                animation.setDuration(2000);
                animation.start();

            }
        });
Imageview XML

    <ImageView
        android:id="@+id/first"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="160dp"
        android:layout_marginEnd="166dp"
        android:layout_marginRight="168dp"
        android:layout_marginBottom="471dp"
        android:adjustViewBounds="true"
        android:background="#00FFFFFF"
        android:contentDescription="@android:string/untitled"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.963"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:srcCompat="@android:color/background_light"
        tools:srcCompat="@android:color/background_light" />


当我使用setX时,如果我同时按下两个按钮,对象将始终向左移动,并保持不变。当我使用ObjectAnimator时,它只是停留在那里。

ObjectAnimator应该可以。
我尝试了你的代码,效果很好,但是翻译距离非常小,我必须多次单击按钮才能看到明显的移动。

您可以将
x++
更改为
x+=10
甚至
x+=50
,以检查它是否实际工作。

ObjectAnimator应该可以。
我尝试了你的代码,效果很好,但是翻译距离非常小,我必须多次单击按钮才能看到明显的移动。

您可以将
x++
更改为
x++=10
甚至
x++=50
以检查它是否实际工作。

如果移动它一个像素,您将无法看到增量,通过增加增量值使其更夸张。如果移动它一个像素,您将无法看到增量,通过增加增量值使其更夸张。