我有9个ImageView,其中8个是可点击的,而imageView9是不可点击的,当点击每个可点击的imageView时,它将使用属性动画将其位置与imageView9交换。 根据我的理解,如果我在ImageView上使用属性动画,它还应该更新ImageView的实际位置。但是,在这种情况下,即使imageView 9已经移动到单击的imageView的位置,每个imageView也会移动到原始位置(如果是imageView9)。我在这里想念的是什么?
MainActivity.java
public class MainActivity extends Activity implements View.OnClickListener {
ImageView imageView1, imageView2, imageView3, imageView4, imageView5, imageView6, imageView7, imageView8, imageView9;
int x, y, lMargin, tlMargin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = (ImageView) findViewById(R.id.image1);
imageView2 = (ImageView) findViewById(R.id.image2);
imageView3 = (ImageView) findViewById(R.id.image3);
imageView4 = (ImageView) findViewById(R.id.image4);
imageView5 = (ImageView) findViewById(R.id.image5);
imageView6 = (ImageView) findViewById(R.id.image6);
imageView7 = (ImageView) findViewById(R.id.image7);
imageView8 = (ImageView) findViewById(R.id.image8);
// imageView9 is not clickable, when other imageView is clicked,
// The clicked imageView swaps its position with imageView9
imageView9 = (ImageView) findViewById(R.id.image9);
imageView1.setOnClickListener(this);
imageView2.setOnClickListener(this);
imageView3.setOnClickListener(this);
imageView4.setOnClickListener(this);
imageView5.setOnClickListener(this);
imageView6.setOnClickListener(this);
imageView7.setOnClickListener(this);
imageView8.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// Get position of imageView9
lMargin = imageView9.getLeft();
// Get position of currently clicked imageView
tlMargin = v.getLeft();
ObjectAnimator blockAnim = ObjectAnimator.ofFloat(v, "x", lMargin);
blockAnim.setDuration(1000);
blockAnim.setRepeatCount(0);
blockAnim.start();
ObjectAnimator emptyAnim = ObjectAnimator.ofFloat(findViewById(R.id.image9), "x", tlMargin);
emptyAnim.setDuration(1000);
emptyAnim.setRepeatCount(0);
emptyAnim.start();
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="100dp"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/relative"
android:layout_width="150px"
android:layout_height="150px"
android:background="@color/gray"
>
<ImageView
android:id="@+id/image9"
android:layout_width="50px"
android:layout_height="50px"
android:background="@color/red"
android:layout_marginTop="100px"
android:layout_marginLeft="100px"
/>
<ImageView
android:id="@+id/image1"
android:layout_width="50px"
android:layout_height="50px"
/>
<ImageView
android:id="@+id/image2"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginTop="0px"
android:layout_marginLeft="50px"
/>
<ImageView
android:id="@+id/image3"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginTop="0px"
android:layout_marginLeft="100px"
/>
<ImageView
android:id="@+id/image4"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginTop="50px"
android:layout_marginLeft="0px"
/>
<ImageView
android:id="@+id/image5"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginTop="50px"
android:layout_marginLeft="50px"
/>
<ImageView
android:id="@+id/image6"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginTop="50px"
android:layout_marginLeft="100px"
android:background="@color/green"
/>
<ImageView
android:id="@+id/image7"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginTop="100px"
android:layout_marginLeft="0px"
android:background="@color/green"
/>
<ImageView
android:id="@+id/image8"
android:layout_width="50px"
android:layout_height="50px"
android:background="@color/blue"
android:layout_marginTop="100px"
android:layout_marginLeft="50px"
/>
</RelativeLayout>
</RelativeLayout>
请帮我指出我的错误,或者非常欢迎任何更好的解释。谢谢。