先来张效果图

主要是继承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. 修改所有列_宝塔面板安装完的一些列操作
  2. 求难、求拙、求慢、求少
  3. 清除SearchNet.exe
  4. javascript内存泄漏调试工具mac_node.js 内存泄漏的秘密
  5. Python交换两个变量的三种方法
  6. Qt生成的exe中为什么会带有不该有的盾牌?
  7. OpenCL 第5课:向量相加
  8. js math.hypot_带有Python示例的math.hypot()方法
  9. CSAPP lab3 bufbomb-缓冲区溢出攻击实验(下)bang boom kaboom
  10. 经纬度中度与度分秒的转换公式是什么,如114.629度=()度()分()秒
  11. js数组去重的4个方法
  12. zabbix 监控项自动发现过滤_zabbix怎么使用自动发现添加新监控项
  13. 浪潮ssr服务器安全加固系统贵吗,浪潮SSR加固服务器安全
  14. SharePoint 使用 CMOS 上传、下载、删除文件,新增文件夹
  15. iPhone XS、iPhone XR、iPhone XS Max 屏幕尺寸,分辨率,PPI 详细数据对比
  16. oracle数据文件recover,又遇BUG-ORA-01148:数据文件忽然变为recover状态
  17. Tinkpad笔记本双击开发Q盘不再提示创建恢复介质的对话框了,而且进入磁盘后文件夹是空的!
  18. 使用JS监听键盘按下事件
  19. 重新认识caniuse
  20. nasm预处理器(1)

热门文章

  1. MySQL多个关键词检索字段
  2. JQuery中美元符号$
  3. git commit --amend 简单使用
  4. 微信网页版如何给公众号发消息
  5. 企业内部网络的多出口相互冗余备份与负载均衡
  6. 建筑工地人脸识别门禁通道闸机如何安装
  7. 抖音看不到好友动态显示服务器,抖音好友动态怎么突然看不到了
  8. 非功能需求分析是什么
  9. 富文本编辑器ueditor 自定义工具栏配置
  10. android系统分区刷机包,安卓刷机最大最专业最齐全的安卓ROM刷机包资源