简体   繁体   English

Android-从RelativeLayout动画视图到同级Relativeayout

[英]Android - animate view from RelativeLayout to sibling Relativeayout

My Activity has a RelativeLayout as it's main view with the id parent . 我的Activity有一个RelativeLayout作为其主视图,其ID为parent Within this I have two RelativeLayouts , child1 and child2 . 在其中,我有两个RelativeLayoutschild1child2

I want to animate an ImageView from child2 RelativeLayout to child1 . 我想为从child2 RelativeLayoutchild1ImageView设置动画。 I don't want the image to disappear in child2 to then suddenly appear in child1 . 我不希望图像在child2消失,然后突然出现在child1

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/parent">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/child1"/>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/child2">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/image"
            android:layout_centerInParent="true"/>
    </RelativeLayout>

</RelativeLayout>

I can't find anyway of doing this without using addView and removeView . 如果不使用addViewremoveView我将无法找到任何removeView If I do this though, the ImageView will just appear in child1 , and I won't be able to animate it smoothly. 但是,如果执行此操作,则ImageView只会出现在child1 ,并且我将无法对其进行动画处理。

I have the idea of the following possible way but it feels like a real long winded hack. 我有以下几种可能的想法,但感觉就像是一个漫长的。

  • finding the x and y of the ImageView 找到ImageViewxy
  • adding a duplicate view in parent parent添加重复视图
  • removing the original from child2 child2移除原件
  • animate the duplicate in parent to the correct position in child1 (which will require potentially difficult calculations) parent项中的重复项动画child1的正确位置(这可能需要潜在的困难计算)
  • add the original view in child1 which is underneath the duplicate view child1添加原始视图,该视图位于重复视图的下面
  • remove the duplicate view 删除重复的视图

Is there any other way of doing this? 还有其他方法吗?

For a simple way you can try like this : 您可以尝试使用以下简单方法:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/parent">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/child1>

       <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/image2"
            android:layout_centerInParent="true"/>

       </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/child2">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/image2"
            android:layout_centerInParent="true"
            android:visibility:gone/>
    </RelativeLayout>

</RelativeLayout>

and in Java when button is pressed for example, just add drawable to "image2", set visible "image2" and hide "image1". 例如在Java中,当按下按钮时,只需将drawable添加到“ image2”,设置可见的“ image2”并隐藏“ image1”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM