What does TranslationZ actually do in Android?

Hi, I am developing an Android application in which I am trying to implement new material design features. I tried to apply the elevation property and the TranslationZ property, but it does not work.
<Button android:id="@+id/button1" style="@style/ButtonStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="true" android:text="Name" /> 

 <?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#0073ff" /> <corners android:radius="16dp" /> </shape> 

 <style name="ButtonStyle"> <item name="android:elevation">8dp</item> <item name="android:translationZ">8dp</item> <item name="android:background">@drawable/file</item> </style> 
+10
source share
2 answers

TranslationZ is a dynamic property used for animation. Basically, it was supposed to cope perfectly with changes in height. When you press a button, its height remains unchanged and the translation animation Z. Thus, the view always knows what the initial value of the height is and can correctly respond to several touch events.

Internally Z = height + translation Z

+8
source

Easy fix

Your problem with the loss of touch feedback and Z animation when updating the style comes from a non-extension of the parent Android: Widget.Material.Button in your style. If you do this, you won’t have to do manual animation or touch feedback. Just rewrite what you need!

Description of Depth Z

According to the documentation, TranslationZ is just a dynamic component of the Z value. This means that when animating your representations along the Z axis (using ViewPropertyAnimator , etc.), the animation state will trigger Z, elevation values ​​from the static component, and will end when full value of Z using the translation component, which is the change between them or delta.

Z = height + translation Z

To implement the animation with the StateListAnimator translation, you need to make a StateListAnimator . In your particular case, because you aren’t animating anything, to set the Z-depth to Button , you just need elevation .

+2
source

All Articles