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

How can I get the color from a pixel on the screen in React Native?

本文关键字:像素 颜色 获取 屏幕 React Native      更新时间:2023-09-26

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

有什么帮助吗?

请参阅此示例。。

使用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颜色代码。。