想在我的颜色选择器Android应用程序中显示颜色名称

时间:2015-03-10 09:51:04

标签: java android color-picker

image.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            int x = (int) event.getX();

            int y = (int) event.getY();

            final Bitmap bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();

            int pixel = bitmap.getPixel(x, y);

            redValue = Color.red(pixel);
            blueValue = Color.blue(pixel);
            greenValue = Color.green(pixel); 

            Log.d("***RGB***", "X: "+x+" Y: "+y /*+" Green: "+greenValue*/);

            tv_selected_colour.setText(""+redValue+""+blueValue+""+greenValue);
            tv_selected_colour.setText("touched color:" + "#" + Integer.toHexString(redValue) + Integer.toHexString(greenValue) + Integer.toHexString(blueValue));

            tv_selected_colour.setTextColor(pixel);
            return false;
        }
});

这是我的代码,显示RGB颜色但我想显示颜色名称..我找到了这个java库https://gist.github.com/nightlark/6482130,我怎样才能在我的项目中实现这个...有人能建议我吗? ??

2 个答案:

答案 0 :(得分:1)

只需将该类添加到项目中并调用它

即可
@Override
        public boolean onTouch(View v, MotionEvent event) {

            int x = (int) event.getX();

            int y = (int) event.getY();

            final Bitmap bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();

            int pixel = bitmap.getPixel(x, y);

            redValue = Color.red(pixel);
            blueValue = Color.blue(pixel);
            greenValue = Color.green(pixel);

            // instantiate it and call getColorName
            ColorNameLookup CNL = new ColorNameLookup();
            String name = CNL.getColorName(redValue,greenValue,blueValue);  

            Log.d("***RGB***", "X: "+x+" Y: "+y /*+" Green: "+greenValue*/);

            tv_selected_colour.setText(""+redValue+""+blueValue+""+greenValue);
            tv_selected_colour.setText("touched color:" + "#" + Integer.toHexString(redValue) + Integer.toHexString(greenValue) + Integer.toHexString(blueValue));

            tv_selected_colour.setTextColor(pixel);
            return false;
        }
});

答案 1 :(得分:0)

使用静态方法创建一个类,如

public class Color{
    private static HashMap<String,String> colors;

    public static void init(){
      // add you color here
      colors = new HashMap<String,String>();
      colors.put("000000" , "white");
      // and so on
    }

    public static String getColorName(String colorValue){
       if (colors.containsKey(colorValue))
           return colors.get(colorValue);
       else return "No such Color";
    }
}

当你的活动开始运作时,请在下面打电话。

Color.init();

请在下方拨打以获取颜色名称

Color.getColorName(String.valueOf(redValue)+String.valueOf(greenValue)+String.valueOf(blueValue));