Android-custom control-Wheel

Source: Internet
Author: User

Some time ago, the artist designed a control and spent two days to get it out. As a result, he accidentally saw an open-source project on the internet...

This control can scroll up and down, and the value of the selected part in the middle can be obtained through the adapter ..

First, first:

Run the code again ..

This is the view of the control.

Note: This is only the code of the white scroll part and the green part in the middle. You need to use XML layout for the background .. The pictures will not be sent. After all, they are made in the company... In addition, I used two widgets to display them together.

Public class scrolllistview extends view {public static final string tag = scrolllistview. class. getsimplename (); static final int up = 0; // sliding direction, up static final int down = 1; // sliding direction, down private context mcontext; private gesturedetector mgesturedetector; private scrolllistviewadapter <?> Mdata; private paint mpaint; private int offset = 0; // offset, descending, ascending // Private int offsetgaol; private int mmaxpos; // slide to the coordinate private int mitemheight = 50 of the last element; // the height of a row of elements is private int mhalfitemheight; // the height of a half-Row Element is private int mheight; // draw the content height private int mhalfheight; // half of the content height private int mcurrentmiddleindex; // the index value of the element in the center of the current control in the list private int mflingvelocity; // fling speed private int mtouchinitialoffset; // Temporary Variable save offse before touch Tprivate Boolean mdragging; // whether or not the private float mstarttouchpos is in the touch state; // the start point coordinate of the touch private float mtextoffsety; // The font center offset private Boolean iscenter = false; // adjust the fine-tuning control of the word center to true. If it is set to false, private int mdirect =-1 is not adjusted. // Private bitmap MTop; private bitmap mbottom is directed; private float mtextpaddingleft = 10; /*** Control Touch event ** @ author lhxia **/public interface scrollviewlistener {/*** touch start to call ** @ Param y * touch the Y coordinate of the first vertex */ publi C void scrollviewtouchstart (float y);/*** call ** @ Param y * Y coordinate of the current touch point */Public void scrollviewtouchmove (float y ); /*** call after touch */Public void scrollviewtouchend (); /*** call the ** @ Param Vy * Y-direction coordinate */Public void scrollviewfling (float Vy) When fling ); /*** update control */Public void scrollviewdraw ();/*** call */Public void onstop (INT index) when the speed is 0 );}; private scrollviewlistener mscrollviewlistener = new scrollviewli Stener () {@ overridepublic void scrollviewtouchstart (float y) {log. E (TAG, "On touch start"); mdragging = true; mstarttouchpos = y; mtouchinitialoffset = offset; mflingvelocity = 0 ;}@ overridepublic void scrollviewtouchmove (float y) {log. E (TAG, "On Move"); float Dis = mstarttouchpos-y; setdirect (DIS); offset = trap (INT) (mtouchinitialoffset-Dis )); updatedisplay () ;}@ overridepublic void scrollviewtouchend () {Log. E (TAG, "On touch end"); mdragging = false; // offsetgaol = offset; invalidate (); iscenter = true ;}@ overridepublic void scrollviewfling (float Vy) {log. E (TAG, "On fling"); mdragging = false; // offsetgaol = offset; mflingvelocity = (INT) (-Vy); updatedisplay ();} @ overridepublic void scrollviewdraw () {log. D (TAG, "On Draw"); If (iscenter) {updatedisplay (); iscenter = false;} else if (mflingvelocity! = 0) {updatedisplay () ;}@ overridepublic void onstop (INT index) {If (mdata! = NULL) {mdata. onrefreshindex (INDEX) ;}};/*** determine the sliding direction * @ Param dis sliding distance */private void setdirect (float dis) {If (DIS> 0) {mdirect = up;} else {mdirect = down;}/*** update the offset and call invalidate to refresh the interface */private void updatedisplay () {If (! Mdragging) {int offsetdelta = 0; If (mflingvelocity! = 0) {// when the speed is not 0, the control slows down the log. D (TAG, "speed down, V =" + mflingvelocity); offsetdelta = mflingvelocity/40; // If (mflingvelocity> 90) {mflingvelocity-= 90 ;} else if (mflingvelocity <-90) {mflingvelocity + = 90;} else {mflingvelocity = 0; log. D (TAG, "Stop, V =" + mflingvelocity); iscenter = true;} offset-= offsetdelta; If (offset <=-(mmaxpos-(mheight/2) -mitemheight) {offset =-(mmaxpos-(MHe Ight/2)-mitemheight); mflingvelocity = 0; iscenter = true; log. D (TAG, "Stop, V =" + mflingvelocity) ;}if (offset >= mhalfheight) {offset = mhalfheight; mflingvelocity = 0; iscenter = true; log. D (TAG, "Stop, V =" + mflingvelocity);} // offsetgaol = offset;} else {// when the speed is 0, the element may not be centered, adjust the center int Delta = (mhalfheight-offset) % mitemheight; If (delta <= mhalfitemheight-10 & Delta! = 0) {Delta + = 1;} else if (delta> mhalfitemheight + 10) {Delta-= 1;} else {If (delta> = mhalfitemheight) {Delta + = 1;} else if (delta <mhalfitemheight) {Delta-= 1 ;}} if (Delta! = 0) {iscenter = true;} If (mdirect = down) {log. D (TAG, "direct down"); offset + = delta;} else if (mdirect = up) {log. D (TAG, "direct up"); offset + = delta;} If (mscrollviewlistener! = NULL) {mscrollviewlistener. onstop (computecurrentmiddleposition () ;}} invalidate () ;}/ *** calculate in the data list, index currently displayed in the middle of the control ** @ Return Index of the elements in the current control */private int computecurrentmiddleposition () {double F; If (offset <= 0) {f = (-offset) + mhalfheight) * 1.0/mitemheight;} else {f = (mhalfheight-offset) * 1.0/mitemheight;} mcurrentmiddleindex = (INT) math. round (f); If (mdata! = NULL) {log. D (TAG, "compute currentmiddleposition is:" + mcurrentmiddleindex + "size" + mdata. size () + "offset:" + Offset + "\ n" + "halfheight" + mhalfheight);} else {return-1 ;}return mcurrentmiddleindex ;} /*** check the offset range * @ Param POS * @ return returns the detected offset */private int trap (int pos) {If (Pos> = mheight/2) return mheight/2; If (Pos <=-(mmaxpos-(mheight/2)-mitemheight) Return-(mmaxp OS-(mheight/2)-mitemheight); Return Pos;}/*** get the Index * @ return */Public int getmiddleindex () of the current element in the list () {computecurrentmiddleposition (); Return mcurrentmiddleindex ;} /*** image 9.png * @ Param C canvas * @ Param ID image ID * @ Param R1 image range */private void drawninepath (canvas C, int ID, rect R1) {bitmap BMP = bitmapfactory. decoderesource (getresources (), ID); ninepatch patch = new ninepatch (BMP, BMP. Getninepatchchunk (), null); patch. draw (C, R1);} public scrolllistview (context, attributeset attrs) {super (context, attrs); mcontext = context; mgesturedetector = new gesturedetector (context, new gesturedetector. simpleongesturelistener () {@ overridepublic Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy) {If (mscrollviewlistener! = NULL) {mscrollviewlistener. scrollviewfling (velocityy);} return true ;}}); typedarray type = context. obtainstyledattributes (attrs, R. styleable. scrolllistview); int textcolor = type. getcolor (R. styleable. scrolllistview_textcolor, 0xff00ff00); // provide the default value, put the unspecified float textsize = type. getdimension (R. styleable. scrolllistview_textsize, 25); mtextpaddingleft = type. getdimension (R. styleable. scrolllistview_padding Left, 25); type. recycle (); mpaint = new paint (); mpaint. setantialias (true); mpaint. settextsize (textsize); mtextoffsety = (mitemheight-mpaint. gettextsize ()/2; mpaint. setcolor (textcolor); mpaint. settypeface (typeface. sans_serif); MTop = bitmapfactory. decoderesource (mcontext. getresources (), R. drawable. bg_condition_top_alpha); mbottom = bitmapfactory. decoderesource (mcontext. getresources (), R. drawable. BG _ Condition_bottom_alpha);} private void Init () {If (mdata! = NULL) {mmaxpos = mdata. size () * mitemheight;} offset = mhalfheight;} @ overrideprotected void onmeasure (INT widthmeasurespec, int heightmeasurespec) {mheight = measurespec. getsize (heightmeasurespec); mhalfheight = mheight/2; mhalfitemheight = mitemheight/2; offset = mhalfheight; // offsetgaol = offset; computecurrentmiddleposition (); super. onmeasure (widthmeasurespec, heightmeasurespec);} @ overrideprotect Ed void ondraw (canvas) {super. ondraw (canvas); // draw the middle line for debugging // canvas. drawline (0, mhalfheight, getmeasuredwidth (), mhalfheight, // mpaint); // canvas. drawline (0, mhalfheight-mitemheight, getmeasuredwidth (), // mhalfheight-mitemheight, mpaint); float y = 0; If (mdata! = NULL) {int size = mdata. size (); string TMP; For (INT I = 0; I <size; I ++) {Y = I * mitemheight + offset-mtextoffsety + 20; TMP = mdata. getvalue (I); If (TMP! = NULL & Y>-20 | Y <mheight + 20) {canvas. drawtext (TMP, mtextpaddingleft, I * mitemheight + offset-mtextoffsety + 20, mpaint) ;}} if (mscrollviewlistener! = NULL) {mscrollviewlistener. scrollviewdraw () ;}// canvas. drawline (0, offset, getmeasuredwidth (), offset, mpaint); drawninepath (canvas, R. drawable. bg_condition_checked, new rect (0, (INT) (mhalfheight-mhalfitemheight + 2), getmeasuredwidth (), (INT) (mhalfheight + mhalfitemheight-1 ))); // draw a 2-end gradient mask // drawninepath (canvas, R. drawable. bg_condition_top_1, // new rect (0, 0, getmeasuredwidth (), getmeasuredheigh T (); canvas. drawbitmap (MTop, 0, 0, mpaint); canvas. drawbitmap (mbottom, 0, mheight-4.3f, mpaint) ;}@ overridepublic Boolean ontouchevent (motionevent event) {If (mgesturedetector. ontouchevent (event) {return true;} switch (event. getaction () {Case motionevent. action_down: mscrollviewlistener. scrollviewtouchstart (event. gety (); break; Case motionevent. action_move: mscrollviewlistener. scrollviewtouchmove (Event. Gety (); break; Case motionevent. action_up: mscrollviewlistener. scrollviewtouchend (); break;} return true;} public void setadapter (scrolllistviewadapter <?> Adapter) {mdata = adapter; Init (); invalidate ();} public scrolllistviewadapter <?> Getadapter () {return mdata ;}}

This is the adapter used to bind data to the View:

Public abstract class scrolllistviewadapter <t> {public abstract string getvalue (INT index); public abstract int size (); /*** when the control is rolling, the control calls * @ Param Index * @ return */public abstract int onrefreshindex (INT index); public abstract string GETID (INT index ); public abstract t getitem (INT index); public abstract void setlist (list <t> data); Public scrolllistviewadapter (list <t> data ){}}

The code is not well written .. If you have any questions, please advise ..

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.