Three ways to get Linearinflater
A. Layoutinflater Inflater = Getlayoutinflater ();
B. layoutinflater Localinflater =
(Layoutinflater) Context.getsystemservice
(Context.layout_inflater_service);
C. layoutinflater Inflater = layoutinflater.from (context);
OnDraw method drawing, Invalidate refresh interface.
Effect Chart:
Click to change color
OnDraw finish drawing, set click event for control, pass parameter to control, then invalidate refresh
1.onDraw drawing and adding ChangeColor method
public class CusView3 extends View {
private int color = 0;
Public CusView3 (context, AttributeSet attrs) {
Super (context, attrs);
}
@Override
protected void OnDraw (Canvas 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 (35.0f);
Canvas.drawtext ("Click Me Refresh", "n", Mpaint);
}
public void ChangeColor () {//To allow outside calls to
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.cusview3
android:id= "@+id/cusview3"
android:layout_ Width= "Wrap_content"
android:layout_height= "wrap_content"
>
</xue.test.CusView3>
</LinearLayout>
3. Set the Click event for the control after drawing, pass the parameter to the control, then invalidate refresh
public class Testcustomviewactivity extends activity {
@Override public
void OnCreate (Bundle Savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
VIEW3 = (CUSVIEW3) Findviewbyid (R.ID.CUSVIEW3);
Click event
View3.setonclicklistener (new View.onclicklistener () {
@Override public
void OnClick (View v) { Message Message
= new Message ();
Message.what = 1;
Myhandler.sendmessage (message);
}
);
Handler MyHandler = new Handler () {
//to handle public
void Handlemessage (msg) {
switch (msg.what) after receiving a message {Case
1:
//Call Method
View3.changecolor ();
Refresh Method
view3.invalidate ();
break;
Super.handlemessage (msg);
}
;
Private CusView3 view3;
}
As for the custom control to occupy the whole screen problem, may need to use the Layoutparams
The above is a small set to introduce the Android Custom view control to achieve the refresh effect, I hope to help you, if you have any questions welcome to my message, small series will promptly reply to everyone!