Circle on the custom interface-callback interface for custom Properties
* If you want to develop an Android Application, you need to use some advanced controls and custom views. Well, start to customize the View and go to the path of great god. This section is the simplest one.
* Starting with a circular view, how to define attributes and Use callback.
The following is the implementation code
/*** A circular progress bar control * @ author fengy **/public class CustomProgressView extends View {Paint paint; int mColor; // The Circle color private OnClickProgressViewListener mclickProgressView; public CustomProgressView (Context context, AttributeSet attrs) {super (context, attrs); // TODO Auto-generated constructor stubpaint = new Paint (); TypedArray typedArray = context. obtainStyledAttributes (attrs, R. styleable. customprogress); mColor = typedArray. getColor (R. styleable. customprogress_color, Color. RED); typedArray. recycle () ;}@ Overrideprotected void onDraw (Canvas canvas) {// TODO Auto-generated method stubsuper. onDraw (canvas); // draw the circle painting of the outermost layer. setColor (mColor); paint. setStyle (Style. STROKE); canvas. drawCircle (getWidth ()/2, getHeight ()/2, getWidth ()/2, paint);} public void setLisner (OnClickProgressViewListener pOnClickProgressView) {mclickProgressView = pOnClickProgressView ;} @ Overridepublic boolean onTouchEvent (MotionEvent event) {// TODO Auto-generated method stubif (event. getAction () = MotionEvent. ACTION_UP) {mclickProgressView. onClick (CustomProgressView. this);} return super. onTouchEvent (event);}/** expose interface as Callback using **/public interface OnClickProgressViewListener {public void onClick (CustomProgressView v );}}
Why is callback used? The purpose is to make the view universal and implement the business logic for the user.
There are two ways to use callback first
CustomProgressView cView = (CustomProgressView) rootView.findViewById(R.id.cv); cView.setLisner(new OnClickProgressViewListener() {@Overridepublic void onClick(CustomProgressView v) {// TODO Auto-generated method stub//dosomething}});
Second, let your class implement the click interface implements OnClickProgressViewListener and then implement its onclick method,
Who can use it to implement it is very simple? In Android view, this method is used, that is, the awesome observer mode.