1.view.gettranslationx calculates the offset of the view. The initial value is 0, the left offset value is negative, and the right offset value is positive.
The 2.view.getx corresponds to the view distance from the left edge of the parent container, equal to Getleft+gettranslationx.:
Example: Layout file:
< Relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context = "Com.example.animdemo2.MainActivity" > <linearlayout android:layout_width= "wrap_content" android:l ayout_height= "Wrap_content" android:layout_margin= "110px" android:orientation= "vertical" > <imag Eview android:id= "@+id/iv" android:layout_width= "50DP" android:layout_height= "50DP" Android:la yout_marginleft= "70px" <--imageview from the left side of its parent container is 70px--/> android:src= "@drawable/ic_launcher"/> </ Linearlayout></relativelayout>
Interface Code:
package Com.example.animdemo2;import Android.animation.objectanimator;import Android.app.activity;import Android.os.bundle;import Android.util.log;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.imageview;public class Mainactivity extends Activity implements onclicklistener{private static Final String TAG = "mainactivity";p rivate ImageView IV; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); iv = (ImageView) Findviewbyid (R.ID.IV); Iv.setonclicklistener (this);} @Overridepublic void OnClick (View v) {log.d (TAG, "Translationx:" +iv.gettranslationx () + ", x:" +iv.getx ()); O Bjectanimator.offloat (iv, "Translationx", -30f). Setduration (+). Start ();}}
Click on the robot two times to view the log:
You can see that the first Translationx is 0, and the value of Getx is 70. The second time because of an offset, the left offset 30px, so Translationx is -30px, and getx value is 70-30=40px. This has nothing to do with the margin of the parent container LinearLayout!
"Android Notes" View.getx and View.gettranslationx differences