Problème d'animation avec un ViewTreeObserver
Bonjour,
Lorsque je veux récupérer la largeur d'un Layout de référence, avec la méthode getWidth();, pour pouvoir ensuite l'appliquée sur un autre Layout (% par rapport à celui de référence) dans mon fragment (appelé dans le onActivityCreated) cela me retourne toujours 0. J'ai donc utilisé un ViewTreeObserver pour me donner la bonne mesure. Première question est-ce la bonne méthode à adopter ? :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
final LinearLayout myRefLayout = (LinearLayout )findViewById(R.id.my_ref_layout);
final LinearLayout myLayout = (LinearLayout )findViewById(R.id.my_layout);
ViewTreeObserver vto = myRefLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int widthRefLayout = myRefLayout.getWidth();
myLayout.getLayoutParams().width = ((widthRefLayout * 50)/100);
myLayout.requestLayout();
myLayout.startAnimation(getActivity(),R.anim.custom_animation);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
obs.removeOnGlobalLayoutListener(this);
} else {
obs.removeGlobalOnLayoutListener(this);
}
}
}); |
De plus,mon animation ne fonctionne pas ?
Merci d'avance pour votre aide !