Android custom View (6)-origin coordinate Problems in View

Source: Internet
Author: User

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.

Related Article

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.