【问题标题】:Translate Animation incorrect view move翻译动画不正确的视图移动
【发布时间】:2014-11-20 09:37:08
【问题描述】:

我在 LinearLayout 中有 3 个视图对象(基本上是 ImageView),而 LinearLayout 又在 RelativeLayout 中,视图在 LinearLayout 中按如下方式对齐:



1 - 2 - 3 - -



如上所示,这 3 个视图被包裹在线性布局下。

基本上这个想法是让侧视图(第一个和第三个视图)随着动画移动到中间视图 onClick() 的位置。(所以如果第 3 个单击视图它应该移动到中间视图的位置,中间移动到第三个视图的位置。)

我实现了翻译动画,当单击 100dp 向左和第二个视图向右移动时,第三个视图发生得很好,但是当我在第一个视图的 onClick() 上应用翻译动画以使其移动到向右和第二向左移动然后第二和第三视图都一起移动! , 这意味着 Android 会自动将视图彼此相邻映射!!(注意:这就是我使用 LinearLayout 的原因,这样就不会发生对齐问题,例如第二个视图是对齐的_低于第一个视图和对齐的_左的第三个视图问题。)

还有一个问题是,虽然视图在动画移动之前移动到了原本属于另一个视图的另一个位置,但它们仍然保持原始视图 ID ??例如,当第三个视图向左移动到第二个视图位置并且第二个 mvoes 到第三个视图位置时,onClick() 仍然映射到它们的原始位置?

XML 文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
</LinearLayout>

<RelativeLayout
    android:id="@+id/Container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#138AF8" >

    <RelativeLayout
        android:id="@+id/RelativeLayout01"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="#138AF8" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/ImageView01"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/cost" />

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/cost" />

            <ImageView
                android:id="@+id/ImageView02"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/cost" />
        </LinearLayout>

    </RelativeLayout>
</RelativeLayout>

</LinearLayout>

名称为 ImageView1、ImageView01、ImageView02 的这 3 个 ImageView 是我想要制作动画的那个。

动画代码:

final TranslateAnimation moveLefttoRight = new TranslateAnimation(0,
            100, 0, 0);
    final TranslateAnimation moveRightToLeft = new TranslateAnimation(0,
            -100, 0, 0);
    moveRightToLeft.setDuration(1000);
    moveLefttoRight.setDuration(1000);
    moveRightToLeft.setFillAfter(true);
    moveLefttoRight.setFillAfter(true);
    final ImageView image1 = (ImageView) findViewById(R.id.imageView1);
    final ImageView image2 = (ImageView) findViewById(R.id.ImageView01);
    final ImageView image3 = (ImageView) findViewById(R.id.ImageView02);



        image1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            Toast.makeText(getApplicationContext(), "image1 clicked",
                    Toast.LENGTH_SHORT).show();
        }
    });
    image2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            Toast.makeText(getApplicationContext(), "image2 clicked",
                    Toast.LENGTH_SHORT).show();
            image2.startAnimation(moveLefttoRight);
            image1.startAnimation(moveRightToLeft);
        }
    });
    image3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            Toast.makeText(getApplicationContext(), "image3 clicked",
                    Toast.LENGTH_SHORT).show();
            image2.startAnimation(moveLefttoRight);
            image3.startAnimation(moveRightToLeft);
        }
    });

【问题讨论】:

    标签: android android-layout android-animation


    【解决方案1】:

    翻译动画实际上并不移动视图,它只是让它看起来像那样。您必须在动画结束之前设置参数,以便按钮也可以移动。example

    对于其他问题,您可以发布您的 xml 布局文件吗?你在图像视图中使用权重吗?

    【讨论】:

    • 我没有在图像视图中使用任何权重,我将发布我的 xml 文件
    • 我已添加 XML 供您查看,请尽快回复我。
    • 你能把你的动画代码也贴出来吗,它似乎与xml无关
    • 好的,我将编辑帖子并输入动画代码!
    • 嗨,您检查了我发布的动画代码吗?
    【解决方案2】:

    我最终使用了 ObjectAnimator 内置动画 API,这会将视图的位置永久移动到坐标。请求。

    【讨论】: