All of the UI controls on Android are view-based, so you deliberately focus on the next one, laying the groundwork for learning about other controls later.
Http://www.360doc.com/content/14/0102/12/12050012_342019150.shtml
Re-use the overlay method frequently
Packagecom.example.ddddddd;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Rect;ImportAndroid.util.AttributeSet;ImportAndroid.util.Log;Importandroid.view.KeyEvent;Importandroid.view.MotionEvent;ImportAndroid.view.View;ImportAndroid.widget.Button; Public classMyButtonextendsbutton{ PublicMyButton (Context context, AttributeSet attrs) {Super(context, attrs); } //the window will execute when the component is loaded protected voidonfinishinflate () {Super. Onfinishinflate (); LOG.D ("Test", "I was loaded with XML"); } //Do not understand, do not know what meaning, explained as follows//used to detect the size of the view component and all the subcomponents he contains protected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) { Super. Onmeasure (Widthmeasurespec, Heightmeasurespec); } //This method is called when the component needs to allocate its child component position, size protected voidOnLayout (BooleanChangedintLeftintTopintRightintbottom) { Super. OnLayout (changed, left, top, right, bottom); } //This method is called when the component size is changed protected voidOnsizechanged (intWintHintOLDW,intOLDH) {LOG.D ("Test", "changed size, original w=" +oldw+ "y=" +oldh+ "now w=" +w+ "y=" +h); Super. Onsizechanged (W, H, OLDW, OLDH); } //callback This method when you want to draw the component content protected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); } //This method is called when the cursor is on this component and the key is pressed Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {LOG.D ("Test", "key" +keycode+ "pressed"); return Super. OnKeyDown (KeyCode, event); } //This method is called when the cursor is on this component and the key is released Public BooleanOnKeyUp (intKeyCode, KeyEvent event) {LOG.D ("Test", "Key" +keycode+ "release"); return Super. OnKeyUp (KeyCode, event); } //There's a trackball event, and I don't know what it is. Public Booleanontrackballevent (Motionevent event) {return Super. Ontrackballevent (event); } //A touch-screen event occurs that triggers this method Public Booleanontouchevent (Motionevent event) {LOG.D ("Test", "I was touched, position X:" +event.getx () + "Y:" +event.gety ()); return Super. Ontouchevent (event); } //when getting or losing focus, trigger protected voidOnfocuschanged (BooleanGainfocus,intdirection, Rect Previouslyfocusedrect) { if(Gainfocus) {LOG.D ("Test", "Get Focus"); }Else{LOG.D ("Test", "Lose Focus"); } Super. onfocuschanged (Gainfocus, direction, previouslyfocusedrect); } //triggers when the component is placed in a window protected voidOnattachedtowindow () {Super. Onattachedtowindow (); } //component is detached from the window, triggering protected voidOndetachedfromwindow () {LOG.D ("Test", "detach"); Super. Ondetachedfromwindow (); } //trigger when visibility changes, general call Setvisibility (View.gone) protected voidOnvisibilitychanged (View Changedview,intvisibility) {LOG.D ("Test", "destroy"); Super. onvisibilitychanged (Changedview, visibility); }}View Code
A ball that moves with your finger
Public classMyViewextendsview{ PublicMyView (Context context) {Super(context); } PublicMyView (Context context, AttributeSet attrs) {Super(context, attrs); } //Define a brush PrivatePaint paint=NewPaint (); Private floatCx=0; Private floatCy=0; protected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); //set the brush color here to make redPaint.setcolor (color.red); //draw a position in the radius of Cx,cy 15, the CircleCanvas.drawcircle (CX, CY, 15, paint); } Public Booleanontouchevent (Motionevent event) {CX=Event.getx (); Cy=event.gety (); //Redrawinvalidate (); return true; }}View Code
Title, referring to a post on the internet to do
Title.xml
<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "50DP"Android:background= "#ffcb05" > <ButtonAndroid:id= "@+id/button_left"Android:layout_width= "60DP"Android:layout_height= "40DP"android:layout_centervertical= "true"Android:layout_marginleft= "5DP"Android:background= "@drawable/back_button"Android:textcolor= "#fff" /> <TextViewAndroid:id= "@+id/title_text"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_centerinparent= "true"Android:text= "title"Android:textcolor= "#fff"android:textsize= "20SP" /></Relativelayout>
View Code
Component Titleview.java
Public classTitleviewextendsFramelayout {//the button returned on the left PrivateButton LeftButton; //Middle Caption Text PrivateTextView TitleText; //Initialize PublicTitleview (Context context, AttributeSet attrs) {Super(context, attrs); //get XML configuration fileLayoutinflater.from (context). Inflate (R.layout.title, This); //Get title Text componentTitleText =(TextView) Findviewbyid (R.id.title_text); //Get Back button componentLeftButton =(Button) Findviewbyid (r.id.button_left); //Click eventsLeftbutton.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {(Activity) GetContext ()). Finish (); } }); } Public voidSettitletext (String text) {titletext.settext (text); } Public voidSetleftbuttontext (String text) {leftbutton.settext (text); } Public voidSetleftbuttonlistener (Onclicklistener l) {Leftbutton.setonclicklistener (L); }}View Code
Activity_main.xml
<Relativelayoutxmlns: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" > <Com.example.ddddddd.TitleViewAndroid:id= "@+id/title_view"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > </Com.example.ddddddd.TitleView></Relativelayout>
View Code