16

J'essaie d'imiter l'animation et le changement de couleur pour le bouton d'action flottant suivant.Animation de Fab on click (zoom avant/arrière)

La façon dont le bouton d'action flottante fonctionne est blanc est désactivé et bleu est activé.

enter image description here

Cependant, j'ai échoué avec l'animation et changer la couleur.

C'ont été mes tentatives pour faire ceci, comme vous pouvez le voir j'ai commenté toutes les différentes manières que j'ai essayées de faire ceci.

Ceci est mon code:

@SuppressWarnings("unused") 
    @OnClick(R.id.fabMovieFavourite) 
    public void addMovieFavourite(View view) { 
/*  final Animator animator = AnimatorInflater.loadAnimator(getActivity(), R.animator.add_favourite_movie); 
     animator.setTarget(view);) 
     animator.start(); 
*/ 
/* 
     AnimatorSet animatorSet = new AnimatorSet(); 
     PropertyValuesHolder propertyValuesHolderX = PropertyValuesHolder.ofFloat(View.SCALE_X, 1.1f); 
     PropertyValuesHolder propertyValuesHolderY = PropertyValuesHolder.ofFloat(View.SCALE_Y, 1.1f); 
     ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(view, propertyValuesHolderX, propertyValuesHolderY); 
     objectAnimator.setDuration(300); 
     objectAnimator.setInterpolator(new OvershootInterpolator(10f)); 
*/ 

     /* 
     objectAnimator.setRepeatCount(1); 
     objectAnimator.setRepeatMode(ObjectAnimator.REVERSE); 
*/ 

/* 
     PropertyValuesHolder propertyValuesHolderX2 = PropertyValuesHolder.ofFloat(View.SCALE_X, 0.9f); 
     PropertyValuesHolder propertyValuesHolderY2 = PropertyValuesHolder.ofFloat(View.SCALE_Y, 0.9f); 
     ObjectAnimator objectAnimator2 = ObjectAnimator.ofPropertyValuesHolder(view, propertyValuesHolderX2, propertyValuesHolderY2); 
     objectAnimator.setDuration(300); 
     objectAnimator2.setInterpolator(new OvershootInterpolator(10f)); 

     animatorSet.playSequentially(objectAnimator, objectAnimator2); 
     objectAnimator.start(); 
*/ 

     // view.BackgroundTintList(ContextCompat.getColorStateList(getContext(), R.color.primary)); 
     //view.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.primary)); 

     if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) { 
      Timber.d("start translationZ"); 
      ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, View.TRANSLATION_Z, 12f); 
      objectAnimator.setDuration(300); 
      objectAnimator.setInterpolator(new OvershootInterpolator(10f)); 
      objectAnimator.setTarget(view); 
      objectAnimator.start(); 
     } 
    } 

Un grand merci pour toutes les suggestions.

+0

ce que je vais essayer est d'utiliser deux unités de production. disparaître et cacher un, tandis que s'estomper et montrer l'autre. –

+0

@AngelKoh Je suis à la recherche d'une solution en utilisant simplement 1 fab – ant2009

+0

@ ant2009 bien que j'ai réalisé cette chose avec l'aide de la bibliothèque https://github.com/jd-alexander/LikeButton bien si vous souhaitez mettre en œuvre ce .. .Je peux aider ... ça ... – PN10

Répondre

45

Il y a deux phases dans cette animation. Le premier met à l'échelle les axes X et Y, et le second le réduit. Donc, nous pouvons les diviser en deux AnimatorSet s et les jouer séquentiellement.

Le point clé de l'animation est de trouver un interpolateur approprié pour le second AnimatorSet, car il n'est pas standard.

enter image description here

Voir, nous voulons remettre les gaz fabuleux, puis Undershoot puis enfin régler à la valeur spécifiée dans l'animateur.

Heureusement, il est très pratique PathInterpolator, qui va créer un interpolateur pour nous avec fourni Path.

Path path = new Path(); 
path.moveTo(0.0f, 0.0f); 
path.lineTo(0.5f, 1.3f); 
path.lineTo(0.75f, 0.8f); 
path.lineTo(1.0f, 1.0f); 
PathInterpolator pathInterpolator = new PathInterpolator(path); 

Alors, nous allons créer la première animation:

final float from = 1.0f; 
final float to = 1.3f; 

ObjectAnimator scaleX = ObjectAnimator.ofFloat(fab, View.SCALE_X, from, to); 
ObjectAnimator scaleY = ObjectAnimator.ofFloat(fab, View.SCALE_Y, from, to); 
ObjectAnimator translationZ = ObjectAnimator.ofFloat(fab, View.TRANSLATION_Z, from, to); 

AnimatorSet set1 = new AnimatorSet(); 
set1.playTogether(scaleX, scaleY, translationZ); 
set1.setDuration(100); 
set1.setInterpolator(new AccelerateInterpolator()); 

set1.addListener(new AnimatorListenerAdapter() { 
    @Override 
    public void onAnimationEnd(Animator animation) { 
     fab.setImageResource(isActive ? R.drawable.heart_active : R.drawable.heart_passive); 
     fab.setBackgroundTintList(ColorStateList.valueOf(isActive ? colorActive : colorPassive)); 
     isActive = !isActive; 
    } 
}); 

Nous intensifions les x, y. En outre, nous changeons la traduction de z pour avoir l'effet d'ombre approprié. Quand l'animation se termine, nous voulons changer l'état fab (couleur du coeur et fond fab).

Maintenant, nous allons créer l'animation pour régler de retour:

ObjectAnimator scaleXBack = ObjectAnimator.ofFloat(fab, View.SCALE_X, to, from); 
ObjectAnimator scaleYBack = ObjectAnimator.ofFloat(fab, View.SCALE_Y, to, from); 
ObjectAnimator translationZBack = ObjectAnimator.ofFloat(fab, View.TRANSLATION_Z, to, from); 

AnimatorSet set2 = new AnimatorSet(); 
set2.playTogether(scaleXBack, scaleYBack, translationZBack); 
set2.setDuration(300); 
set2.setInterpolator(pathInterpolator); 

Voir ici, nous avons utilisé pathInterpolator que nous avons créé plus tôt.

Nous voulons jouer ces deux AnimatorSet de la séquence:

final AnimatorSet set = new AnimatorSet(); 
set.playSequentially(set1, set2); 

set.addListener(new AnimatorListenerAdapter() { 
    @Override 
    public void onAnimationEnd(Animator animation) { 
     fab.setClickable(true); 
    } 

    @Override 
    public void onAnimationStart(Animator animation) { 
     fab.setClickable(false); 
    } 
}); 

, nous voulons également désactiver clique sur fabuleux tout en animant. Nous l'activons/désactivons en fonction de l'état de l'animation.

Enfin, nous lançons l'animation lorsqu'un clic se produit:

fab.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     set.start(); 
    } 
}); 

Résultat:

enter image description here

Source code at github