* Android App Development If you want to go deeper, you're going to use some advanced controls and a custom view, okay, start customizing view to the Great God Road, this section from the simplest
* Start with circle view, how to define attributes, and callback use.
Here is the implementation code
/** * A circular progress bar control * @author fengy * */public class Customprogressview extends View {paint paint; int mcolor;//circle Color Private O Nclickprogressviewlistener 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 outermost circle Paint.setcolor (Mcolor);p Aint.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 use callbacks, the purpose is to view the commonality, business logic for users to achieve.
There are two ways to use the 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}});
The second allows your class to implement the Click Interface implements Onclickprogressviewlistener then implement his OnClick method,
It's very simple who uses who, and the Android view is all used in this way, which is the awesome observer pattern.
Custom interface Circle-Custom attribute callback interface