Android custom View
In the process of Android development, the official Android control sometimes cannot meet our needs. At this time, I need to customize it. Next we will look at the custom View:
Package com. example. myview; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. paint. style; import android. util. attributeSet; import android. view. view; import android. view. view. onClickListener; public class MyView extends View implements OnClickListener {private int a = 0; private Paint paint; public MyView (Context context) {super (context ); // TODO Auto-generated constructor stub} public MyView (Context context, AttributeSet attrs) {super (context, attrs) ;}@ Overrideprotected void onDraw (Canvas canvas) {super. onDraw (canvas); paint = new Paint (); // defines a paint brush. setColor (Color. RED); // set the paint color to paint. setStyle (Style. FILL); // set the paint brush to FILL the canvas. drawCircle (100,100,100, paint); // draw a circle painting. setColor (Color. BLUE); // set the color paint for the screen. setTextSize (20); // set the font size canvas. drawText ("small source" + a, 100,100, paint); setOnClickListener (this); // Add a listener to the View} @ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stuba ++; invalidate (); // redraw }}
In XML, We need to write the custom control as follows:
This completes a very simple custom View. Work together on the program Road, come on!
Okay, a little progress! It will eventually become a great god!