Three methods to obtain linearinflater
A. layoutinflater Inflater = getlayoutinflater ();
B. layoutinflater localinflater =
(Layoutinflater) Context. getsystemservice
(Context. layout_inflater_service );
C. layoutinflater Inflater = layoutinflater. From (context );
The ondraw method is used for drawing, and the invalidate interface is refreshed.
:
Click to change color
After the ondraw graph is drawn, set the click event for the control, pass the parameter to the control, and refresh the invalidate
1. Draw an image with ondraw and add the changecolor method.
Public class cusview3 extends view {private int color = 0; Public cusview3 (context, attributeset attrs) {super (context, attrs) ;}@ overrideprotected void ondraw (canvas) {super. ondraw (canvas); paint mpaint = new paint (); If (color> 2) {color = 0;} switch (color) {Case 0: mpaint. setcolor (color. green); break; Case 1: mpaint. setcolor (color. red); break; Case 2: mpaint. setcolor (color. blue); break; default: break;} mpaint. setstyle (style. fill); mpaint. settextsize (35366f); canvas. drawtext ("click to refresh", 10, 60, mpaint);} public void changecolor () {// to make the outside call color ++ ;}}
2. Layout
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><xue.test.CusView3android:id="@+id/cusview3"android:layout_width="wrap_content"android:layout_height="wrap_content"></xue.test.CusView3></LinearLayout>
3. After drawing, set a click event for the control, upload the parameters to the control, and refresh the invalidate.
Public class testcustomviewactivity extends activity {@ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); view3 = (cusview3) findviewbyid (R. id. cusview3); // click the event view3.setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {message = new message (); message. what = 1; myhandler. sendmessage (Message) ;}}) ;}handler myhandler = new handler () {// public void handlemessage (Message MSG) {Switch (MSG. what) {Case 1: // call method view3.changecolor (); // refresh method view3.invalidate (); break;} super. handlemessage (MSG) ;}}; private cusview3 view3 ;}
Layoutparams may be used to occupy the entire screen.
Source code download: http://download.csdn.net/detail/ethan_xue/4152203