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
- Package com.example.test;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import Android.util.Log;
- Import android.view.MotionEvent;
- Import Android.view.View;
- Import Android.view.View.OnTouchListener;
- Import Android.view.Window;
- Import Android.view.WindowManager;
- Import Android.widget.RelativeLayout;
- Public class Mainactivity extends Activity {
- Private Boolean isfocus=false;
- private Relativelayout Rlcenter;
- private int screenwidth;
- private int screenheight;
- private float x, y;
- private float Rawx,rawy;
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Requestwindowfeature (Window.feature_no_title);
- GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN
- , WindowManager.LayoutParams.FLAG_FULLSCREEN);
- Setcontentview (R.layout.activity_main);
- Rlcenter= (relativelayout) Findviewbyid (r.id.rl_center);
- ScreenWidth = Screenutils.getscreenwidth (mainactivity. this);
- ScreenHeight = Screenutils.getscreenheight (mainactivity. this);
- LOG.I ("mainactivity","ScreenWidth:" +screenwidth);
- LOG.I ("mainactivity","ScreenHeight:" +screenheight);
- Rlcenter.setontouchlistener (new Ontouchlistener () {
- @Override
- Public Boolean OnTouch (View V, motionevent event) {
- int action = Event.getaction ();
- switch (action) {
- Case Motionevent.action_down:
- X=event.getx ();
- Y=event.gety ();
- RAWX=EVENT.GETRAWX ();
- Rawy=event.getrawy ();
- LOG.I ("mainactivity", "event.getx () =" +x+"," +"event.gety () =" +y+"," +"event.getrawx () =" + rawx+"Event.getrawy () =" +rawy);
- Break ;
- }
- return false;
- }
- });
- }
- @Override
- public void Onwindowfocuschanged (boolean hasfocus) {
- super.onwindowfocuschanged (Hasfocus);
- if (hasfocus&&!isfocus) {
- LOG.I ("mainactivity", "Rlcenter.getwidth=" +rlcenter.getwidth () +"," +"rlcenter.getheight=" + Rlcenter.getheight ());
- LOG.I ("mainactivity", "rlcenter.getleft=" +rlcenter.getleft () +"," +"rlcenter.getright=" + Rlcenter.getright () +"," +"rlcenter.gettop=" +rlcenter.gettop () + "," +"rlcenter.getbottom=" + Rlcenter.getbottom ());
- }
- Isfocus=true;
- }
- }
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
- Package com.example.test2;
- 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{
- private ImageView ivtest;
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- Ivtest= (ImageView) Findviewbyid (r.id.iv_test);
- Ivtest.setonclicklistener (new Onclicklistener () {
- @Override
- public void OnClick (View v) {
- LOG.I ("mainactivity","ivtest.gettranslationx () =" +ivtest.gettranslationx () +"," +" Ivtest.gettranslationy () = "+ivtest.gettranslationy ());
- Objectanimator.offloat (Ivtest,"Translationx", 100f). Setduration (+).start ();
- }
- });
- }
- }
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.