如何从React Native屏幕上的像素中获取颜色?

时间:2016-07-25 15:14:12

标签: javascript android ios reactjs react-native

我想渲染一张照片,并允许用户通过触摸来选择颜色。但是我无论如何都无法找到触摸事件下的绘制像素。

任何帮助?

1 个答案:

答案 0 :(得分:0)

参见此示例..

使用OntouchListener检测你对imageView的触摸..

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

        float eventX = event.getX();
        float eventY = event.getY();
        float[] eventXY = new float[]{eventX, eventY};

        int x = Integer.valueOf((int) eventXY[0]);
        int y = Integer.valueOf((int) eventXY[1]);

        Drawable imgDrawable = ((ImageView) view).getDrawable();
        Bitmap bitmap = ((BitmapDrawable) imgDrawable).getBitmap();

        //Limit x, y range within bitmap
        if (x < 0) {
            x = 0;
        } else if (x > bitmap.getWidth() - 1) {
            x = bitmap.getWidth() - 1;
        }

        if (y < 0) {
            y = 0;
        } else if (y > bitmap.getHeight() - 1) {
            y = bitmap.getHeight() - 1;
        }

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

        int r = Color.red(touchedRGB);
        int b = Color.blue(touchedRGB);
        int g = Color.green(touchedRGB);

        String rr = Integer.toString(r);
        String bb = Integer.toString(g);
        String gg = Integer.toString(b);

        String xyz = (rr+bb+gg);

        colorRGB.setText("touched color: " + "#" + xyz);

这里xyz是你的rgb颜色代码..