Android颜色选择器

一种是固定颜色的选择器,这个很简单,只要画出来各种颜色区域,用户选择哪个,就选择了什么颜色。

另一种是万能颜色选择器,这个有一些算法的,所以就参考了网上的文章(由于原始出处不详,就不表示感谢了),又做了一些优化和修改。

 

目前的这个万能颜色选择器的功能已经足够了,也没什么可保密的,就放上来,给需要的人做个参考。喜欢就拿去好了。

 

  1. package arui.csdn.generaltools.colorchooser.universalcolor;  
  2. import arui.csdn.generaltools.colorchooser.ColorChooserType;  
  3. import arui.csdn.generaltools.colorchooser.OnColorChangedListener;  
  4. import android.content.Context;  
  5. import android.graphics.Canvas;  
  6. import android.graphics.Color;  
  7. import android.graphics.LinearGradient;  
  8. import android.graphics.Paint;  
  9. import android.graphics.RectF;  
  10. import android.graphics.Shader;  
  11. import android.graphics.SweepGradient;  
  12. import android.view.MotionEvent;  
  13. import android.view.View;  
  14. /** 
  15.  * Universal color view class. This class will draw color chooser graph. 
  16.  *  
  17.  * @author http://blog.csdn.net/arui319 
  18.  *  
  19.  */  
  20. public class UniversalColorView extends View {  
  21.     private Paint mPaint;  
  22.     private Paint mCenterPaint;  
  23.     private Paint mHSVPaint;  
  24.     private final int[] mColors;  
  25.     private int[] mHSVColors;  
  26.     private boolean mRedrawHSV;  
  27.     private OnColorChangedListener mListener;  
  28.     private boolean mTrackingCenter;  
  29.     private boolean mHighlightCenter;  
  30.     private static final int CENTER_X = 100;  
  31.     private static final int CENTER_Y = CENTER_X;  
  32.     private static final int CENTER_RADIUS = 30;  
  33.     private static final int OUTER_RADIUS = 100;  
  34.     private static final int HSV_X = CENTER_X;  
  35.     private static final int HSV_Y_TOP = CENTER_Y + 10;  
  36.     private static final int HSV_Y_BOTOM = HSV_Y_TOP + 20;  
  37.     private static final float PI = 3.1415926f;  
  38.     public UniversalColorView(Context context, OnColorChangedListener listener,  
  39.             int color) {  
  40.         super(context);  
  41.         this.setBackgroundColor(Color.LTGRAY);  
  42.         mListener = listener;  
  43.         mColors = new int[] { 0xFFFF00000xFFFF00FF0xFF0000FF0xFF00FFFF,  
  44.                 0xFF00FF000xFFFFFF000xFFFF0000 };  
  45.         Shader s = new SweepGradient(00, mColors, null);  
  46.         mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);  
  47.         mPaint.setShader(s);  
  48.         mPaint.setStyle(Paint.Style.STROKE);  
  49.         mPaint.setStrokeWidth(55);  
  50.         mCenterPaint = new Paint(Paint.ANTI_ALIAS_FLAG);  
  51.         mCenterPaint.setColor(color);  
  52.         mCenterPaint.setStrokeWidth(5);  
  53.         mHSVColors = new int[] { 0xFF000000, color, 0xFFFFFFFF };  
  54.         mHSVPaint = new Paint(Paint.ANTI_ALIAS_FLAG);  
  55.         mHSVPaint.setStrokeWidth(10);  
  56.         mRedrawHSV = true;  
  57.     }  
  58.     @Override  
  59.     protected void onDraw(Canvas canvas) {  
  60.         float r = CENTER_X - mPaint.getStrokeWidth() * 0.5f;  
  61.         canvas.translate(CENTER_X, CENTER_X);  
  62.         int c = mCenterPaint.getColor();  
  63.         if (mRedrawHSV) {  
  64.             mHSVColors[1] = c;  
  65.             mHSVPaint.setShader(new LinearGradient(0 - HSV_X, 0, HSV_X, 0,  
  66.                     mHSVColors, null, Shader.TileMode.CLAMP));  
  67.         }  
  68.         canvas.drawOval(new RectF(-r, -r, r, r), mPaint);  
  69.         canvas.drawCircle(00, CENTER_RADIUS, mCenterPaint);  
  70.         canvas.drawRect(new RectF(0 - HSV_X, HSV_Y_TOP, HSV_X, HSV_Y_BOTOM),  
  71.                 mHSVPaint);  
  72.         if (mTrackingCenter) {  
  73.             mCenterPaint.setStyle(Paint.Style.STROKE);  
  74.             if (mHighlightCenter) {  
  75.                 mCenterPaint.setAlpha(0xFF);  
  76.             } else {  
  77.                 mCenterPaint.setAlpha(0x80);  
  78.             }  
  79.             canvas.drawCircle(00, CENTER_RADIUS  
  80.                     + mCenterPaint.getStrokeWidth(), mCenterPaint);  
  81.             mCenterPaint.setStyle(Paint.Style.FILL);  
  82.             mCenterPaint.setColor(c);  
  83.         }  
  84.         mRedrawHSV = true;  
  85.     }  
  86.     @Override  
  87.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  88.         setMeasuredDimension(CENTER_X * 2, HSV_Y_BOTOM * 2 - 20);  
  89.     }  
  90.     @Override  
  91.     public boolean onTouchEvent(MotionEvent event) {  
  92.         float x = event.getX() - CENTER_X;  
  93.         float y = event.getY() - CENTER_Y;  
  94.         double radius = Math.sqrt(x * x + y * y);  
  95.         boolean inCenter = radius <= CENTER_RADIUS;  
  96.         boolean inOuter = radius <= OUTER_RADIUS;  
  97.         switch (event.getAction()) {  
  98.         case MotionEvent.ACTION_DOWN:  
  99.             mTrackingCenter = inCenter;  
  100.             if (inCenter) {  
  101.                 mHighlightCenter = true;  
  102.                 invalidate();  
  103.                 break;  
  104.             }  
  105.         case MotionEvent.ACTION_MOVE:  
  106.             if (mTrackingCenter) {  
  107.                 if (mHighlightCenter != inCenter) {  
  108.                     mHighlightCenter = inCenter;  
  109.                     invalidate();  
  110.                 }  
  111.             } else if ((x >= 0 - HSV_X && x <= HSV_X)  
  112.                     && (y <= HSV_Y_BOTOM && y >= HSV_Y_TOP)) {  
  113.                 // see if we are in the hsv slider   
  114.                 int a, r, g, b, c0, c1;  
  115.                 float p;  
  116.                 // set the center paint to this color   
  117.                 if (x < 0) {  
  118.                     c0 = mHSVColors[0];  
  119.                     c1 = mHSVColors[1];  
  120.                     p = (x + 100) / 100;  
  121.                 } else {  
  122.                     c0 = mHSVColors[1];  
  123.                     c1 = mHSVColors[2];  
  124.                     p = x / 100;  
  125.                 }  
  126.                 a = ave(Color.alpha(c0), Color.alpha(c1), p);  
  127.                 r = ave(Color.red(c0), Color.red(c1), p);  
  128.                 g = ave(Color.green(c0), Color.green(c1), p);  
  129.                 b = ave(Color.blue(c0), Color.blue(c1), p);  
  130.                 mCenterPaint.setColor(Color.argb(a, r, g, b));  
  131.                 mRedrawHSV = false;  
  132.                 invalidate();  
  133.             } else if (inOuter) {  
  134.                 float angle = (float) Math.atan2(y, x);  
  135.                 // need to turn angle [-PI ... PI] into unit [0....1]   
  136.                 float unit = angle / (2 * PI);  
  137.                 if (unit < 0) {  
  138.                     unit += 1;  
  139.                 }  
  140.                 mCenterPaint.setColor(interpColor(mColors, unit));  
  141.                 invalidate();  
  142.             }  
  143.             break;  
  144.         case MotionEvent.ACTION_UP:  
  145.             if (mTrackingCenter) {  
  146.                 if (inCenter && mListener != null) {  
  147.                     mListener.colorChanged(this,  
  148.                             ColorChooserType.UNIVERSAL_COLOR_TYPE, mCenterPaint  
  149.                                     .getColor());  
  150.                 }  
  151.                 mTrackingCenter = false;  
  152.                 invalidate();  
  153.             }  
  154.             break;  
  155.         }  
  156.         return true;  
  157.     }  
  158.     private int interpColor(int colors[], float unit) {  
  159.         if (unit <= 0) {  
  160.             return colors[0];  
  161.         }  
  162.         if (unit >= 1) {  
  163.             return colors[colors.length - 1];  
  164.         }  
  165.         float p = unit * (colors.length - 1);  
  166.         int i = (int) p;  
  167.         p -= i;  
  168.         // now p is just the fractional part [0...1) and i is the index   
  169.         int c0 = colors[i];  
  170.         int c1 = colors[i + 1];  
  171.         int a = ave(Color.alpha(c0), Color.alpha(c1), p);  
  172.         int r = ave(Color.red(c0), Color.red(c1), p);  
  173.         int g = ave(Color.green(c0), Color.green(c1), p);  
  174.         int b = ave(Color.blue(c0), Color.blue(c1), p);  
  175.         return Color.argb(a, r, g, b);  
  176.     }  
  177.     private int ave(int s, int d, float p) {  
  178.         return s + Math.round(p * (d - s));  
  179.     }  
  180.     public int getColor() {  
  181.         return mCenterPaint.getColor();  
  182.     }  
  183.     public void setColor(int color) {  
  184.         mCenterPaint.setColor(color);  
  185.     }  
  186. }  

 

 

  1. package arui.csdn.generaltools.colorchooser;  
  2. /** 
  3.  * color changed listener. 
  4.  *  
  5.  * @author http://blog.csdn.net/arui319 
  6.  *  
  7.  */  
  8. public interface OnColorChangedListener {  
  9.     /** 
  10.      * Color changed event happened. 
  11.      *  
  12.      * @param source 
  13.      *            event source object 
  14.      * @param type 
  15.      *            ColorChooserType 
  16.      * @param color 
  17.      *            color int value 
  18.      */  
  19.     public void colorChanged(Object source, ColorChooserType type, int color);  
  20. }  

 

 

  1. package arui.csdn.generaltools.colorchooser;  
  2.   
  3. /** 
  4.  * Color chooser's type. One is defined color panel, another is universal color 
  5.  * panel. 
  6.  *  
  7.  * @author http://blog.csdn.net/arui319 
  8.  *  
  9.  */  
  10. public class ColorChooserType {  
  11.     private int type = 0;  
  12.     private static final int DEFINED_COLOR = 1;  
  13.     private static final int UNIVERSAL_COLOR = 2;  
  14.     public static final ColorChooserType DEFINED_COLOR_TYPE = new ColorChooserType(  
  15.             DEFINED_COLOR);  
  16.     public static final ColorChooserType UNIVERSAL_COLOR_TYPE = new ColorChooserType(  
  17.             UNIVERSAL_COLOR);  
  18.     private ColorChooserType(int type) {  
  19.         this.type = type;  
  20.     }  
  21.     public int getType() {  
  22.         return type;  
  23.     }  
  24.     @Override  
  25.     public boolean equals(Object type) {  
  26.         if (type instanceof ColorChooserType) {  
  27.             if (this.getType() == ((ColorChooserType) type).getType()) {  
  28.                 return true;  
  29.             }  
  30.         }  
  31.         return false;  
  32.     }  
  33.     @Override  
  34.     public int hashCode() {  
  35.         return this.getType();  
  36.     }  
  37. }  

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值