`

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
    }; 
分享到:
评论
3 楼 j67065 2012-08-27  
还是谢谢!我知道怎么用了,就是对那个颜色的解析函数的返回式子的算法不是很明白,希望大神能够帮下忙
private int ave(int s, int d, float p) { 
        return s + java.lang.Math.round(p * (d - s)); 
    } 
2 楼 蓝月儿 2012-08-20  
j67065 写道
有点看不懂

一年了,我都忘了怎么弄出来的了,
1 楼 j67065 2012-06-08  
有点看不懂

相关推荐

Global site tag (gtag.js) - Google Analytics