先来张效果图
主要是继承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,