简体   繁体   English

如何从图像中获取 select 像素并在 Android 编程中降低该像素的不透明度

[英]How to select a pixel from an image and reduce the opacity of that pixel in Android programming

I want to select a pixel from an image that is displayed in the Image View and then change the opacity of the same pixel without changing the opacity of the rest of the image pixels.我想要 select 图像视图中显示的图像的一个像素,然后更改同一像素的不透明度而不更改图像像素的 rest 的不透明度。 For example, I select a point with position x = 50 and y = 70, and I just want the opacity to change in this point and not change the opacity elsewhere.How can I do that?例如,我 select 一个点 position x = 50 和 y = 70,我只想改变这个点的不透明度而不改变其他地方的不透明度。我该怎么做?

Here's what I managed to do:这是我设法做的:

// 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;
});

It's a little more than what you asked but I needed to test it.这比你问的要多一点,但我需要测试一下。 When you click on the ImageView, the opacity (alpha) of the pixel you clicked on is halved.当您单击 ImageView 时,您单击的像素的不透明度 (alpha) 减半。

That's actually barely perceptible on most phones because the screen has a very high density.这实际上在大多数手机上几乎察觉不到,因为屏幕密度非常高。

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

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