studio programacion herramientas fundamentos con avanzado aplicaciones android android-animation

programacion - Android: cambie el margen izquierdo mediante la animación



manual de android en pdf (4)

La respuesta del usuario1991679 es excelente, pero si necesita interpolar un margen desde cualquier otro valor excepto 0, necesita usarlo en sus cálculos:

ViewGroup.MarginLayoutParams params = (MarginLayoutParams) mBottomLayout.getLayoutParams(); final int bottomMarginStart = params.bottomMargin; // your start value final int bottomMarginEnd = <your value>; // where to animate to Animation a = new Animation() { @Override protected void applyTransformation(float interpolatedTime, Transformation t) { ViewGroup.MarginLayoutParams params = (MarginLayoutParams) mBottomLayout.getLayoutParams(); // interpolate the proper value params.bottomMargin = bottomMarginStart + (int) ((bottomMarginEnd - bottomMarginStart) * interpolatedTime); mBottomLayout.setLayoutParams(params); } }; a.setDuration(300); mBottomLayout.startAnimation(a);

En mi caso, tuve que animar una animación de "ingresar a la pantalla", que pasa de "-48dp" a 0. Sin el valor inicial, la animación siempre es 0, saltando, sin animar la vista. La solución fue interpolar el desplazamiento y agregarlo al valor original.

Estoy cambiando el margen izquierdo de una vista de imagen de la siguiente manera:

ViewGroup.MarginLayoutParams layoutParams = (MarginLayoutParams) image.getLayoutParams (); layoutParams.leftMargin = VALUE; image.setLayoutParams ( layoutParams );

Me gustaría que el cambio en el margen se aplique con la animación. Alguna pista ?

Lo que probé:

ObjectAnimator objectAnimator = ObjectAnimator.ofFloat ( image , "x" , VALUE); objectAnimator.start();

Esto funciona perfectamente, ya que la imagen se mueve al valor X especificado con animación, SIN EMBARGO , el valor de layoutParams.leftMargin permanece sin cambios. Entonces no puedo usar este método, porque si trato de cambiar el valor de layoutParams.leftMargin a 100 después de usar el ObjectAnimator con el valor 100, el valor aplicado no es correcto (se aplica 200 en lugar de 100, el efecto si el ObjectAnimator permanece A pesar de que estoy configurando el margen izquierdo de la siguiente manera:

layoutParams.leftMargin = 100;


Llegué a esta pregunta, pero no pude usarla porque quiero animar el margen desde un valor negativo a 0, así que utilicé la base de valorAnimater en la respuesta del usuario1991679:

final View animatedView = view.findViewById(R.id.animatedView); final LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) animatedView.getLayoutParams(); ValueAnimator animator = ValueAnimator.ofInt(params.bottomMargin, 0); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { params.bottomMargin = (Integer) valueAnimator.getAnimatedValue(); animatedView.requestLayout(); } }); animator.setDuration(300); animator.start();

Debe cambiar LinearLayout.LayoutParams de acuerdo con el contenedor animadoView. También puede usar nineoldandroids para versiones anteriores que no tienen ValueAnimator.


Puedes usar lo siguiente

image.animate().setDuration(durationIn).translationXBy(offsetFloat).start();

También puede agregar .setInterpolator(new BounceInterpolator()) para cambiar el aspecto de la animación.


Use la clase Animation, no ObjectAnimator.

final int newLeftMargin = <some value>; Animation a = new Animation() { @Override protected void applyTransformation(float interpolatedTime, Transformation t) { LayoutParams params = yourView.getLayoutParams(); params.leftMargin = (int)(newLeftMargin * interpolatedTime); yourView.setLayoutParams(params); } }; a.setDuration(500); // in ms yourView.startAnimation(a);

Tenga en cuenta que debe usar la clase LayoutParams correcta, es decir, si su vista es hija de LinearLayout, entonces los parámetros deben ser LinearLayout.LayoutParams