"Android Notes" View.getx and View.gettranslationx differences

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.