Android: with RippleEffect & StateListAnimator

前端 未结 2 1268
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-19 08:37

I have a layout, that includes another layout:



        
相关标签:
2条回答
  • 2021-02-19 09:17

    You need to find the view and call the appropriate method to change the state list animator and the background. You might need to also call setClickable on the root view of you included layout.

    LinearLayout layout1 = findViewById(R.id.layout1);
    View root = layout1.getChildAt(0);
    
    StateListAnimator sla = AnimatorInflater.loadStateListAnimator(context, R.anim.lift_up); 
    root.setStateListAnimator(sla);
    root.setBackground(R.drawable.ripple_effect);
    
    0 讨论(0)
  • 2021-02-19 09:17

    Based on the thread Does Android XML Layout's 'include' Tag Really Work? and LayoutInflater.java it seems <include> tag only supports android:id, layout_* and android:visibility attributes. So your code to set background and stateListAnimator have no effect.

    To fix the below issue with @Stepane's code :

    your method enables the ripple effect properly, however the SLA isn't fired. I cannot see any elevation taking place

    If the inflated view is transparent, then the elevation is not visible, you have to set viewOutline or use some non-transparent color for the view to see the shadow.

    An extract from ViewOutlineProvider documentation:

    Interface by which a View builds its Outline, used for shadow casting and clipping.

    To set the outlineProvider you can make use of View#setOutlineProvider (ViewOutlineProvider provider) method or you can set via android:outlineProvider xml tag.

    Updated Code:

    LinearLayout root =(LinearLayout) findViewById(R.id.container);
    StateListAnimator sla = AnimatorInflater.loadStateListAnimator(this, R.animator.lift_up);
    root.setStateListAnimator(sla);
    root.setClickable(true);
    root.setOutlineProvider(ViewOutlineProvider.PADDED_BOUNDS);
    root.setBackground(ContextCompat.getDrawable(this, R.drawable.ripple_effect));
    

    ripple_effect.xml

    <ripple
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:color="?android:colorAccent"
        tools:ignore="NewApi">
      <item>
        <shape
            android:shape="rectangle">
            <solid android:color="@android:color/transparent"/>            
            <!-- Doesn't require outlineProvider
            <solid android:color="@android:color/darker_gray"/>
            -->
        </shape>
      </item>
    </ripple>
    

    res/animator/lift_up.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item
          android:state_enabled="true"
          android:state_pressed="true">
        <objectAnimator
            android:duration="@android:integer/config_shortAnimTime"
            android:propertyName="translationZ"
            android:valueTo="48dp"/>
      </item>
      <item>
        <objectAnimator
            android:duration="@android:integer/config_shortAnimTime"
            android:propertyName="translationZ"
            android:valueTo="0dp"/>
      </item>
    </selector>
    
    0 讨论(0)
提交回复
热议问题