我怎么能给android中的十六进制颜色代码赋予颜色名称

时间:2015-03-07 17:44:19

标签: android image-processing colors rgb

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);

        // Find references to the GUI objects
        button = (Button) findViewById(R.id.button);
        image = (ImageView) findViewById(R.id.image);
        // TextView id
        tv_selected_colour = (TextView) findViewById(R.id.tv_color_selected);
        // Set button's onClick listener object.
        button.setOnClickListener(this);
        // access images using bitmap
        final Bitmap bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();
        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();
                //xand y coordinates stored in to pixel
                int pixel = bitmap.getPixel(x, y);
                //RGB values
                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);
                // hex colour display
                tv_selected_colour.setText("touched color:" + "#" + Integer.toHexString(redValue) + Integer.toHexString(greenValue) + Integer.toHexString(blueValue));

                tv_selected_colour.setTextColor(pixel);

                return false;
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Here we need to check if the activity that was triggers was the Image
        // Gallery.
        // If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
        // If the resultCode is RESULT_OK and there is some data we know that an
        // image was picked.
        if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK
                && data != null) {
            // Let's read picked image data - its URI
            Uri pickedImage = data.getData();
            // Let's read picked image path using content resolver
            String[] filePath = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(pickedImage, filePath,
                    null, null, null);
            cursor.moveToFirst();
            String imagePath = cursor.getString(cursor
                    .getColumnIndex(filePath[0]));

            // Now we need to set the GUI ImageView data with data read from the
            // picked file.
            image.setImageBitmap(BitmapFactory.decodeFile(imagePath));

            // At the end remember to close the cursor or you will end with the
            // RuntimeException!
            cursor.close();
        }
    }

    @Override
    public void onClick(View v) {
        // Create the Intent for Image Gallery.
        Intent i = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

        // Start new activity with the LOAD_IMAGE_RESULTS to handle back the
        // results when image is picked from the Image Gallery.
        startActivityForResult(i, LOAD_IMAGE_RESULTS);
    }

此代码从图库中选择图像,并在图像上触摸Hex颜色代码..现在我想显示颜色名称以及#fff White#000 black等..任何人都可以建议请问怎么样?这可能吗?

0 个答案:

没有答案