The difference between Android development Getx,getrawx,getwidth,gettranslationx, etc.

Source: Internet
Author: User
Tags gety

Reprint Please specify source: http://blog.csdn.net/dmk877/article/details/51550031

Long time no blog, the recent work is really busy, just finished a TV project development, for the first development of TV project I said: Mobile phone development is good AH. Originally thought that TV project development is finished can rest a few days but I still think more, haha, immediately will be the development of new projects, alas, said more are tears ah, nonsense not to say we go to the point, today and we discussed together is some distance from Android, may in the daily development we will often encounter, But sometimes these concepts are particularly easy to confuse, so today I summarize the difference between getx,getrawx,getwidth and so on. Hope to be helpful to everyone, if there is falsehood, welcome criticism, if you have questions welcome message
A graphical representation of GetX, GETRAWX, Gettranslationx, etc.

First, let's look at the representation of these methods on a graph, and then validate them in the form of code. We look at the representation of the geometry of these methods:

The above is just a picture to show the meaning of these distances, below we use the text to describe, and then through the case to verify that we thoroughly understand the meaning of these distances.
Second, GetX, getrawx, Gettranslationx meaning of the text description
Event.getx (): Represents the distance from the left edge of the touch point Distance event.gety (): Represents the distance of the touch point distance to its upper boundary event.getrawx: Represents the distance of the touch point from the left edge of the screen Event.getrawy: Represents the distance of the touch point from the upper edge of the Screen View.getwidth (): Indicates the width of the current control, that is, GetRight ()-getleft ()
View.getheight (): Represents the height of the current control, that is, Getbottom ()-gettop () View.gettop (): The distance from the top of the child view to the top of the parent View View.getright (): The distance from the right edge of the child view to the left edge of the parent View View.getbottom (): The distance from the bottom of the child view to the top of the parent View View.getleft (): The distance from the left edge of the child view to the left edge of the parent view View.gettranslationx () calculates the offset of the view on the x-axis. The initial value is 0, the left offset value is negative, and the right offset value is positive. View.gettranslationy () calculates the offset of the view on the y-axis. The initial value is 0, the upward offset is negative, and the downward offset is proof.
The careful reunion sends a Gettranslationx, which calculates the offset of the view on the x-axis. The initial value is 0, the left offset value is negative, and the right offset value is positive. Since the use of graphics is bad, there will be a case to illustrate its meaning.
Third, the case understanding Getx, GETRAWX, Gettranslationx usage
Next we have two cases to understand the meaning of Getx, GETRAWX, Gettranslationx, first of all to see the operation:


And then I'll explain this layout first the outermost one is the phone screen, and then the white rectangle inside is the black rectangle inside the parent view, the middle of the small white point is one of the points we clicked, it is in the center of the screen, where the resolution of the screen is 480*320, we know at this resolution 1dp= 1px, and then the white rectangle width and height are 300dp, the middle black rectangle width and height are 150dp, the middle black area is rlcenter, the middle of the small white spot is ivdot everybody can calculate a rlcenter.getwidth,rlcenter.getx, Rlcenter.getrawx and Rlcenter.gettop,rlcenter.getbottom and so on, and then with the post-print log comparison, first look at the code:

[Java]View PlainCopy
  1. Package com.example.test;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Import Android.util.Log;
  5. Import android.view.MotionEvent;
  6. Import Android.view.View;
  7. Import Android.view.View.OnTouchListener;
  8. Import Android.view.Window;
  9. Import Android.view.WindowManager;
  10. Import Android.widget.RelativeLayout;
  11. Public class Mainactivity extends Activity {
  12. Private Boolean isfocus=false;
  13. private Relativelayout Rlcenter;
  14. private int screenwidth;
  15. private int screenheight;
  16. private float x, y;
  17. private float Rawx,rawy;
  18. @Override
  19. protected void OnCreate (Bundle savedinstancestate) {
  20. super.oncreate (savedinstancestate);
  21. Requestwindowfeature (Window.feature_no_title);
  22. GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN
  23. , WindowManager.LayoutParams.FLAG_FULLSCREEN);
  24. Setcontentview (R.layout.activity_main);
  25. Rlcenter= (relativelayout) Findviewbyid (r.id.rl_center);
  26. ScreenWidth = Screenutils.getscreenwidth (mainactivity.   this);
  27. ScreenHeight = Screenutils.getscreenheight (mainactivity.   this);
  28. LOG.I ("mainactivity","ScreenWidth:" +screenwidth);
  29. LOG.I ("mainactivity","ScreenHeight:" +screenheight);
  30. Rlcenter.setontouchlistener (new Ontouchlistener () {
  31. @Override
  32. Public Boolean OnTouch (View V, motionevent event) {
  33. int action = Event.getaction ();
  34. switch (action) {
  35. Case Motionevent.action_down:
  36. X=event.getx ();
  37. Y=event.gety ();
  38. RAWX=EVENT.GETRAWX ();
  39. Rawy=event.getrawy ();
  40. LOG.I ("mainactivity", "event.getx () =" +x+"," +"event.gety () =" +y+"," +"event.getrawx () =" +  rawx+"Event.getrawy () =" +rawy);
  41. Break ;
  42. }
  43. return false;
  44. }
  45. });
  46. }
  47. @Override
  48. public void Onwindowfocuschanged (boolean hasfocus) {
  49. super.onwindowfocuschanged (Hasfocus);
  50. if (hasfocus&&!isfocus) {
  51. LOG.I ("mainactivity", "Rlcenter.getwidth=" +rlcenter.getwidth () +"," +"rlcenter.getheight=" +  Rlcenter.getheight ());
  52. LOG.I ("mainactivity", "rlcenter.getleft=" +rlcenter.getleft () +"," +"rlcenter.getright=" + Rlcenter.getright () +"," +"rlcenter.gettop=" +rlcenter.gettop () + "," +"rlcenter.getbottom=" +  Rlcenter.getbottom ());
  53. }
  54. Isfocus=true;
  55. }
  56. }
then look at the printed results:


Is it the same as you calculated? One of the things that you can see is that there are 0.0 of errors because the small white spot in the middle is 2DP, and it may not be just the middle of the click, but it doesn't affect our testing. Well, here I believe we should have a clearer understanding of these concepts. We then look at Gettranslationx, which we mentioned above, which calculates the offset of the view on the x-axis. The initial value is 0, the left offset value is negative, and the right offset value is positive. How to verify it? With the property animation and can do, first we look at the code:

[Java]View PlainCopy
  1. Package com.example.test2;
  2. Import Android.animation.ObjectAnimator;
  3. Import android.app.Activity;
  4. Import Android.os.Bundle;
  5. Import Android.util.Log;
  6. Import Android.view.View;
  7. Import Android.view.View.OnClickListener;
  8. Import Android.widget.ImageView;
  9. Public class Mainactivity extends activity{
  10. private ImageView ivtest;
  11. @Override
  12. protected void OnCreate (Bundle savedinstancestate) {
  13. super.oncreate (savedinstancestate);
  14. Setcontentview (R.layout.activity_main);
  15. Ivtest= (ImageView) Findviewbyid (r.id.iv_test);
  16. Ivtest.setonclicklistener (new Onclicklistener () {
  17. @Override
  18. public void OnClick (View v) {
  19. LOG.I ("mainactivity","ivtest.gettranslationx () =" +ivtest.gettranslationx () +"," +"  Ivtest.gettranslationy () = "+ivtest.gettranslationy ());
  20. Objectanimator.offloat (Ivtest,"Translationx", 100f). Setduration (+).start ();
  21. }
  22. });
  23. }
  24. }
Look at the print results:


See, just start printing the Gettranslationx is 0, and then we perform the property animation this property animation is to let this view move to the right 100px and then print to find its value becomes 100, which also validates our statement.

Okay, this blog post is here. I believe you should be very clear about these concepts,

If you find an error during reading, thank you for correcting it.

Reprint Please specify source: http://blog.csdn.net/dmk877/article/details/51550031

The difference between Android development Getx,getrawx,getwidth,gettranslationx, etc.

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.