#2
q2152362132015-05-10 20:57
|
程序代码:
private void initIdentifyTabView() {
// TODO Auto-generated method stub
final TextView sample01 = (TextView) identifyTabView
.findViewById(R.id.sample01);
final TextView sample02 = (TextView) identifyTabView
.findViewById(R.id.sample02);
final TextView sample03 = (TextView) identifyTabView
.findViewById(R.id.sample03);
final TextView textColorDiff = (TextView) identifyTabView
.findViewById(R.id.textColorDiff);
final TextView textColorInfo = (TextView) identifyTabView
.findViewById(R.id.textColorInfo);
final View viewPickedColor = identifyTabView
.findViewById(R.id.viewPickedColor);
Button btnCamera = (Button) identifyTabView
.findViewById(R.id.btnCamera);
// 设置初始默认显示的辨色图片
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.android);
imageview = identifyTabView.findViewById(R.id.viewPicture);
imageview.setBackgroundDrawable(new BitmapDrawable(bmp));
// 从触摸图片以辨别颜色
imageview.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
int w = imageview.getWidth();
int h = imageview.getHeight();
// 限制触摸位置的范围,避免超出图片之外
if (x < 0 || y < 0 || x >= w || y >= h) {
textColorInfo.setText("超出范围");
return true;
}
// 如果图像没有初始化,则先获得图像数据
if (pickbmp == null) {
Bitmap bgBmp = ((BitmapDrawable) imageview.getBackground())
.getBitmap();
pickbmp = Bitmap.createScaledBitmap(bgBmp, w, h, false);
}
// 得到当前触摸的像素颜色并显示
int pixel = pickbmp.getPixel(x, y);
viewPickedColor.setBackgroundColor(pixel);
// 显示其RGB 值
String rgb = "#"
+ Integer.toHexString(pixel).substring(2).toUpperCase();
textColorInfo.setText("当前颜色:" + rgb);
// 转换当前像素为HSV 颜色,为颜色差异计算做准备
int r = Color.red(pixel);
int g = Color.green(pixel);
int b = Color.blue(pixel);
float[] hsv = new float[3];
Color.RGBToHSV(r, g, b, hsv);
String[][] str=new String[sampleList.size()][2];
for(int i=0;i<sampleList.size();i++){
String RGB=sampleList.get(i).rgb;
int val1=Color.parseColor(RGB);
int r1=Color.red(val1);
int g1=Color.green(val1);
int b1=Color.blue(val1);
//转换rgb颜色为hsv颜色
float[] hsv1=new float[3];
Color.RGBToHSV(r1,g1,b1,hsv1);
String s=ColorSample.distHSV(hsv[1],hsv[2],hsv[3],hsv1[1],hsv1[2],hsv1[3])+"";
str[i][0]=s;
str[i][1]=RGB;
}
//数组排序
for(int i=0;i<str.length;i++){
for(int j=0;j<str.length-1;j++){
String[] ss;
if (str[i][0].compareTo(str[i + 1][0]) > 0) {
ss = str[i];
str[i] = str[i + 1];
str[i + 1] = ss;
}
}
}
String grb1=str[0][1];
String grb2=str[1][1];
String grb3=str[2][1];
textColorDiff.setText("差异度:" + Integer.parseInt(str[0][0]) + ", " + Integer.parseInt(str[1][0]) + ", "
+ Integer.parseInt(str[2][0]));
sample01.setBackgroundColor(Integer.parseInt(grb1));
sample02.setBackgroundColor(Integer.parseInt(grb2));
sample03.setBackgroundColor(Integer.parseInt(grb1));
return true;
}
});
}
//色卡数据列表,这个程序前面定义的sampleList,// TODO Auto-generated method stub
final TextView sample01 = (TextView) identifyTabView
.findViewById(R.id.sample01);
final TextView sample02 = (TextView) identifyTabView
.findViewById(R.id.sample02);
final TextView sample03 = (TextView) identifyTabView
.findViewById(R.id.sample03);
final TextView textColorDiff = (TextView) identifyTabView
.findViewById(R.id.textColorDiff);
final TextView textColorInfo = (TextView) identifyTabView
.findViewById(R.id.textColorInfo);
final View viewPickedColor = identifyTabView
.findViewById(R.id.viewPickedColor);
Button btnCamera = (Button) identifyTabView
.findViewById(R.id.btnCamera);
// 设置初始默认显示的辨色图片
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.android);
imageview = identifyTabView.findViewById(R.id.viewPicture);
imageview.setBackgroundDrawable(new BitmapDrawable(bmp));
// 从触摸图片以辨别颜色
imageview.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
int w = imageview.getWidth();
int h = imageview.getHeight();
// 限制触摸位置的范围,避免超出图片之外
if (x < 0 || y < 0 || x >= w || y >= h) {
textColorInfo.setText("超出范围");
return true;
}
// 如果图像没有初始化,则先获得图像数据
if (pickbmp == null) {
Bitmap bgBmp = ((BitmapDrawable) imageview.getBackground())
.getBitmap();
pickbmp = Bitmap.createScaledBitmap(bgBmp, w, h, false);
}
// 得到当前触摸的像素颜色并显示
int pixel = pickbmp.getPixel(x, y);
viewPickedColor.setBackgroundColor(pixel);
// 显示其RGB 值
String rgb = "#"
+ Integer.toHexString(pixel).substring(2).toUpperCase();
textColorInfo.setText("当前颜色:" + rgb);
// 转换当前像素为HSV 颜色,为颜色差异计算做准备
int r = Color.red(pixel);
int g = Color.green(pixel);
int b = Color.blue(pixel);
float[] hsv = new float[3];
Color.RGBToHSV(r, g, b, hsv);
String[][] str=new String[sampleList.size()][2];
for(int i=0;i<sampleList.size();i++){
String RGB=sampleList.get(i).rgb;
int val1=Color.parseColor(RGB);
int r1=Color.red(val1);
int g1=Color.green(val1);
int b1=Color.blue(val1);
//转换rgb颜色为hsv颜色
float[] hsv1=new float[3];
Color.RGBToHSV(r1,g1,b1,hsv1);
String s=ColorSample.distHSV(hsv[1],hsv[2],hsv[3],hsv1[1],hsv1[2],hsv1[3])+"";
str[i][0]=s;
str[i][1]=RGB;
}
//数组排序
for(int i=0;i<str.length;i++){
for(int j=0;j<str.length-1;j++){
String[] ss;
if (str[i][0].compareTo(str[i + 1][0]) > 0) {
ss = str[i];
str[i] = str[i + 1];
str[i + 1] = ss;
}
}
}
String grb1=str[0][1];
String grb2=str[1][1];
String grb3=str[2][1];
textColorDiff.setText("差异度:" + Integer.parseInt(str[0][0]) + ", " + Integer.parseInt(str[1][0]) + ", "
+ Integer.parseInt(str[2][0]));
sample01.setBackgroundColor(Integer.parseInt(grb1));
sample02.setBackgroundColor(Integer.parseInt(grb2));
sample03.setBackgroundColor(Integer.parseInt(grb1));
return true;
}
});
}
private List<ColorSample> sampleList=new ArrayList<ColorSample>();
触摸事件一触动,程序就报错了,