当前位置:   article > 正文

Android上一种用于选择颜色的控件(颜色选择器)_android颜色选择控件

android颜色选择控件

引言

最近在做一个项目时其中有一个需求–自定义灯光颜色。要求通过手机端控制灯光颜色,手机端预设五种颜色及用户可自定义颜色。在百度上搜索找到一个开源的色环控件ColorPickerView,可实现颜色的自定义,效果如图1。给客户看了效果后,客户不是很满意,客户想要类似于UI设计中色相环那样的效果,如图2。
图1  ColorPickerView效果图
图2 色相环
既然客户要求了,那就必须得按客户要求来做。开始做,第一步“先百度”。在百度上果真搜到了一个控件可实现色相环的效果,该控件实现的原理时通过在View上加载图片,根据用户手指触控点获取该图片在该像素点的颜色来实现颜色选择的(这个想法挺不错)。但是最终我没有使用该控件,原因如下:
(1)由于某些原因,UI无法提供色相环的图片给我;
(2)CSDN上没有分,没办法下载demo使用demo中的色相环图片,并且demo中的图片不一定符合我的需求;
(3)适配问题,需要在hdpi,xhdpi,xxhdpi等都做不同尺寸的图片,适配起来比较麻烦。
基于以上原因放弃了使用该控件,不过那个控件挺棒的!不能使用开源控件,那就只能自己写了呗,于是我自己做了一个自定义的小控件,在项目中实现效果如图3。做好后展示给客户,符合客户要求。
图3 自定义颜色选择器ColorPicker效果
这个自定义控件我命名为ColorPicker(颜色选择器,百度搜的开源的也有叫这个的)。这个控件的特点:
1.程序员的事情程序员做,不需要UI提供色相环图片,颜色我们可以自己在代码中设置。控件中我预设了一个7环的色相环,如果颜色不符合要求可在代码中自定义颜色;
2.适配多屏幕,控件可根据屏幕密度,用户设置宽高等自适应,适配效果还凑合;
3.环数自定义,每环颜色也可以自定义,不同环颜色数也可以不同;
4.增加颜色选中效果(代码中也有选中音效,被我注释了,后续的代码中可以看到)。

核心代码

控件绘制原理很简单,根据控件的大小计算每一环的环宽,再根据各环的颜色一一绘制。控件触点判断原理与绘制类似,根据触点距离圆心距离求出触点在第几环,再根据触点坐标求出触点角度,根据角度判断触点选中的颜色,核心代码如下。
通过继承onMeasure方法,根据控件大小求出颜色色块大小及整体圆半径;

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	// TODO Auto-generated method stub
	int widthMode = MeasureSpec.getMode(widthMeasureSpec);
	int heightMode = MeasureSpec.getMode(heightMeasureSpec);
	int widthSize = MeasureSpec.getSize(widthMeasureSpec);
	int heightSize = MeasureSpec.getSize(heightMeasureSpec);
	float density = getScreenDensity();
	int defaultWidth = (int) (DEFAULT_WIDTH_DP * density);
	int defaultHeight = (int) (DEFAULT_HEIGHT_DP * density);
	if (widthMode == MeasureSpec.UNSPECIFIED
			|| widthMode == MeasureSpec.AT_MOST) {
		widthMeasureSpec = MeasureSpec.makeMeasureSpec(defaultWidth,
				MeasureSpec.EXACTLY);
		mWidth = defaultWidth;
	} else {
		mWidth = widthSize;
	}

	if (heightMode == MeasureSpec.UNSPECIFIED
			|| heightMode == MeasureSpec.AT_MOST) {
		heightMeasureSpec = MeasureSpec.makeMeasureSpec(defaultHeight,
				MeasureSpec.EXACTLY);
		mHeight = defaultHeight;
	} else {
		mHeight = heightSize;
	}

	centerX = mWidth / 2;
	centerY = mHeight / 2;
	int radius = getBigCircleRadius();
	/*
	 * 计算色块大小
	 */
	colorBlockSize = radius / (colorArray.length + 1);
	super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

(2)根据触点计算选择的颜色

private void computeTouchBlock() {
	if (touchX == 0 && touchY == 0) {
		return;
	}

	int tmpColorArrId = currentColorArrId;
	int tmpColorId = currentColorId;

	double distanceFromCenter = getDistanceFromCenter();
	if (distanceFromCenter >= getBigCircleRadius()) {
		return;
	}

	/*
	 * 计算色环ID
	 */
	currentColorArrId = (int) (distanceFromCenter / colorBlockSize);
	if (currentColorArrId == (colorArray.length + 1)) {
		currentColorArrId -= 1;
	}

	/*
	 * 计算当前选中的色块在色环中的位置
	 */
	if (currentColorArrId != 0) {
		String[] colors = colorArray[currentColorArrId - 1];
		/* 根据当前环颜色数计算角度步长 */
		int angleStep = 360 / colors.length;
		int angle = (int) (Math.atan2(touchY - centerY, touchX - centerX) * 180 / Math.PI);
		angle %= 360;
		if (angle < -angleStep / 2) {
			angle += 360;
		}

		if (angle > 360 - angleStep / 2) {
			angle -= 360;
		}
		currentColorId = angle / angleStep;
	}

	/*
	 * 判断选择的色块是否已经改变
	 */
	if (tmpColorArrId != currentColorArrId || tmpColorId != currentColorId) {
		isBlockChanged = true;
	}

	if (mListener == null) {
		return;
	}

	/**
	 * 处理颜色监听
	 */
	if (currentColorArrId != 0) {
		String color = colorArray[currentColorArrId - 1][currentColorId];
		int colorInInt = Color.parseColor(color);
		if (isRelease) {
			isRelease = false;
			mListener.onColorSelected(Color.red(colorInInt),
					Color.green(colorInInt), Color.blue(colorInInt));
		} else {
			mListener.onColorSelecting(Color.red(colorInInt),
					Color.green(colorInInt), Color.blue(colorInInt));
		}
	} else {
		if (isRelease) {
			isRelease = false;
			mListener.onColorSelected(0xff, 0xff, 0xff);
		} else {
			mListener.onColorSelecting(0xff, 0xff, 0xff);
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

(3)绘制各色相环

private void drawColors(Canvas canvas) {
	mPaint.setColor(Color.WHITE);
	mPaint.setStyle(Style.FILL);
	canvas.drawCircle(centerX, centerY, colorBlockSize, mPaint);
	for (int colorArrId = 0; colorArrId < colorArray.length; colorArrId++) {
		String[] colors = colorArray[colorArrId];
		/* 根据当前环颜色数计算角度步长 */
		int angleStep = 360 / colors.length;
		int radius = (colorArrId + 1) * colorBlockSize + colorBlockSize / 2;
		for (int colorId = 0; colorId < colors.length; colorId++) {
			int startAngle = -angleStep / 2 + colorId * angleStep;
			drawColorBlock(canvas, radius, colors[colorId], startAngle,
					angleStep);
		}

	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

(4)绘制色块

private void drawColorBlock(Canvas canvas, int radius, String color,
		int startAngle, int sweepAngle) {
		mPaint.setStrokeWidth(colorBlockSize);
		mPaint.setStyle(Style.STROKE);
		mPaint.setColor(Color.parseColor(color));
		RectF oval = new RectF(centerX - radius, centerY - radius, centerX
			+ radius, centerY + radius);
		canvas.drawArc(oval, startAngle, sweepAngle, false, mPaint);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

(5)绘制选中效果

private void drawTouchBlock(Canvas canvas) {
	if (touchX == 0 && touchY == 0) {
		return;
	}

	if (currentColorArrId == 0) {
		mPaint.setStyle(Style.FILL);
		mPaint.setColor(Color.parseColor("#33ffffff"));
		canvas.drawCircle(centerX, centerY, colorBlockSize, mPaint);
	} else {
		String[] colors = colorArray[currentColorArrId - 1];
		/* 根据当前环颜色数计算角度步长 */
		int angleStep = 360 / colors.length;
		int radius = (currentColorArrId + 1) * colorBlockSize
				+ colorBlockSize / 2;
		int startAngle = -angleStep / 2 + currentColorId * angleStep;
		drawColorBlock(canvas, radius, "#33ffffff", startAngle, angleStep);
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

控件整体代码

以上为控件核心代码,控件整体代码如下。

package com.xiaojianya.view;

import java.io.FileDescriptor;
import java.io.IOException;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;

/**
 * 颜色选择器,用于实现色相环颜色选择效果
 * 
 * @author sk8snow
 * 
 */
public class ColorPicker extends View {
	/**
	 * 自定义画笔,用于绘制控件
	 */
	private Paint mPaint;
	/**
	 * 控件高
 */
private int mHeight;
/**
 * 控件宽
 */
private int mWidth;
/**
 * 控件中心点X坐标
 */
private int centerX;
/**
 * 控件中心点Y坐标
 */
private int centerY;
/**
 * 色块大小
 */
private int colorBlockSize = 0;
/**
 * 触摸点X坐标
 */
private float touchX = 0;
/**
 * 触摸点Y坐标
 */
private float touchY = 0;

/**
 * 默认大小,使用dp作为单位
 */
private static final int DEFAULT_WIDTH_DP = 300;
/**
 * 默认大小,使用dp作为单位
 */
private static final int DEFAULT_HEIGHT_DP = 300;
/**
 * 选中色相环ID
 */
private int currentColorArrId = -1;
/**
 * 选中环中颜色ID
 */
private int currentColorId = -1;
/**
 * 是否触点释放
 */
private boolean isRelease = false;
/**
 * 是否选择的色块改变
 */
private boolean isBlockChanged = false;

/**
 * 色相环颜色值
 */
private String[][] colorArray = {
		{ "#fef5ce", "#fff3cd", "#feeeca", "#fdeac9", "#fee7c7", "#fce3c4",
				"#fbddc1", "#fad7c3", "#fad0c2", "#f2ced0", "#e6cad9",
				"#d9c7e1", "#d2c3e0", "#cfc6e3", "#cac7e4", "#c9cde8",
				"#c7d6ed", "#c7dced", "#c7e3e6", "#d2e9d9", "#deedce",
				"#e7f1cf", "#eef4d0", "#f5f7d0" },
		{ "#ffeb95", "#fee591", "#fcdf8f", "#fcd68d", "#facd89", "#f9c385",
				"#f7b882", "#f5ab86", "#f29a82", "#e599a3", "#ce93b3",
				"#b48cbe", "#a588be", "#9d8cc2", "#9491c6", "#919dcf",
				"#89abd9", "#85bada", "#86c5ca", "#9fd2b1", "#bada99",
				"#cbe198", "#dde899", "#edf099" },
		{ "#fee250", "#fed84f", "#fbce4d", "#f9c04c", "#f7b24a", "#f6a347",
				"#f39444", "#f07c4d", "#ec614e", "#d95f78", "#b95b90",
				"#96549e", "#7c509d", "#6e59a4", "#5c60aa", "#5572b6",
				"#3886c8", "#1c99c7", "#0daab1", "#57ba8b", "#90c761",
				"#b0d35f", "#ccdd5b", "#e5e756" },
		{ "#FDD900", "#FCCC00", "#fabd00", "#f6ab00", "#f39801", "#f18101",
				"#ed6d00", "#e94520", "#e60027", "#cf0456", "#a60b73",
				"#670775", "#541b86", "#3f2b8e", "#173993", "#0c50a3",
				"#0168b7", "#0081ba", "#00959b", "#03a569", "#58b530",
				"#90c320", "#b8d201", "#dadf00" },
		{ "#DBBC01", "#DAB101", "#D9A501", "#D69400", "#D28300", "#CF7100",
				"#CD5F00", "#CA3C18", "#C7001F", "#B4004A", "#900264",
				"#670775", "#4A1277", "#142E82", "#0A448E", "#005AA0",
				"#0070A2", "#018287", "#02915B", "#4A9D27", "#7DAB17",
				"#9EB801", "#BCC200", "#DBBC01" },
		{ "#B49900", "#B39000", "#B18701", "#AD7901", "#AB6B01", "#AA5B00",
				"#A84A00", "#A62D10", "#A50011", "#94003C", "#770050",
				"#540060", "#3B0263", "#2B1568", "#10226C", "#053577",
				"#004A87", "#005D88", "#006C6F", "#00784A", "#38831E",
				"#648B0A", "#829601", "#999F01" },
		{ "#9F8700", "#9E7F00", "#9D7601", "#9A6900", "#995E00", "#975000",
				"#954000", "#932406", "#92000B", "#840032", "#6A0048",
				"#4A0055", "#320057", "#240D5D", "#0C1860", "#032C6A",
				"#014076", "#005278", "#016064", "#006B41", "#2E7316",
				"#567C03", "#718500", "#888D00" } };

/**
 * 颜色选择监听器,设置此监听器用于监听控件中被选择颜色的改变
 */
private OnColorSelectedListener mListener;
/**
 * 颜色选中音效文件
 */
private FileDescriptor beepFile;

public ColorPicker(Context context) {
	this(context, null);
}

public ColorPicker(Context context, AttributeSet attrs) {
	super(context, attrs);
	init();
}

/**
 * 设置色相环颜色
 * 
 * @param colorArray
 *            各色相环颜色数组,由内向外依次绘制,索引0绘制于最内侧色环
 */
public void setColor(String[][] colorArray) {
	this.colorArray = colorArray;
	int radius = getBigCircleRadius();
	/*
	 * 计算色块大小
	 */
	colorBlockSize = radius / (colorArray.length + 1);
	postInvalidate();
}

/**
 * 设置控件颜色选择监听器,用于监听控件选择颜色的改变
 * 
 * @param listener
 *            颜色选择监听器
 */
public void setOnColorSelectedListener(OnColorSelectedListener listener) {
	mListener = listener;
}

/**
 * 初始化控件
 */
private void init() {
	mPaint = new Paint();
	mPaint.setAntiAlias(true);
	mPaint.setDither(true);
	/*
	 * 初始化选中音频文件,若需实现选中音效,去掉此处注释
	 */
//		beepFile = getAssetsFile(getContext(), "beep.mp3");
}

/**
 * 获取屏幕密度,用于屏幕适配
 * 
 * @return 屏幕密度
 */
private float getScreenDensity() {
	DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
	return dm.density;
}

@Override
protected void onDraw(Canvas canvas) {
	// TODO Auto-generated method stub
	super.onDraw(canvas);
	drawColors(canvas);
	drawTouchBlock(canvas);
	/*
	 * 播放选中音效,,若需实现选中音效,去掉此处注释
	 */
//if (isBlockChanged) {
//		playBeep();
//		}

}
/**
 * 从Asset文件夹中读入资源文件
 * 
 * @param context
 * @param filename
 *            资源文件名称
 * @return
 */
private FileDescriptor getAssetsFile(Context context, String filename) {
	AssetFileDescriptor assetFileDescriptor = null;
	try {
		assetFileDescriptor = context.getResources().getAssets()
				.openFd(filename);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	if (assetFileDescriptor != null) {
		return assetFileDescriptor.getFileDescriptor();
	} else {
		return null;
	}
}

// 播放选中音效
private void playBeep() {

	MediaPlayer mp = new MediaPlayer();

	try {
		mp.setDataSource(beepFile);
	} catch (IllegalArgumentException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IllegalStateException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

	try {
		mp.prepare();
	} catch (IllegalStateException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

	mp.setOnCompletionListener(new OnCompletionListener() {

		@Override
		public void onCompletion(MediaPlayer mp) {
			// TODO Auto-generated method stub
			mp.release();
		}
	});

	mp.start();

}

/**
 * 绘制色相环
 * 
 * @param canvas
 */
private void drawColors(Canvas canvas) {
	mPaint.setColor(Color.WHITE);
	mPaint.setStyle(Style.FILL);
	canvas.drawCircle(centerX, centerY, colorBlockSize, mPaint);
	for (int colorArrId = 0; colorArrId < colorArray.length; colorArrId++) {
		String[] colors = colorArray[colorArrId];
		/* 根据当前环颜色数计算角度步长 */
		int angleStep = 360 / colors.length;
		int radius = (colorArrId + 1) * colorBlockSize + colorBlockSize / 2;
		for (int colorId = 0; colorId < colors.length; colorId++) {
			int startAngle = -angleStep / 2 + colorId * angleStep;
			drawColorBlock(canvas, radius, colors[colorId], startAngle,
					angleStep);
		}

	}
}

/**
 * 绘制选中效果
 * 
 * @param canvas
 */
private void drawTouchBlock(Canvas canvas) {
	if (touchX == 0 && touchY == 0) {
		return;
	}

	if (currentColorArrId == 0) {
		mPaint.setStyle(Style.FILL);
		mPaint.setColor(Color.parseColor("#33ffffff"));
		canvas.drawCircle(centerX, centerY, colorBlockSize, mPaint);
	} else {
		String[] colors = colorArray[currentColorArrId - 1];
		/* 根据当前环颜色数计算角度步长 */
		int angleStep = 360 / colors.length;
		int radius = (currentColorArrId + 1) * colorBlockSize
				+ colorBlockSize / 2;
		int startAngle = -angleStep / 2 + currentColorId * angleStep;
		drawColorBlock(canvas, radius, "#33ffffff", startAngle, angleStep);
	}
}

/**
 * 获取触点与圆心的距离
 * 
 * @return 触点与圆心的距离
 */
private double getDistanceFromCenter() {
	float factor = (touchX - centerX) * (touchX - centerX);
	factor += (touchY - centerY) * (touchY - centerY);
	return Math.sqrt(factor);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
	// TODO Auto-generated method stub
	touchX = event.getX();
	touchY = event.getY();
	switch (event.getAction() & MotionEvent.ACTION_MASK) {
	case MotionEvent.ACTION_UP:
		isRelease = true;
		break;
	case MotionEvent.ACTION_MOVE:
		break;
	case MotionEvent.ACTION_DOWN:
		isRelease = false;
		break;
	}
	isBlockChanged = false;
	computeTouchBlock();
	postInvalidate();
	return true;
}

/**
 * 计算当前选中的色块位置
 */
private void computeTouchBlock() {
	if (touchX == 0 && touchY == 0) {
		return;
	}

	int tmpColorArrId = currentColorArrId;
	int tmpColorId = currentColorId;

	double distanceFromCenter = getDistanceFromCenter();
	if (distanceFromCenter >= getBigCircleRadius()) {
		return;
	}

	/*
	 * 计算色环ID
	 */
	currentColorArrId = (int) (distanceFromCenter / colorBlockSize);
	if (currentColorArrId == (colorArray.length + 1)) {
		currentColorArrId -= 1;
	}

	/*
	 * 计算当前选中的色块在色环中的位置
	 */
	if (currentColorArrId != 0) {
		String[] colors = colorArray[currentColorArrId - 1];
		/* 根据当前环颜色数计算角度步长 */
		int angleStep = 360 / colors.length;
		int angle = (int) (Math.atan2(touchY - centerY, touchX - centerX) * 180 / Math.PI);
		angle %= 360;
		if (angle < -angleStep / 2) {
			angle += 360;
		}

		if (angle > 360 - angleStep / 2) {
			angle -= 360;
		}
		currentColorId = angle / angleStep;
	}

	/*
	 * 判断选择的色块是否已经改变
	 */
	if (tmpColorArrId != currentColorArrId || tmpColorId != currentColorId) {
		isBlockChanged = true;
	}

	if (mListener == null) {
		return;
	}

	/**
	 * 处理颜色监听
	 */
	if (currentColorArrId != 0) {
		String color = colorArray[currentColorArrId - 1][currentColorId];
		int colorInInt = Color.parseColor(color);
		if (isRelease) {
			isRelease = false;
			mListener.onColorSelected(Color.red(colorInInt),
					Color.green(colorInInt), Color.blue(colorInInt));
		} else {
			mListener.onColorSelecting(Color.red(colorInInt),
					Color.green(colorInInt), Color.blue(colorInInt));
		}
	} else {
		if (isRelease) {
			isRelease = false;
			mListener.onColorSelected(0xff, 0xff, 0xff);
		} else {
			mListener.onColorSelecting(0xff, 0xff, 0xff);
		}
	}
}

/**
 * 绘制色块
 * 
 * @param canvas
 * @param radius
 *            半径
 * @param color
 *            色块颜色
 * @param startAngle
 *            开始角度
 * @param sweepAngle
 *            覆盖角度
 */
private void drawColorBlock(Canvas canvas, int radius, String color,
		int startAngle, int sweepAngle) {
	mPaint.setStrokeWidth(colorBlockSize);
	mPaint.setStyle(Style.STROKE);
	mPaint.setColor(Color.parseColor(color));
	RectF oval = new RectF(centerX - radius, centerY - radius, centerX
			+ radius, centerY + radius);
	canvas.drawArc(oval, startAngle, sweepAngle, false, mPaint);
}

/**
 * 获取整个色环的半径,取宽和高中最小值的二分之一减去8像素
 * 
 * @return 色环半径
 */
private int getBigCircleRadius() {
	int radius = mWidth > mHeight ? mHeight / 2 : mWidth / 2;
	return radius - 8;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	// TODO Auto-generated method stub
	int widthMode = MeasureSpec.getMode(widthMeasureSpec);
	int heightMode = MeasureSpec.getMode(heightMeasureSpec);
	int widthSize = MeasureSpec.getSize(widthMeasureSpec);
	int heightSize = MeasureSpec.getSize(heightMeasureSpec);
	float density = getScreenDensity();
	int defaultWidth = (int) (DEFAULT_WIDTH_DP * density);
	int defaultHeight = (int) (DEFAULT_HEIGHT_DP * density);
	if (widthMode == MeasureSpec.UNSPECIFIED
			|| widthMode == MeasureSpec.AT_MOST) {
		widthMeasureSpec = MeasureSpec.makeMeasureSpec(defaultWidth,
				MeasureSpec.EXACTLY);
		mWidth = defaultWidth;
	} else {
		mWidth = widthSize;
	}

	if (heightMode == MeasureSpec.UNSPECIFIED
			|| heightMode == MeasureSpec.AT_MOST) {
		heightMeasureSpec = MeasureSpec.makeMeasureSpec(defaultHeight,
				MeasureSpec.EXACTLY);
		mHeight = defaultHeight;
	} else {
		mHeight = heightSize;
	}

	centerX = mWidth / 2;
	centerY = mHeight / 2;
	int radius = getBigCircleRadius();
	/*
	 * 计算色块大小
	 */
	colorBlockSize = radius / (colorArray.length + 1);
	super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

/**
 * 颜色选择监听器,用于监听颜色的改变,在选择颜色过程中,当颜色改变时且未释放触点时调用onColorSelecting方法,
 * 在选择过程中所有被临时选中的颜色
 * 都将通过onColorSelecting方法回传;当最终选择的颜色确定时,调用onColorSelected,此方法回传最终选择的颜色
 * 
 * @author sk8snow
 * 
 */
public interface OnColorSelectedListener {
	/**
	 * 颜色选中改变,当未释放触点时调用此方法,此方法在选择颜色过程中可能颜色改变时就会被调用,整个过程中会被多次调用,
	 * 但所传值为选择的不同颜色值
	 * 
	 * @param red
	 * @param green
	 * @param blue
	 */
	public void onColorSelecting(int red, int green, int blue);

	/**
	 * 颜色选中改变,选中最终颜色,此方法在触点被释放时调用,在选择颜色过程中只会调用一次,所传值为最终选择的颜色值
	 * 
	 * @param red
	 * @param green
	 * @param blue
	 */
	public void onColorSelected(int red, int green, int blue);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527

}
将以上代码直接复制至项目中可直接使用此控件。

demo

控件demo下载地址,demo中开启了选中音效。控件相关demo代码,布局文件如下:

<com.xiaojianya.view.ColorPicker
    android:id="@+id/color_picker"
    android:layout_width="200dp"
    android:layout_height="200dp" />

<LinearLayout
    android:layout_marginTop="16dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical" >

    <TextView
        android:id="@+id/color_value_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp" />

    <ImageView
        android:id="@+id/color_indicator"
        android:layout_width="25dp"
        android:layout_height="25dp" />
</LinearLayout>

<Button 
    android:layout_marginTop="16dp"
    android:id="@+id/change_color_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="改变色相环"/>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

Activity代码如下:

package com.xiaojianya.colorpickerdemo;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.xiaojianya.view.ColorPicker;
import com.xiaojianya.view.ColorPicker.OnColorSelectedListener;


public class MainActivity extends Activity implements OnClickListener {
	private ColorPicker colorPicker;
	private TextView colorValueTxt;
	private ImageView colorIndicator;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();
}

private void init(){
	colorPicker = (ColorPicker)findViewById(R.id.color_picker);
	colorPicker.setOnColorSelectedListener(new OnColorSelectedListener() {
		
		@Override
		public void onColorSelecting(int red, int green, int blue) {
			// TODO Auto-generated method stub
			String value = "红色:" +  red + "\t绿色:" + green + "\t蓝色:" + blue;
			int color = Color.rgb(red, green, blue);
			value += "\n颜色值:0x" + Integer.toHexString(color);
			colorValueTxt.setText(value);
			colorIndicator.setBackgroundColor(color);
		}
		
		@Override
		public void onColorSelected(int red, int green, int blue) {
			// TODO Auto-generated method stub
			String value = "红色:" +  red + "\t绿色:" + green + "\t蓝色:" + blue;
			int color = Color.rgb(red, green, blue);
			value += "\n颜色值:0x" + Integer.toHexString(color);
			colorValueTxt.setText(value);
			colorIndicator.setBackgroundColor(color);
		}
	});
	
	colorValueTxt = (TextView)findViewById(R.id.color_value_txt);
	colorIndicator = (ImageView)findViewById(R.id.color_indicator);
	findViewById(R.id.change_color_btn).setOnClickListener(this);
}

@Override
public void onClick(View arg0) {
	// TODO Auto-generated method stub
	
	String[][] colorArray = {
			{ "#fef5ce", "#fff3cd", "#feeeca", "#fdeac9", "#fee7c7", "#fce3c4",
					"#fbddc1", "#fad7c3", "#fad0c2", "#f2ced0", "#e6cad9",
					"#d9c7e1", "#d2c3e0", "#cfc6e3", "#cac7e4", "#c9cde8",
					"#c7d6ed", "#c7dced", "#c7e3e6", "#d2e9d9", "#deedce",
					"#e7f1cf", "#eef4d0", "#f5f7d0" },
			{ "#ffeb95", "#fee591", "#fcdf8f", "#fcd68d", "#facd89", "#f9c385",
					"#f7b882", "#f5ab86", "#f29a82", "#e599a3", "#ce93b3",
					"#b48cbe", "#a588be", "#9d8cc2", "#9491c6", "#919dcf",
					"#89abd9", "#85bada", "#86c5ca", "#9fd2b1", "#bada99",
					"#cbe198", "#dde899", "#edf099" },
			{ "#fee250", "#fed84f", "#fbce4d", "#f9c04c", "#f7b24a", "#f6a347",
					"#f39444", "#f07c4d", "#ec614e", "#d95f78", "#b95b90",
					"#96549e", "#7c509d", "#6e59a4", "#5c60aa", "#5572b6",
					"#3886c8", "#1c99c7", "#0daab1", "#57ba8b", "#90c761",
					"#b0d35f", "#ccdd5b", "#e5e756" },
			{ "#FDD900", "#FCCC00", "#fabd00", "#f6ab00", "#f39801", "#f18101",
					"#ed6d00", "#e94520", "#e60027", "#cf0456", "#a60b73",
					"#670775", "#541b86", "#3f2b8e", "#173993", "#0c50a3",
					"#0168b7", "#0081ba", "#00959b", "#03a569", "#58b530",
					"#90c320", "#b8d201", "#dadf00" },
			{ "#DBBC01", "#DAB101", "#D9A501", "#D69400", "#D28300", "#CF7100",
					"#CD5F00", "#CA3C18", "#C7001F", "#B4004A", "#900264",
					"#670775", "#4A1277", "#142E82", "#0A448E", "#005AA0",
					"#0070A2", "#018287", "#02915B", "#4A9D27", "#7DAB17",
					"#9EB801", "#BCC200", "#DBBC01" }};
	colorPicker.setColor(colorArray);
	
}

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/254502
推荐阅读
相关标签
  

闽ICP备14008679号