Android Developer Custom View

Source: Internet
Author: User

Ching

Original address: http://blog.csdn.net/qingdujun/article/details/41551151


"Attached:--Custom view frequently processed callback function

Onfinishinflate () triggers when all child controls in view are mapped to XML
onmeasure (int, int) determines the size of all child elements
OnLayout (boolean, int, int, int, int) triggered when view allocates the size and position of all child elements
onsizechanged (int, int, int, int) triggered when the size of the view changes
OnDraw (Canvas) View details of rendered content
OnKeyDown (int, keyevent) has a button pressed and then triggered
onKeyUp (int, keyevent) triggers when the button is pressed and bounced
Ontrackballevent (motionevent) trackball Event
Ontouchevent (motionevent) Touch screen Event
Onfocuschanged (boolean, int, Rect) triggers when view Gets or loses focus
Onwindowfocuschanged (Boolean) Triggers when a window contains a view that gets or loses focus
Onattachedtowindow () Triggers when view is attached to a window
Ondetachedfromwindow () Fires when view leaves an attached window, which is the opposite of Onattachedtowindow ().
onwindowvisibilitychanged (int) triggers when the visible view that is contained in the window has changed


1, inherit the view class, overload the parent class method (overload according to your own needs)

Package Sn.dolp.view;import Android.content.context;import Android.graphics.canvas;import android.graphics.Color; Import Android.graphics.paint;import Android.util.attributeset;import Android.view.motionevent;import Android.view.view;public class Xmlview extends View {public float cx = 40;public float cy = 50; Paint paint = new paint ();p ublic Xmlview (context context) {super (context);//TODO auto-generated constructor Stub}public X Mlview (context context, AttributeSet Attrs) {Super (context, attrs);//TODO auto-generated constructor Stub}public Xmlview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle);//TODO auto-generated Constr Uctor stub} @Overridepublic void OnDraw (canvas canvas) {super.ondraw (canvas);p aint.setcolor (color.red); Canvas.drawcircle (CX, CY, N, paint);} @Overridepublic boolean ontouchevent (Motionevent event) {cx = Event.getx (); cy = Event.gety (); invalidate (); return true;}}

2. Modify the XML layout

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:paddingbottom= "@dimen/activity_vertical_margin"    android:paddingleft= "@dimen/activity_ Horizontal_margin "    android:paddingright=" @dimen/activity_horizontal_margin "    android:paddingtop=" @dimen /activity_vertical_margin "    tools:context=" sn.dolp.view.MainActivity ">    <!--insert Homemade View interface-->< Sn.dolp.view.XmlView     android:layout_width= "match_parent"    android:layout_height= "match_parent"    / ></RelativeLayout>
Original address: http://blog.csdn.net/qingdujun/article/details/41551151
References: Uuton's article, http://www.uuton.com/post/3f493_24c2c7,2014 November 27 19:12:49

Android Developer Custom View

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.