先来张效果图

主要是继承View的类,通过Paint的画笔类,对小圆点即选择的位置做区域限制,获取到当前选择区域的像素转换成rgb。

核心类ColorPickerView

public class ColorPickerView extends View   {private Context mContext;
   private Paint mRightPaint;            //画笔
   private int mHeight;                  //view高
   private int mWidth;                   //view宽
   private Bitmap mLeftBitmap;
   private Bitmap bitmapTemp;

   private Paint mBitmapPaint;//画笔
   private PointF mLeftSelectPoint;//坐标
   private OnColorBackListener onColorBackListener;
   private int mLeftBitmapRadius;
   public String colorStr="";
   private int initX = 0;
   private int initY = 0;
   private int r;//半径

   public ColorPickerView(Context context) {this(context, null);
   }public ColorPickerView(Context context, AttributeSet attrs) {super(context, attrs);
      mContext = context;
      init();
   }public void setOnColorBackListener(OnColorBackListener listener) {onColorBackListener = listener;
   }//初始化资源与画笔
   private void init() {bitmapTemp = BitmapFactory.decodeResource(getResources(), R.drawable.color_wheel);
      mRightPaint = new Paint();
      mRightPaint.setStyle(Paint.Style.FILL);
      mRightPaint.setStrokeWidth(1);
      mBitmapPaint = new Paint();

      mLeftBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.color_wheel_dot);
      mLeftBitmapRadius = mLeftBitmap.getWidth() / 2;
      //获取圆心
      initX = bitmapTemp.getWidth() / 2;
      initY = bitmapTemp.getHeight() / 2;
      r = bitmapTemp.getHeight() / 2;
      mLeftSelectPoint = new PointF(0, 0);
   }//important patient please!!!
   @Override
   protected void onDraw(Canvas canvas) {canvas.drawBitmap(bitmapTemp , null , new Rect(0, 0, mWidth , mHeight ), mBitmapPaint);
      if(mLeftSelectPoint.x != 0 || mLeftSelectPoint.y != 0){canvas.drawBitmap(mLeftBitmap, mLeftSelectPoint.x - mLeftBitmapRadius,
               mLeftSelectPoint.y - mLeftBitmapRadius, mBitmapPaint);
      }}@Override
   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {mWidth = bitmapTemp.getWidth();
      mHeight = bitmapTemp.getHeight();
      setMeasuredDimension(mWidth, mHeight);
   }@Override
   public boolean onTouchEvent(MotionEvent event) {float x = event.getX();
      float y = event.getY();
      switch (event.getAction()) {case MotionEvent.ACTION_DOWN:case MotionEvent.ACTION_MOVE:proofLeft(x, y);
            invalidate();
            getRGB();
            break;
         case MotionEvent.ACTION_UP://取色
            getRGB();
            invalidate();
      }return true;
   }private String toBrowserHexValue(int number) {StringBuilder builder = new StringBuilder(Integer.toHexString(number & 0xff));
      while (builder.length() < 2) {builder.append("0");
      }return builder.toString().toUpperCase();
   }/**
    * 像素转RGB
    */
   private void getRGB(){int pixel = bitmapTemp.getPixel((int)mLeftSelectPoint.x, (int)mLeftSelectPoint.y);
      int r = Color.red(pixel);
      int g = Color.green(pixel);
      int b = Color.blue(pixel);
      int a = Color.alpha(pixel);
      colorStr = "#" + toBrowserHexValue(r) + toBrowserHexValue(g)+ toBrowserHexValue(b);    //十六进制的颜色字符串。
      if (onColorBackListener != null) {onColorBackListener.onColorBack(a, r, g, b);
      }}/**
    * 当view离开附着的窗口时触发,该方法和 onAttachedToWindow() 是相反
    */
   @Override
   protected void onDetachedFromWindow() {if (mLeftBitmap != null && mLeftBitmap.isRecycled() == false) {mLeftBitmap.recycle();//图片回收
      }if (bitmapTemp != null && bitmapTemp.isRecycled() == false) {bitmapTemp.recycle();//图片回收
      }super.onDetachedFromWindow();
   }// 校正xy
   private void proofLeft(float x, float y) {int r = bitmapTemp.getWidth() / 2 - mLeftBitmapRadius / 2;//圆半径
      //北
      PointF N = new PointF(initX,initY - r);//北
      PointF S = new PointF(initX,initY + r);//南
      PointF W = new PointF(initX - r,initY);//西
      PointF E = new PointF(initX + r,initY);//东
      int a = twoSpotGetLine(initX,initY,x,y);//圆心到点

      if(a < r){//在圆内
         mLeftSelectPoint.x = x;
         mLeftSelectPoint.y = y;
      }else{double angle = 0;
         int c = r;
         int b = 0;
         int newx = 0;
         int newy = 0;
         if(x > initX){//二四象限 NE SE
            if(y > initY){//四象限 东南ES
               b = twoSpotGetLine(S.x,S.y,x,y);//南点到点
               double aoccos = (Math.pow(a,2)+ Math.pow(c,2) - Math.pow(b,2)) / (2*a*c);
               angle = 90d - (Math.acos(aoccos)*(180/Math.PI));//角度
               Log.e("getLeftColor", "角度东南ES a: " + a + ",b: " + b + ",c: " + c + ",angle:" + angle);
               if(angle % 90 == 0){newx = initX;
                  newy = initY + r;
               }}else if(y < initY){//二象限 北东EN
               b = twoSpotGetLine(E.x,E.y,x,y);//北点到点
               double aoccos = (Math.pow(a,2)+ Math.pow(c,2) - Math.pow(b,2)) / (2*a*c);
               angle = 360d - (Math.acos(aoccos)*(180/Math.PI));//角度
               Log.e("getLeftColor", "角度北东EN a: " + a + ",b: " + b + ",c: " + c + ",angle:" + angle);
               if(angle % 90 == 0){newx = initX + r;
                  newy = initY;
               }}}else{//一三象限
            if(y > initY){//一象限 西北WN
               b = twoSpotGetLine(W.x,W.y,x,y);//西点到点
               double aoccos = (Math.pow(a,2)+ Math.pow(c,2) - Math.pow(b,2)) / (2*a*c);
               angle = 180d - (Math.acos(aoccos)*(180/Math.PI));//角度
               Log.e("getLeftColor", "角度西北WN a: " + a + ",b: " + b + ",c: " + c + ",angle:" + angle);
               if(angle % 90 == 0){newx = initX - r;
                  newy = initY;
               }}else if(y < initY){//三象限 南西SW
               b = twoSpotGetLine(N.x,N.y,x,y);//东点到点
               double aoccos = (Math.pow(a,2)+ Math.pow(c,2) - Math.pow(b,2)) / (2*a*c);
               angle = 270d - (Math.acos(aoccos)*(180/Math.PI));//角度
               Log.e("getLeftColor", "角度南西SW a: " + a + ",b: " + b + ",c: " + c + ",angle:" + angle);
               if(angle % 90 == 0){newx = initX;
                  newy = initY - r;
               }}}if(angle % 90 != 0){newx = (int)(initX + r * Math.cos(angle * Math.PI/180));
            newy = (int)(initY + r * Math.sin(angle * Math.PI/180));
         }Log.e("getLeftColor", "新坐标 x: " + newx + ",y: " + newy );
         mLeftSelectPoint.x = newx;
         mLeftSelectPoint.y = newy;
      }//    图片区域
//    if (x < 0) {
//       mLeftSelectPoint.x = 0;
//    } else if (x > (LEFT_WIDTH)) {
//       mLeftSelectPoint.x = LEFT_WIDTH;
//    } else {
//       mLeftSelectPoint.x = x;
//    }
//
//    Log.e("proofLeft()", "proofLeft: " + x + "," + y);
//    if (x < 0) {
//       mLeftSelectPoint.x = 0;
//    } else if (x > (LEFT_WIDTH)) {
//       mLeftSelectPoint.x = LEFT_WIDTH;
//    } else {
//       mLeftSelectPoint.x = x;
//    }
//    if (y < 0) {
//       mLeftSelectPoint.y = 0;
//    } else if (y > (mHeight - 0)) {
//       mLeftSelectPoint.y = mHeight - 0;
//    } else {
//       mLeftSelectPoint.y = y;
//    }
   }/**
    * 两点取直线
    * @param x1
    * @param y1
    * @param x2
    * @param y2
    * @return
    */
   private int twoSpotGetLine(float x1,float y1,float x2,float y2){double line2 = Math.pow((x1-x2),2) + Math.pow((y1-y2),2);
      return (int)Math.abs(Math.sqrt(line2));
   }public String getColorStr() {return colorStr;
   }public void setColorStr(String colorStr) {this.colorStr = colorStr;
   }public interface OnColorBackListener {public void onColorBack(int a, int r, int g, int b);
   }
}

首页 MainActivity

public class MainActivity extends Activity {private ColorPickerView colorDisk=null;
   private TextView tv;

   @Override
   protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      tv=(TextView)findViewById(R.id.tv_info);
      colorDisk=(ColorPickerView)findViewById(R.id.colorDisk);
      colorDisk.setOnColorBackListener(new ColorPickerView.OnColorBackListener() {@Override
         public void onColorBack(int a, int r, int g, int b) {tv.setText("R:" + r + "\nG:" + g + "\nB:" + b + "\n" + colorDisk.getColorStr());
            tv.setTextColor(Color.argb(a, r, g, b));
         }});

   }@Override
   public boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }}

xml页面

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

     <com.mrlin.mycolordisk.ColorPickerView
         android:id="@+id/colorDisk"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerHorizontal="true"/>

     <TextView
         android:id="@+id/tv_info"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@+id/colorDisk"
         android:layout_centerHorizontal="true"
         android:layout_marginBottom="20dp"
         android:gravity="center_horizontal"
         android:layout_marginTop="15dp"
         android:text="颜色取值" />

</RelativeLayout>

最后附上源码

http://download.csdn.net/download/feiniyan4944/10200855

android 调色盘颜色选取相关推荐

  1. Matplotlib科研画图.调色盘颜色提取和更改

    Matplotlib科研画图.调色盘颜色提取和更改 #提取调色盘颜色 palette#提取seaborn调色盘颜色 plt.style.use('default') #清空之前调色盘更改 sns.se ...

  2. android 屏幕坐标色彩,Android自定义View实现颜色选取器

    Android 自定义View 颜色选取器,可以实现水平.竖直选择颜色类似 SeekBar 的方式通过滑动选择颜色. 效果图 xml 属性 1.indicatorColor 指示点颜色 2.indic ...

  3. echarts的学习(六)调色盘的学习

    调色盘 调色盘是什么 调色盘的分类 主题的调色盘 全局的调色盘 局部的调色盘 颜色渐变 线性渐变 径向渐变 调色盘是什么 我们之前的饼图的样式是这样的 以上我们也没有自己配置颜色,他就有自己的颜色了, ...

  4. 第16章 调色盘管理器

    如果硬件允许,本章就没有存在的必要.尽管许多现代的显示卡提供24位颜色(也称「true color」或「数百万色」)或16位颜色(「增强色」或「数万种颜色」),一些显示卡-尤其是在便携式计算机上或高分 ...

  5. Echarts主题和调色盘以及颜色渐变

    默认主题: <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF- ...

  6. [实战] Android FragmentDialog palette调色盘

    使用第三方library,添加可互动的EditText用户可以选择手动输入hexCode或是直接选一个颜色 在app gradle中添加 implementation 'com.github.duan ...

  7. PyQt5 画笔颜色选取功能 QPalette,QColorDialog

    画笔,画刷颜色改变功能 功能说明:点击"change"按钮 --> 跳出调色盘 --> 选择颜色并确定 -->画笔或画刷颜色改变,且控件颜色改变. class m ...

  8. 009_调色盘和高亮样式

    1. color调色盘 1.1. 调色盘, 可以在option中设置.它给定了一组颜色, 图例.系列会自动从其中选择颜色.可以设置全局的调色盘, 也可以设置系列自己专属的调色盘. option = { ...

  9. echarts(一)下载引入,调色盘,[标题、图例组件、坐标轴]

    一个简单的例子 1. 下载并引入 (1)npm install echarts --save (2)import echarts from 'echarts' //main.js引入echarts 或 ...

  10. 图表可视化seaborn风格和调色盘

    seaborn是基于matplotlib的python数据可视化库,提供更高层次的API封装,包括一些高级图表可视化等工具. 使用seaborn需要先安装改模块pip3 install seaborn ...

最新文章

  1. [ay原创作品]用wpf写了个模仿36Kr网站登录背景的效果
  2. 阿里公共DNS正式发布:223.5.5.5 223.6.6.6
  3. leetcode最大矩形 (动态规划 python)
  4. coreData数据操作
  5. 【BZOJ3328】PYXFIB 数论+矩阵乘法
  6. Java反射机制深度剖析
  7. RPCBind 服务被利用进行 UDP 反射 DDoS 风险预警
  8. python数据分析和数据可视化总结
  9. 双层pdf软件free_如何一键下载网上文档以及pdf
  10. 无人驾驶汽车系统入门(十四)——ROS入门与实践(1)
  11. 双目相机选择——镜头与相机的参数介绍及选择
  12. 珍藏的PS技巧(可以尝试一下哦)(转载)
  13. 认知计算框架有哪些?
  14. php直接读取导入excel文件内容
  15. SCU(System Control Units)
  16. html 用css画出斑马线,使用css实现斑马线(面试转载)
  17. Anaconda3(Python3.6)使用whl文件安装opencv
  18. 帆软成为BI市场第一不奇怪,它的模式没办法学
  19. ALI Flutter进阶笔记
  20. 2023大厂招聘岗位数预测!明年哪些公司可以去?

热门文章

  1. 如何检测页面是否允许访问Cookie
  2. 遥感应用中影像最佳波段组合分析详细过程
  3. 最完美开启三星note9USB调试模式的方法
  4. 计算机组装日记,求微机组装与维护实习日记?
  5. excel中的if函数使用方法
  6. Spring Bean生命周期(简单易懂)
  7. 每一首歌曲的背后都有一段感人的故事……
  8. 计算机睡眠与休眠有什么区别,小编教你电脑睡眠和休眠的区别是什么.
  9. 中图分类号,文献标识码,文章编号
  10. Japanese Student Championship 2021