在 android 中更改特定区域的图像颜色

Change image color in a specific area in android

我的 android 应用程序中加载了一张图片,用户可以 select 该图片的特定区域。我只想更改用户 select 指定区域 中的 RGB 颜色 ,图像的其余部分必须保持不变。我可以改变图像中的 RGB 颜色,但我不能只改变它的一部分。

    private int[] ChannelRed(Bitmap mBitmap)
    {
      int picw, pich;
      picw = mBitmap.getWidth();
      pich = mBitmap.getHeight();
      int[] pix = new int[picw * pich];
      mBitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);
      for (int y = 0; y < pich; y++)
         for (int x = 0; x < picw; x++)
         {
            int index = y * picw + x;
            int r = (pix[index] >> 16) & 0xff;
            pix[index] = 0xff000000 | (r << 16) | (0 << 8) | 0;
         }
      return pix;
    }

此代码应该可以帮助您了解该区域的工作。

Point2D p1 = null;
Point2D p2 = null;
@Override
public boolean onTouchEvent(MotionEvent event) {
     pointX = event.getX();
     pointY = event.getY();
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            p1 = new Point2D(pointX, pointY);
            return true;
        case MotionEvent.ACTION_UP:
            p2 = new Point2D(pointX, pointY);
            postInvalidate();
            break;
        default:
            return false;
    }
    return true;
}

 private int[] ChannelRed(Bitmap mBitmap)
    {
      int picw, pich;
      int sx = p1.getX();
      int sy = p1.getY();
      int fx = p2.getX();
      int fy = p2.getY();
      picw = mBitmap.getWidth();
      pich = mBitmap.getHeight();
      int[] pix = new int[picw * pich];
      mBitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);
      for (int y = sy; y < fy; y++)
         for (int x = sx; x < fx; x++)
         {
            int index = y * picw + x;
            int r = (pix[index] >> 16) & 0xff;
            pix[index] = 0xff000000 | (r << 16) | (0 << 8) | 0;
         }
      p1=null;
      p2=null;
      return pix;
    }