47 votes

Redimensionnement programmé des mises en page (sous forme d'animation)

Je veux redimensionner certaines mises en page dans mon activité.

Voici le code du XML principal :

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/top"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:background="#3ee3e3" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/middle"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">
    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:background="#fe51e6" >
    </LinearLayout>
</LinearLayout>

Comme vous pouvez le voir, la hauteur des mises en page du haut et du bas est de 0, et celle du milieu est de 0. couvre toute la place.

Je veux diminuer de façon programmatique la taille de la mise en page du milieu, tout en augmentant les tailles de la mise en page du haut et du bas. et du bas, jusqu'à ce que toutes les mises en page aient la même hauteur.

Je veux que ça ressemble à une animation.

Comment dois-je m'y prendre ?

Gracias

101voto

faylon Points 3497

J'ai écrit une ResizeAnimation dans un but similaire. C'est simple mais coûteux.

Java

    /**
     * an animation for resizing the view.
     */
    public class ResizeAnimation extends Animation {
        private View mView;
        private float mToHeight;
        private float mFromHeight;

        private float mToWidth;
        private float mFromWidth;

        public ResizeAnimation(View v, float fromWidth, float fromHeight, float toWidth, float toHeight) {
            mToHeight = toHeight;
            mToWidth = toWidth;
            mFromHeight = fromHeight;
            mFromWidth = fromWidth;
            mView = v;
            setDuration(300);
        }

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            float height =
                    (mToHeight - mFromHeight) * interpolatedTime + mFromHeight;
            float width = (mToWidth - mFromWidth) * interpolatedTime + mFromWidth;
            LayoutParams p = mView.getLayoutParams();
            p.height = (int) height;
            p.width = (int) width;
            mView.requestLayout();
        }
    }

Kotlin

class ResizeAnimation(
    private val view: View,
    private val toHeight: Float,
    private val fromHeight: Float,
    private val toWidth: Float,
    private val fromWidth: Float,
    duration: Long
) : Animation() {

    init {
        this.duration = duration
    }

    override fun applyTransformation(
        interpolatedTime: Float,
        t: Transformation?
    ) {
        val height = (toHeight - fromHeight) * interpolatedTime + fromHeight
        val width = (toWidth - fromWidth) * interpolatedTime + fromWidth
        val layoutParams = view.layoutParams
        layoutParams.height = height.toInt()
        layoutParams.width = width.toInt()
        view.requestLayout()
    }
}

1 votes

Je suppose que vous devez remplacer public void initialize & public boolean willChangeBounds aussi.

0 votes

@faylon Pourquoi avez-vous dit que c'était coûteux ? Consomme-t-il plus de mémoire ou prend-il plus de temps à s'exécuter ?

1 votes

@NiteshKhatri Cela coûte beaucoup de cpu, donc l'animation ne sera pas trop fluide sur un téléphone bas de gamme, surtout quand la mise en page elle-même est déjà complexe. Je pensais que nous avions déjà une meilleure façon de mettre en œuvre de telles animations dans le nouveau SDK Android.

4voto

Decoy Points 743

Sur Honeycomb (Android 3.0), il y a la fonction Animator y ObjectAnimator pour des animations plus fluides.

Lisez-le aquí

Exemple d'animation du déplacement d'un groupe de vues (LinearLayout) avec un interpolateur de rebond.

BounceInterpolator bounceInterpolator = new BounceInterpolator();   
ObjectAnimator anim = ObjectAnimator.ofFloat(myViewGroup, "translationY", 0f, -200 );
anim.setInterpolator(bounceInterpolator);
anim.setDuration(1100).start();

Cela déclenchera une animation fluide avec un effet de rebond et fera vraiment bouger les vues, contrairement aux animations antérieures à Honeycomb. Extrait de la documentation :

Les animations précédentes modifiaient l'aspect visuel des objets cibles... mais elles ne modifiaient pas réellement les objets eux-mêmes.

3voto

Mete Points 19

Vous pouvez également redimensionner avec les nouvelles animations de printemps de Google.

Méthode de création de SpringAnimation :

fun getSpringAnimation(view: View, springAnimationType: FloatPropertyCompat<View>, finalPosition: Float): SpringAnimation {
    val animation = SpringAnimation(view, springAnimationType )
    // create a spring with desired parameters
    val spring = SpringForce()
    spring.finalPosition = finalPosition
    spring.stiffness = SpringForce.STIFFNESS_VERY_LOW // optional
    spring.dampingRatio = SpringForce.DAMPING_RATIO_NO_BOUNCY // optional
    // set your animation's spring
    animation.spring = spring
    return animation
}

Utilisation (Redimensionner à 80 % de la taille d'affichage d'origine.)

 getSpringAnimation(view, SpringAnimation.SCALE_X, 0.8f).start()
 getSpringAnimation(view, SpringAnimation.SCALE_Y, 0.8f).start()

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X