一个小程序需要用到选择简单的几个颜色,看github上的ColorPicker和Colorpickercollection都很不错。
不过Colorpickercollection比较适合,代码感觉复杂了点,颜色可配置,大项目可以使用很不错。
简单的模仿一下,不考虑配置(可以用资源配置读取),滚动(layout布局),双缓存重绘等复杂问题。
先看最终效果图:
其实一个自定义的View就可以实现,简单功能简单实现,参考代码:
package com.linjimu.todotask;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class vColorsPicker extends View {
private int Padding = 50;
private float Rd = 40f;
private float Hsp = 100f;
private int iCountPerRow = 5;
private List<Integer> listColors;
private float w;
private float h;
private static final int DEFAULT_VIEW_WIDTH = 700;
private static final int DEFAULT_VIEW_HEIGHT = 600;
private Paint mPaint;
private float xClick;
private float yClick;
private int selectedColor = Color.rgb(74, 88, 111);
public vColorsPicker(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
listColors = new ArrayList<Integer>();
listColors.add(Color.rgb(255, 168, 148));
listColors.add(Color.rgb(251, 200, 83));
listColors.add(Color.rgb(142, 215, 212));
listColors.add(Color.rgb(147, 197, 228));
listColors.add(Color.rgb(168, 153, 223));
listColors.add(Color.rgb(247, 124, 124));
listColors.add(Color.rgb(197, 219, 84));
listColors.add(Color.rgb(75, 211, 220));
listColors.add(Color.rgb(120, 223, 255));
listColors.add(Color.rgb(124, 170, 247));
listColors.add(Color.rgb(240, 92, 92));
listColors.add(Color.rgb(251, 168, 100));
listColors.add(Color.rgb(65, 186, 186));
listColors.add(Color.rgb(80, 187, 230));
listColors.add(Color.rgb(129, 139, 221));
listColors.add(Color.rgb(242, 141, 191));
listColors.add(Color.rgb(242, 170, 231));
listColors.add(Color.rgb(141, 54, 170));
listColors.add(Color.rgb(90, 150, 224));
listColors.add(Color.rgb(120, 133, 157));
listColors.add(Color.rgb(224, 112, 168));
listColors.add(Color.rgb(187, 195, 212));
listColors.add(Color.rgb(191, 191, 191));
listColors.add(Color.rgb(93, 120, 165));
listColors.add(Color.rgb(65, 68, 73));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawBg(canvas);
}
private void drawBg(Canvas canvas) {
w = getWidth();
h = getHeight();
float Wsp = (w - 2 * (Padding + Rd)) / (iCountPerRow - 1);
float x, y;
Rect rect;
int index;
for (int i = 0; i < iCountPerRow; i++) {
y = Padding + Rd + Hsp * i;
for (int j = 0; j < iCountPerRow; j++) {
index = i * iCountPerRow + j;
if (index < listColors.size()) {
x = Padding + Rd + Wsp * j;
rect = new Rect((int) (x - Rd), (int) (y - Rd), (int) (x + Rd), (int) (y + Rd));
if (xClick > 0 && yClick > 0) {
if ((xClick > x - Rd & xClick < x + Rd) && (yClick > y - Rd & yClick < y + Rd)) {
mPaint.setColor(Color.RED);
mPaint.setStyle(Style.STROKE);
canvas.drawRect(rect, mPaint);
selectedColor = listColors.get(index);
}
}
mPaint.setStyle(Style.FILL_AND_STROKE);
mPaint.setColor(listColors.get(index));
canvas.drawCircle(x, y, Rd, mPaint);
}
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = measureDimension(DEFAULT_VIEW_WIDTH, widthMeasureSpec);
int height = measureDimension(DEFAULT_VIEW_HEIGHT, heightMeasureSpec);
setMeasuredDimension(width, height);
//super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
protected int measureDimension(int defaultSize, int measureSpec) {
int result = defaultSize;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
//1. layout给出了确定的值,比如:100dp
//2. layout使用的是match_parent,但父控件的size已经可以确定了,比如设置的是具体的值或者match_parent
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;//建议:result直接使用确定值
}
//1. layout使用的是wrap_content
//2. layout使用的是match_parent,但父控件使用的是确定的值或者wrap_content
else if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(defaultSize, specSize);//建议:result不能大于specSize
}
//UNSPECIFIED,没有任何限制,所以可以设置任何大小
//多半出现在自定义的父控件的情况下,期望由自控件自行决定大小
else {
result = defaultSize;
}
return result;
}
@Override
public boolean performClick() {
return super.performClick();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
xClick = event.getX();
yClick = event.getY();
invalidate();
performClick();
}
return true;
}
public int getSelectedColor() {
return selectedColor;
}
}