web-dev-qa-db-fra.com

Redimensionnement des mises en page par programme (en tant qu'animation)

Je souhaite 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 constater, la hauteur des mises en page du haut et du bas est 0 et celle du milieu Couvre tout le lieu. 

Je souhaite par programme diminuer la taille de la disposition moyenne, tout en augmentant les tailles supérieure, inférieure et supérieure, jusqu'à ce que toutes les dispositions aient la même hauteur.

Je veux que ça ressemble à une animation.

Comment devrais-je faire ça?

Merci

42
dor506

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

/**
 * 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();
    }
}
93
faylon

Sur Honeycomb (Android 3.0), il existe les classes Animator et ObjectAnimator pour des animations plus fluides.

Lisez-le ici

Exemple sur la façon d’animer le 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 déplacera réellement les vues, contrairement à l'animation antérieure à Honeycomb. De la docs:

Les animations précédentes ont changé l'apparence visuelle des objets cibles ... mais elles n'ont pas réellement changé les objets eux-mêmes.

4
Decoy

Aussi, vous pouvez redimensionner avec les nouvelles animations de printemps de Google.

Méthode de création 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 de la vue d'origine.)

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