gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:42:24 25 4
gpt4 key购买 nike

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

最佳答案

这是我成功做到的:

// 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) 减半。

这实际上在大多数手机上几乎察觉不到,因为屏幕密度非常高。

关于java - 如何在 Android 编程中从图像中选择像素并降低该像素的不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61837699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com