Android color selector

Source: Internet
Author: User
Tags getcolor

Refer to the online article and make two color selectors.

 

One is a fixed-color selector. This is very simple. You only need to draw various color areas and select which one you choose.

The other is the universal color selector, which has some algorithms. Therefore, I have referred to the online article (thanks to the unknown source ), some optimizations and modifications were made.

 

At present, this omnipotent color selector is enough and there is nothing to keep confidential. Let's put it up and give a reference to the people who need it. Take it if you like.

 

Package arui. csdn. generaltools. colorchooser. universalcolor; <br/> Import arui. csdn. generaltools. colorchooser. colorchoosertype; <br/> Import arui. csdn. generaltools. colorchooser. oncolorchangedlistener; <br/> Import android. content. context; <br/> Import android. graphics. canvas; <br/> Import android. graphics. color; <br/> Import android. graphics. lineargradient; <br/> Import android. graphics. paint; <br/> Import Android. graphics. rectf; <br/> Import android. graphics. shader; <br/> Import android. graphics. sweepgradient; <br/> Import android. view. motionevent; <br/> Import android. view. view; <br/>/** <br/> * Universal Color View class. this class will draw color chooser graph. <br/> * @ author http://blog.csdn.net/arui319 <br/> */<br/> public class universalcolorview extends view {<br/> private pain T mpaint; <br/> private paint mcenterpaint; <br/> private paint mhsvpaint; <br/> private final int [] mcolors; <br/> private int [] mhsvcolors; <br/> private Boolean mredrawhsv; <br/> private oncolorchangedlistener mlistener; <br/> private Boolean mtrackingcenter; <br/> private Boolean mhighlightcenter; <br/> Private Static final int center_x = 100; <br/> Private Static final int center_y = center_x; <br/> PRI Vate static final int center_radius = 30; <br/> Private Static final int outer_radius = 100; <br/> Private Static final int HSV _x = center_x; <br/> Private Static final int HSV _y_top = center_y + 10; <br/> Private Static final int HSV _y_botom = HSV _y_top + 20; <br/> Private Static final float Pi = 3.1415926f; <br/> Public universalcolorview (context, oncolorchangedlistener listener, <br/> int colo R) {<br/> super (context); <br/> This. setbackgroundcolor (color. ltgray); <br/> mlistener = listener; <br/> mcolors = new int [] {0xffff0000, 0xffff00ff, 0xff0000ff, 0xff00ffff, <br/> 0xff00ff00, 0xffffff00, 0xffff0000 }; <br/> shader S = new sweepgradient (0, 0, mcolors, null); <br/> mpaint = new paint (paint. anti_alias_flag); <br/> mpaint. setshader (s); <br/> mpaint. setstyle (paint. style. stroke); <br/> mpaint. s Etstrokewidth (55); <br/> mcenterpaint = new paint (paint. anti_alias_flag); <br/> mcenterpaint. setcolor (color); <br/> mcenterpaint. setstrokewidth (5); <br/> mhsvcolors = new int [] {0xff000000, color, 0 xffffffff}; <br/> mhsvpaint = new paint (paint. anti_alias_flag); <br/> mhsvpaint. setstrokewidth (10); <br/> mredrawhsv = true; <br/>}< br/> @ override <br/> protected void ondraw (canvas) {<br/> float r = C Enter_x-mpaint. getstrokewidth () * 0.5f; <br/> canvas. translate (center_x, center_x); <br/> int c = mcenterpaint. getcolor (); <br/> If (mredrawhsv) {<br/> mhsvcolors [1] = C; <br/> mhsvpaint. setshader (New lineargradient (0-HSV _x, 0, HSV _x, 0, <br/> mhsvcolors, null, shader. tilemode. clamp); <br/>}< br/> canvas. drawoval (New rectf (-R,-R, R, R), mpaint); <br/> canvas. drawcircle (0, 0, center_radius, mcenterp Aint); <br/> canvas. drawrect (New rectf (0-HSV _x, HSV _y_top, HSV _x, HSV _y_botom), <br/> mhsvpaint); <br/> If (mtrackingcenter) {<br/> mcenterpaint. setstyle (paint. style. stroke); <br/> If (mhighlightcenter) {<br/> mcenterpaint. setalpha (0xff); <br/>}else {<br/> mcenterpaint. setalpha (0x80); <br/>}< br/> canvas. drawcircle (0, 0, center_radius <br/> + mcenterpaint. getstrokewidth (), mcenterpaint); <br/> mcenter Paint. setstyle (paint. style. fill); <br/> mcenterpaint. setcolor (c); <br/>}< br/> mredrawhsv = true; <br/>}< br/> @ override <br/> protected void onmeasure (INT widthmeasurespec, int heightmeasurespec) {<br/> setmeasureddimension (center_x * 2, HSV _y_botom * 2-20 ); <br/>}< br/> @ override <br/> Public Boolean ontouchevent (motionevent event) {<br/> float x = event. getx ()-center_x; <br/> float y = event. gety ()-Center_y; <br/> double radius = math. SQRT (x * x + y * Y); <br/> Boolean incenter = radius <= center_radius; <br/> Boolean inouter = radius <= outer_radius; <br/> switch (event. getaction () {<br/> case motionevent. action_down: <br/> mtrackingcenter = incenter; <br/> If (incenter) {<br/> mhighlightcenter = true; <br/> invalidate (); <br/> break; <br/>}< br/> case motionevent. action_move: <br/> If (mtrackin Gcenter) {<br/> If (mhighlightcenter! = Incenter) {<br/> mhighlightcenter = incenter; <br/> invalidate (); <br/>}< br/>} else if (x> = 0-HSV _x & x <= HSV _x) <br/> & (Y <= HSV _y_botom & Y> = HSV _y_top )) {<br/> // see if we are in the HSV slider <br/> int A, R, G, B, C0, C1; <br/> float P; <br/> // set the center paint to this color <br/> If (x <0) {<br/> C0 = mhsvcolors [0]; <br/> C1 = mhsvcolors [1]; <br/> P = (x + 100)/100; <br/>} else {<Br/> C0 = mhsvcolors [1]; <br/> C1 = mhsvcolors [2]; <br/> P = x/100; <br/>}< br/> A = ave (color. alpha (C0), color. alpha (C1), P); <br/> r = ave (color. red (C0), color. red (C1), P); <br/> G = ave (color. green (C0), color. green (C1), P); <br/> B = ave (color. blue (C0), color. blue (C1), P); <br/> mcenterpaint. setcolor (color. argb (A, R, G, B); <br/> mredrawhsv = false; <br/> invalidate (); <br/>} else if (inouter) {<br /> Float angle = (float) math. atan2 (Y, x); <br/> // need to turn angle [-Pi... pi] into Unit [0 .... 1] <br/> float unit = angle/(2 * PI); <br/> If (Unit <0) {<br/> unit + = 1; <br/>}< br/> mcenterpaint. setcolor (interpcolor (mcolors, unit); <br/> invalidate (); <br/>}< br/> break; <br/> case motionevent. action_up: <br/> If (mtrackingcenter) {<br/> If (incenter & mlistener! = NULL) {<br/> mlistener. colorchanged (this, <br/> colorchoosertype. universal_color_type, mcenterpaint <br/>. getcolor (); <br/>}< br/> mtrackingcenter = false; <br/> invalidate (); <br/>}< br/> break; <br/>}< br/> return true; <br/>}< br/> private int interpcolor (INT colors [], float Unit) {<br/> If (Unit <= 0) {<br/> return colors [0]; <br/>}< br/> If (unit> = 1) {<br/> return colors [colors. length-1]; <br/>}< br/> float P = unit * (colors. length-1); <br/> int I = (INT) P; <br/> P-= I; <br/> // now P is just the fractional part [0... 1) and I is the index <br/> int C0 = colors [I]; <br/> int C1 = colors [I + 1]; <br/> int A = ave (color. alpha (C0), color. alpha (C1), P); <br/> int r = ave (color. red (C0), color. red (C1), P); <br/> int G = ave (color. green (C0), color. green (C1), P); <br/> int B = ave (color. blue (C0), color. blue (C1), P); <br/> return color. argb (A, R, G, B); <br/>}< br/> private int ave (INT S, int D, float P) {<br/> return S + math. round (p * (D-S); <br/>}< br/> Public int getcolor () {<br/> return mcenterpaint. getcolor (); <br/>}< br/> Public void setcolor (INT color) {<br/> mcenterpaint. setcolor (color); <br/>}< br/> 

 

Package arui. csdn. generaltools. colorchooser; <br/>/** <br/> * color changed listener. <br/> * @ author http://blog.csdn.net/arui319 <br/> */<br/> Public interface oncolorchangedlistener {<br/>/** <br/> * color changed event happened. <br/> * @ Param source <br/> * event source object <br/> * @ Param type <br/> * colorchoosertype <br/> * @ Param color <br/> * color int value <br/> */<br/> Public void colorchanged (Object source, colorchoosertype type, int color); <br/>} 

 

Package arui. csdn. generaltools. colorchooser; </P> <p>/** <br/> * color chooser's type. one is defined Color panel, another is Universal Color <br/> * panel. <br/> * @ author http://blog.csdn.net/arui319 <br/> */<br/> public class colorchoosertype {<br/> private int type = 0; <br/> Private Static final int defined_color = 1; <br/> Private Static final int universal_color = 2; <br/> Public static final colorchoosertype defined_color_type = new colorchoosertype (<br/> defined_color); <br/> Public static final colorchoosertype partition = new colorchoosertype (<br/> universal_color ); <br/> private colorchoosertype (INT type) {<br/> This. type = type; <br/>}< br/> Public int GetType () {<br/> return type; <br/>}< br/> @ override <br/> Public Boolean equals (Object Type) {<br/> If (type instanceof colorchoosertype) {<br/> If (this. getType () = (colorchoosertype) type ). getType () {<br/> return true; <br/>}< br/> return false; <br/>}< br/> @ override <br/> Public int hashcode () {<br/> return this. getType (); <br/>}< br/> 

 

Related Article

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.