有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在Android编程中从图像中选择像素并降低该像素的不透明度

我想从图像视图中显示的图像中选择一个像素,然后更改同一像素的不透明度,而不更改其余图像像素的不透明度。 例如,我选择了一个位置为x=50,y=70的点,我只想在这个点上改变不透明度,而不是在其他地方改变不透明度。我该怎么做


共 (1) 个答案

  1. # 1 楼答案

    以下是我设法做到的:

    // Decode the bitmap resource and make sure it's mutable.
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inMutable = true;
    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.black, opts);
    
    imv.setImageBitmap(bm);
    
    imv.setOnTouchListener((v, event) -> {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            int x = (int) event.getX();
            int y = (int) event.getY();
    
            // Get the color of the touched pixel and change only the alpha component.
            int color = bm.getPixel(x, y);
            int alpha = color >>> 24;
            alpha /= 2;  // Alpha will be divided by two in the final color.
            color = color & 0xFFFFFF | alpha << 24;
    
            // Change the pixel color on the bitmap and update the ImageView.
            bm.setPixel(x, y, color);
            imv.setImageBitmap(bm);
            imv.invalidate();
            return true;
        }
        return false;
    });
    

    这比你要求的多一点,但我需要测试一下。单击ImageView时,单击的像素的不透明度(alpha)减半

    这在大多数手机上几乎看不到,因为屏幕的密度非常高