Android development custom View, android custom view

Source: Internet
Author: User

Android development custom View, android custom view

Author: Qing Dujun

Address: http://blog.csdn.net/qingdujun/article/details/41551151


[Appendix: -- custom callback functions that are often processed by a View

OnFinishInflate () is triggered when all the child controls in the View are mapped to xml
OnMeasure (int, int) determines the size of all child elements
OnLayout (boolean, int, int) is triggered when the View allocates the size and position of all child elements.
OnSizeChanged (int, int) is triggered when the view Size changes
OnDraw (Canvas) view rendering content details
OnKeyDown (int, KeyEvent) is triggered when a key is pressed.
OnKeyUp (int, KeyEvent) triggered when a key is pressed and then popped up
OnTrackballEvent (MotionEvent) trackball event
OnTouchEvent (MotionEvent) touch screen event
OnFocusChanged (boolean, int, Rect) is triggered when the View gets or loses focus.
OnWindowFocusChanged (boolean) is triggered when the view contained in the window gets or loses focus.
OnAttachedToWindow () is triggered when a view is attached to a window.
OnDetachedFromWindow () is triggered when the view leaves the attached window. This method is opposite to onAttachedToWindow.
OnWindowVisibilityChanged (int) is triggered when the visible view contained in the window changes]


1. inherit the View class and reload the parent class method (reload 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();public XmlView(Context context) {super(context);// TODO Auto-generated constructor stub}public XmlView(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 constructor stub}@Overridepublic void onDraw(Canvas canvas){super.onDraw(canvas);paint.setColor(Color.RED);canvas.drawCircle(cx, cy, 15, 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/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/activity_v Ertical_margin "tools: context =" sn. dolp. view. MainActivity "> <! -- Insert the custom View Interface --> <sn. dolp. view. XmlView android: layout_width = "match_parent" android: layout_height = "match_parent"/> </RelativeLayout>
Address: http://blog.csdn.net/qingdujun/article/details/41551151
References: UUTON article, http://www.uuton.com/post/3f493_24c2c7, November 27, 2014

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.