Android Custom View-android Learning Journey (14)

Source: Internet
Author: User

Steps to customize view

When the system components provided by andoid do not meet the requirements, it is entirely possible to integrate the view to derive the custom component.
The first defines a subclass that inherits the view, and then overrides one or more of his methods.

An introduction to overriding methods

Constructor: This is the most basic way to customize the view, which is required when creating with Java code or reading from an XML file.
Onfinishinflate () This is a callback method that is called when the application loads the component from an XML layout file and uses it to construct the interface.
Onmeasure (): The Change method detects the view component and the size of the component it contains.
OnLayout (): The location where the sub-component needs to be allocated this method is called when the child is older.
Onsizechanged (): This method is called when the size of the component is changed.
OnDraw (): This method is called when drawing his content.
OnKeyDown (): This method is called when the key is pressed.
Onkeyoff (): This method is called when the key is released.
Ontrackballevent (): This method is called when a trackball occurs.
Ontouchevent (): Touch event occurs
Onwindowfocuschanged (): When the component gets lost focus
Onattachedtowindow (): Bar when a component is placed in a window
Ondetachedfromwindow (): When the component is detached from the window
Onwindowvisibilitychanged (): When the visibility of a component changes

Sample view code with a small ball following your finger
 Public  class viewtest extends View {     Public floatCurrentX = +; Public floatCurrentY = -;PrivatePaint paint; Public viewtest(Context context) {Super(context); } Public viewtest(Context context,attributes attiattributes) {Super(context); }@Override    protected void OnDraw(Canvas canvas) {Super. OnDraw (canvas); Paint =NewPaint ();        Paint.setcolor (color.red); Canvas.drawcircle (Currentx,currenty, the, paint); }@Override     Public Boolean ontouchevent(Motionevent event)        {CurrentX = Event.getx ();        CurrentY = Event.gety (); Invalidate ();return true; }}

Android Custom View-android Learning Journey (14)

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.