android 调色盘颜色选取

先来张效果图

主要是继承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, 
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值