Circle on the custom interface-callback interface for custom Properties

Source: Internet
Author: User

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.



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.