Traducir una vista de un diseño a otro con animación de traducción

Soy nuevo en la animación de Android y mi requisito es traducir una vista de una disposición a la disposición en un solo archivo del xml en el tecleo de esa visión.

Escenario: Supongamos que haga clic en un botón, presente en la parte superior del encabezado en un archivo xml, y que debe mover / traducir hacia abajo (que debe dar un impacto que se encuentra en la otra disposición hacia abajo al encabezado), y también quiero que Cuando el usuario hace clic de nuevo en el mismo, ahora debe moverse a su posición original.

Aquí estoy explicando con mi archivo xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/app_bg" android:orientation="vertical" > <RelativeLayout android:id="@+id/top" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/header" android:paddingLeft="10dp" android:paddingRight="10dp" > <Button android:id="@+id/btnSearchHeader" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_centerInParent="true" android:background="@drawable/search_icon" /> </RelativeLayout> <RelativeLayout android:id="@+id/bottom" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/app_transparent" android:paddingLeft="10dp" android:paddingRight="10dp" android:layout_marginTop="10dp" android:visibility="visible" > <Button android:id="@+id/btnMenu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginRight="5dp" android:text="ABC" /> <Button android:id="@+id/btnSearchSelected" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/btnMenu" android:text="CDE" /> </RelativeLayout> </LinearLayout> 

ESPECIFICACIÓN DE PRECISIÓN MÁS PRECISO (Lea cuidadosamente 🙂

Aquí tengo dos sub layouts interiores: –

Arriba Layout – id-> top Arriba Layout- id -> bottom

Ahora, una vista (Button -> btnSearchHeader) se encuentra en mi diseño superior y quiero animar lo mismo al diseño inferior (debería dar un impacto que se traduce con una animación de traducción al diseño inferior) al hacer clic en ese botón Y cuando el usuario hace clic en ese botón, debe volver a traducir a su posición original con una animación de traducción .. es decir, debe mostrar de nuevo en el diseño superior

No tengo ni idea de cómo dar estos impactos utilizando animaciones de traducción, sin embargo, sólo tengo un conocimiento básico de traducción de animación que es insuficiente para mí para trabajar en mi requisito.

Cualquier tipo de ayuda relacionada es apreciable.

Gracias

¿Has probado algo simple como el siguiente?

  final int topHeight = findViewById(R.id.top).getHeight(); final int bottomHeight = findViewById(R.id.bottom).getHeight(); final View button = findViewById(R.id.btnSearchHeader); final ObjectAnimator moveDownAnim = ObjectAnimator.ofFloat(button, "translationY", 0.F, topHeight + bottomHeight / 2 - button.getHeight() / 2); final ObjectAnimator moveUpAnim = ObjectAnimator.ofFloat(button, "translationY", topHeight + bottomHeight / 2 - button.getHeight() / 2, 0.F); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (0.F == v.getTranslationY()) moveDownAnim.start(); else moveUpAnim.start(); } }); 

Si realmente necesita la vista de botón para cambiar de padres, puede utilizar AnimatorListener para lograr esto al final de cada animación. Algo como:

 moveDownAnim.addListener(new Animator.AnimatorListener() { @Override public void onAnimationEnd(Animator animation) { ((ViewGroup)findViewById(R.id.top)).removeView(button); ((ViewGroup)findViewById(R.id.bottom)).addView(button); ((RelativeLayout)button.getLayoutParams()).addRule(RelativeLayout.CENTER_IN_PARENT); button.setTranslationY(0.F); // Since it is now positioned in the new layout, no need for translation. } @Override public void onAnimationCancel(Animator animation) { /* NOP */ } @Override public void onAnimationRepeat(Animator animation) { /* NOP */ } @Override public void onAnimationStart(Animator animation) { /* NOP */ } }); 

(Y el oyente análogo para el moveUpAnim .)

Sin embargo, dudo que tenga que hacer esto para lograr el efecto visual que desea. Pero si usted hace esta parte, usted también necesitará probablemente fijar una altura fija para su opinión top contraria a wrap_content . (De lo contrario, si ocurre un pase de disposición mientras el botón se ha movido a la vista inferior, la altura del diseño superior puede ir a 0 si no hay nada más en él.) Más fácil sería hacer esto directamente en el archivo de diseño xml. Sin embargo, si desea "hacerlo sobre la marcha", puede cambiar la altura del diseño en el método onAnimationEnd() utilizando algo así como:

 @Override public void onAnimationEnd(Animator animation) { final ViewGroup topLayout = findViewById(R.id.top); topLayout.getLayoutParams().height = topLayout.getHeight(); // Keep it the same height... topLayout.removeView(button); ((ViewGroup)findViewById(R.id.bottom)).addView(button); ((RelativeLayout)button.getLayoutParams()).addRule(RelativeLayout.CENTER_IN_PARENT); button.setTranslationY(0.F); // Since it is now positioned in the new layout, no need for translation. } 
FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.