4

dans Android, je veux faire quelque chose comme ça (mais avec 2 couleurs alternées en noir et blanc:Changer la couleur FAB avec effet d'entraînement

changeant de couleur avec effet d'entraînement comme celui-ci

enter image description here

Qu'est-ce que J'ai essayé de le faire est:

1) paramètre par défaut défini backgroundTint & couleur ondulation via XML

app:backgroundTint="@android:color/black" 
app:rippleColor="@android:color/white" 

2) dans la méthode onclick, a changé de couleur blanche backgroundTint et l'ondulation de noir

définir une chaîne de couleur initiale à-dire high_color = "black". puis,

fab.setOnClickListener(new View.OnClickListener() { 
     @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 
     @Override 
     public void onClick(View v) { 
      if(high_color.equals("black")){ 
       fab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(getApplicationContext(), R.color.white))); 
       fab.setImageTintList(ColorStateList.valueOf(ContextCompat.getColor(getApplicationContext(), R.color.black))); 
       fab.setRippleColor(ContextCompat.getColor(getApplicationContext(), R.color.black)); 
       high_color = "white"; 
      }else { 
       fab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(getApplicationContext(), R.color.black))); 
       fab.setImageTintList(ColorStateList.valueOf(ContextCompat.getColor(getApplicationContext(), R.color.white))); 
       fab.setRippleColor(ContextCompat.getColor(getApplicationContext(), R.color.whites)); 
       high_color = "black"; 
      } 
     } 
    }); 

maintenant je reçois quelque chose comme ceci:

ce que je reçois est ce

enter image description here

est là de toute façon de faire celui-ci ressemble au premier? comme ralentir la vitesse d'animation ondulation ou quelque chose comme ça?

+0

oeil à cette bibliothèque: https: // GitHub.com/ozodrukh/RippleDrawable – rafsanahmad007

+0

Cela ne ressemble pas à un effet d'entraînement pour moi. Avez-vous essayé Circular Reveal pour cela? vérifiez ceci: https://guides.codepath.com/android/Circular-Reveal-Animation. Et ceci: https://developer.android.com/training/material/animations.html –

+0

Ah, si vous voulez un exemple ... vérifiez mon repo, il fait exactement ce que vous voulez, mais sur un CustomView, dans ce file: https://github.com/leandroBorgesFerreira/LoadingButtonAndroid/blob/master/loading-button-android/src/main/java/br/com/simplepass/loading_button_lib/AnimatedDrawables/CircularRevealAnimatedDrawable.java –

Répondre

0

Je pense que vous devez utiliser des sélecteurs Les sélecteurs permettent de modifier facilement les arrière-plans.

Utilisation Selectors Dans la même manière Vous aimez cette

<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@android:color/white" state_enabled="true "/> <item android:color="@android:color/black" state_enabled="false "/> </selector>

utiliser comme un arrière-plan de FAB

app:background ="@drawable/selector"

vous risquez de rendre Couleur blanc comme fond quand il est Tapped.Else Il Will Be Black Il ya beaucoup plus de codes android dans les sélecteurs.Lire sur les sélecteurs sur les développeurs Android Site Répondez-moi si cela vraiment Vous a aidé.

+0

Vous allez même définir Ripple Color In Fab 'application: rippleColor =" @ android: couleur/# 999999 "' –

0

Eh bien, je n'ai jamais essayé ceci sur FAB, mais j'ai implémenté la même chose sur un ImageButton pour des raisons de simplicité comme montré ci-dessous et cela fonctionne. J'espère que cela aide. Ceci est destiné aux API supérieures à 21 (Lollipop). Mon ImageButton a une élévation au repos de 6dp par défaut et au toucher il sera augmenté à 18dp (6dp + 12dp) que vous pouvez changer si vous avez besoin de lift_on_touch.xml. En outre, j'utilise StateListAnimator qui change en fonction du changement d'état du ImageButton.

<ImageButton 
    android:id="@+id/share_fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="16dp" 
    android:background="@drawable/ripples_on_touch" 
    android:elevation="6dp" 
    android:padding="16dp" 
    android:src="@drawable/ic_share_white_24dp" 
    android:stateListAnimator="@animator/lift_on_touch" 
    tools:targetApi="lollipop" /> 

v21/ripples_on_touch.xml

<?xml version="1.0" encoding="utf-8"?> 
<ripple xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:color="#F83E0ACC" 
tools:targetApi="lollipop"> 
<item> 
    <shape android:shape="oval"> 
     <solid android:color="#FFFF6F00" /> 
    </shape> 
</item> 
</ripple> 

v21/lift_on_touch.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_pressed="true"> 
    <set> 
     <objectAnimator 
      android:duration="@android:integer/config_longAnimTime" 
      android:propertyName="scaleX" 
      android:valueTo="1.250" 
      android:valueType="floatType" /> 
     <objectAnimator 
      android:duration="@android:integer/config_longAnimTime" 
      android:propertyName="scaleY" 
      android:valueTo="1.250" 
      android:valueType="floatType" /> 
     <objectAnimator 
      android:duration="@android:integer/config_longAnimTime" 
      android:propertyName="translationZ" 
      android:valueTo="12dp" 
      android:valueType="floatType" /> 
    </set> 
</item> 

<item> 
    <set> 
     <objectAnimator 
      android:duration="@android:integer/config_longAnimTime" 
      android:propertyName="scaleX" 
      android:valueTo="1.0" 
      android:valueType="floatType" /> 
     <objectAnimator 
      android:duration="@android:integer/config_longAnimTime" 
      android:propertyName="scaleY" 
      android:valueTo="1.0" 
      android:valueType="floatType" /> 
     <objectAnimator 
      android:duration="@android:integer/config_longAnimTime" 
      android:propertyName="translationZ" 
      android:valueTo="0dp" 
      android:valueType="floatType" /> 
    </set> 
</item> 
</selector>