簡體   English   中英

如何從圖像中獲取 select 像素並在 Android 編程中降低該像素的不透明度

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

我想要 select 圖像視圖中顯示的圖像的一個像素,然后更改同一像素的不透明度而不更改圖像像素的 rest 的不透明度。 例如,我 select 一個點 position 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) 減半。

這實際上在大多數手機上幾乎察覺不到,因為屏幕密度非常高。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM