android 调色板小练习

在开发中一个供取色的调色板的小调查,在同学的帮助下 android (android-sdk-windows\samples\android-8\ApiDemos\src\com\example\android\apis\graphics\ColorPickerDialog.java
)的源码 中得到点思路,可是最终还是不怎么明白那个取色 设色的过程。有点上高中 大学时数学三角函数的感觉。好多都忘了 呵呵。
重写了一个View,并且定义一个界面颜色变化的监听。在类中重写onTouchEvent方法,分别判断当抬起,放下,移动时的状态变化。获取颜色几行代码,看着简单,由于知道的少理解起来很不容易。
获取位置:
float angle = (float) java.lang.Math.atan2(y, x);//不在中心圆此范围时计算颜色,将将矩形坐标 (x, y) 转换成极坐标 (r, theta),返回所得角 theta
// need to turn angle [-PI ... PI] into unit [0....1]
float unit = angle / (2 * PI);
if (unit < 0) {
unit += 1;
}
mCenterPaint.setColor(interpColor(mColors, unit));//移动过程中心圆的变化

根据位置,解析这个点的颜色
private int ave(int s, int d, float p) {
return s + java.lang.Math.round(p * (d - s));
}

private int interpColor(int colors[], float unit) {//中心圆取色,颜色解析
if (unit <= 0) {
return colors[0];
}
if (unit >= 1) {
return colors[colors.length - 1];
}

float p = unit * (colors.length - 1);
int i = (int) p;
p -= i;

// now p is just the fractional part [0...1) and i is the index
int c0 = colors[i];
int c1 = colors[i + 1];
int a = ave(Color.alpha(c0), Color.alpha(c1), p);
int r = ave(Color.red(c0), Color.red(c1), p);
int g = ave(Color.green(c0), Color.green(c1), p);
int b = ave(Color.blue(c0), Color.blue(c1), p);

return Color.argb(a, r, g, b);
}

最后返回的颜色既是选择的颜色。这块不怎么明白。
下面是色彩环的颜色定义:
mColors = new int[] {
0xFFFF0000,0xFF00FF00,
0xFF00FFFF, 0xFFFFFFFF, 0xFF00FFFF, 0xFF0000FF,
0xFFFF00FF, 0xFFFF0000
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值