Ich möchte einige Layouts in meiner Aktivität ändern.
Hier ist der Code des Haupt-XML:
<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>
Wie Sie sehen, ist die obere und untere Layouthöhe gleich 0 und das mittlere Layout __. deckt den gesamten Bereich ab.
Ich möchte die mittlere Layoutgröße programmgesteuert verringern und dabei sowohl die obere als auch die untere Layoutgröße erhöhen, bis alle Layouts dieselbe Höhe haben.
Ich möchte, dass es wie eine Animation aussieht.
Wie soll ich das machen?
Vielen Dank
Ich habe eine ResizeAnimation für einen ähnlichen Zweck geschrieben. Es ist einfach, aber teuer.
/**
* 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();
}
}
Auf Honeycomb (Android 3.0) gibt es die Klassen Animator
und ObjectAnimator
für flüssigere Animationen.
Lesen Sie es hier
Beispiel zum Animieren der Bewegung einer Ansichtsgruppe (LinearLayout) mit einem Bounce-Interpolator.
BounceInterpolator bounceInterpolator = new BounceInterpolator();
ObjectAnimator anim = ObjectAnimator.ofFloat(myViewGroup, "translationY", 0f, -200 );
anim.setInterpolator(bounceInterpolator);
anim.setDuration(1100).start();
Dadurch wird eine glatte Animation mit einem Bounce-Effekt ausgelöst, und die Ansichten werden nicht wie Animationen vor Honeycomb verschoben. Aus den Dokumenten:
Die vorherigen Animationen haben das visuelle Erscheinungsbild der Zielobjekte geändert ... aber sie haben die Objekte selbst nicht geändert.
Sie können auch die neuen Spring-Animationen von Google anpassen.
SpringAnimation-Erstellungsmethode:
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
}
Verwendung (Größe auf 80% der ursprünglichen Ansichtsgröße ändern.)
getSpringAnimation(view, SpringAnimation.SCALE_X, 0.8f).start()
getSpringAnimation(view, SpringAnimation.SCALE_Y, 0.8f).start()