We have customized the View, but I have never thought of a problem: Where is the (0, 0) coordinate in the View, that is, the origin coordinate ?? Are we sometimes confused? Next we will study the origin coordinate problems in the View.
1. View origin in new HelloView
We can draw a line from the source to the bottom right of the View to see the source and the width and height of the View. Here we draw from onDraw (Canvas canvas), and the core code is given below.
HelloView. java
/** * Initialize the paint brush for line painting. **/ Public VoidInitLinePaint (){ LinePaint =NewPaint (); // Set the paint brush LinePaint. setColor (Color.GREEN); LinePaint. setAntiAlias (True); LinePaint. setStrokeWidth (5); // you can specify the line width. } /** * Draw a View **/ Protected VoidOnDraw (Canvas canvas ){ Log.V("OnDraw (Canvas canvas)", "" +This. GetHeight () + "" +This. GetWidth ()); Canvas. drawColor (Color.WHITE); InitLinePaint (); // initialize the paint brush MyUseBitmapFactory (canvas ); Canvas. drawLine (0, 0,This. GetWidth (),This. GetHeight (), linePaint ); } |
Run:
It is observed that the View Size starts from the Label that inherits the View.
Calculated, that is, the View origin starts in the lower left corner of the Label and ends in the lower right corner of the screen.
Try more by yourself.
2. Use the View origin point when layout files in XML.
Modify layout:
MainActivity. java
/** * Use a custom View **/ Public ClassMainActivityExtendsActivity { Public VoidOnCreate (Bundle savedInstanceState ){ Super. OnCreate (savedInstanceState ); SetContentView (R. layout.Main); // Use a custom View } } |
The running result diagram is provided here:
I don't want to say that. You can understand it. Haha, I don't want to explain it much.