Custom interface Circle-Custom attribute callback interface

Source: Internet
Author: User

* 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.