繁体   English   中英

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

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

我想渲染照片,并允许用户通过触摸选择颜色。 但是我仍然找不到在触摸事件下检索绘制的像素的方法。

有什么帮助吗?

请参见此示例。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM